-5

I am having trouble in running my Android app. It is a simple activity meant to get data from a remote database via HTTP.

I am getting these two errors in log cat:

Error parsing to json on getJarrayFromString(); org.json.JSONException: Value Database of type java.lang.String cannot be converted to JSONArray.

And:

Error at fillproductlist(): java.lang.NullPointerException

I am not very good at Java/Android and this is first time I am asking this question here. I am unable to understand what layer of my app the problem might be in (my Java code, PHP, or database code)?

Code snapshot:

private void fillProductList() {

    if (utils.isInternet()
            && CustomHttpClient.isAddressOk(Utils.LINK_PRODUCTS)) {

        final int from = bid.size();
        final int nr = 5;
        long sqliteSize = db.size(mySqlDatabase.TABLE_BOOKS);
        if (from == 0) {
            // we add the post variables and values for the request
            postParameters.add(new BasicNameValuePair("from", String
                    .valueOf(0).toString()));
            postParameters.add(new BasicNameValuePair("nr", String.valueOf(
                    nr).toString()));
        } else {
            postParameters.add(new BasicNameValuePair("from", String
                    .valueOf(from).toString()));
            postParameters.add(new BasicNameValuePair("nr", String.valueOf(
                    nr).toString()));
        }
        postParameters.add(new BasicNameValuePair("title", titleSearch));
        postParameters.add(new BasicNameValuePair("code", codeSearch));
        postParameters.add(new BasicNameValuePair("module", moduleSearch));
        if (sortOrder != null)
            postParameters.add(new BasicNameValuePair("order", sortOrder));
        if (sortType != null)
            postParameters.add(new BasicNameValuePair("by", sortType));
        task = new RequestTask("books", postParameters);
        task.setOnTaskCompleted(new OnTaskCompletedListener() {
            public void onTaskStarted() {
                if (!rlLoading.isShown()) {
                    rlLoading.startAnimation(fadeIn());
                    rlLoading.setVisibility(View.VISIBLE);
                }
                IS_PRODUCTS_TASK = true;
            }

            public void onTaskCompleted(String result) {
                try {
                    if (result != "") {
                        // Enter the remote php link 
                        // we convert the response into json array
                        jarray = utils.getJarrayFromString(result);
                        int mysqlSize = (jarray.getJSONObject(0)
                                .getInt("numRows"));
                        Log.i(DEBUG, "From " + from + " to " + mysqlSize);
                        if (from <= mysqlSize) {
                            int rows;
                            // we check to see if there is 0
                            if (jarray.length() > 0) {

                                Log.i(DEBUG,
                                        "From "
                                                + from
                                                + " to "
                                                + Math.floor(mysqlSize / nr)
                                                * nr);
                                if (from + 5 <= Math.floor(mysqlSize / nr)
                                        * nr) {
                                    rows = jarray.length();
                                } else {
                                    rows = mysqlSize % nr + 1;
                                    Utils.IS_ENDED_PRODUCT_LIST = true;
                                }
                                ArrayList<String> list = new ArrayList<String>();
                                for (int i = 1; i < rows; i++) {
                                    JSONObject row = jarray
                                            .getJSONObject(i);
                                    bid.add(row.getInt("bid"));
                                    bTitle.add(row.getString("bTitle"));
                                    bCode.add(row.getString("bCode"));
                                    bPrice.add(row.getString("bPrice")
                                            + "£");
                                    bDescription.add(row
                                            .getString("bDescription"));
                                    bModule.add(row.getString("bModule"));
                                    bImage.add(Utils.PATH
                                            + row.getString("bImage"));
                                    list.add(row.getString("bImage"));
                                    // we check if this id already exists in the db, if it doesn't exists w create new one
                                    if (!db.hasIDbooks(row.getInt("bid")))
                                        db.createRowOnBooks(
                                                row.getInt("bid"),
                                                row.getString("bTitle"),
                                                row.getString("bCode"),
                                                row.getString("bPrice"),
                                                row.getString("bDescription"),
                                                row.getString("bModule"),
                                                Utils.PATH
                                                        + row.getString("bImage"),
                                                row.getString("bSpecialOffer"),
                                                row.getInt("bSpecialDiscount"),
                                                row.getString("bDateAdded"));
                                    Log.i(DEBUG,
                                            row.getString("bDescription"));
                                }
                                new DownloadImages(list, bAdapter)
                                        .execute();
                            }
                        }
                        postParameters.removeAll(postParameters);
                    } else {
                        Utils.IS_ENDED_PRODUCT_LIST = true;
                        if (rlLoading.isShown()) {
                            rlLoading.startAnimation(fadeOut());
                            rlLoading.setVisibility(View.INVISIBLE);
                        }
                    }
                } catch (Exception e) {
                    Log.e(DEBUG,
                            "Error at fillProductList(): " + e.toString());
                }
            }
        });
        task.execute();
    } else {
        // if we are not connected on internet or somehow the link would not work, then we will take the rows stored in sqlite db
        if (db.size(mySqlDatabase.TABLE_BOOKS) > 0) {
            Cursor cursor = db.getBookssRows(mySqlDatabase.TABLE_BOOKS);
            cursor.moveToFirst();
            while (!cursor.isAfterLast()) {
                bid.add(cursor.getInt(cursor
                        .getColumnIndex(mySqlDatabase.KEY_BID)));
                bTitle.add(cursor.getString(cursor
                        .getColumnIndex(mySqlDatabase.KEY_BTITLE)));
                bCode.add(cursor.getString(cursor
                        .getColumnIndex(mySqlDatabase.KEY_BCODE)));
                bPrice.add(cursor.getString(cursor
                        .getColumnIndex(mySqlDatabase.KEY_BPRICE)) + "£");
                bDescription.add(cursor.getString(cursor
                        .getColumnIndex(mySqlDatabase.KEY_BDESCRIPTION)));
                bModule.add(cursor.getString(cursor
                        .getColumnIndex(mySqlDatabase.KEY_BMODULE)));
                bImage.add(cursor.getString(cursor
                        .getColumnIndex(mySqlDatabase.KEY_BIMAGE)));
                cursor.moveToNext();
            }
            bAdapter.notifyDataSetChanged();
            Utils.IS_ENDED_PRODUCT_LIST = true;
        }
    }
}
3
  • 1
    Your code is hard to read and could benefit from some formatting. The real method we need to see is the getJarrayFromString(result). Add that to your question. Commented Mar 1, 2013 at 10:46
  • Also show the received json string, thats being parsed. Commented Mar 1, 2013 at 10:47
  • Thank you guys, I am finding it really hard to paste in correct format here. Is there any alternative way of doing this? :( I feel really stupid. Commented Mar 1, 2013 at 10:51

1 Answer 1

1

In the OnTaskCompleted before

jarray = utils.getJarrayFromString(result);

add a log entry with

Log.i(DEBUG,result);

I am suspecting that you are getting a null value as result

if(result!="")

does not ensure a valid value. Cheers

1
  • Thanks mate :) I purchased this code from someone, and unfortunately found out that there is no after sales support for this!! :( Also, do you think, there needs to be the name of the php script? eg. if (result!="books"); or something? Would you be able to see my code if I email you the whole code? Please let me know if you can? Thanks :) Commented Mar 1, 2013 at 10:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.