R
R
run1822017-02-20 23:11:19
Java
run182, 2017-02-20 23:11:19

Why does android application crash when calling okhttp?

Here is a log:

D/CONNECT:: START
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: com.example.run18.flirtodrom, PID: 5385
                  java.lang.NullPointerException: println needs a message
                      at android.util.Log.println_native(Native Method)
                      at android.util.Log.d(Log.java:143)
                      at com.example.run18.flirtodrom.PlaceItem$GetProductDetails$1.run(PlaceItem.java:107)
                      at android.os.Handler.handleCallback(Handler.java:751)
                      at android.os.Handler.dispatchMessage(Handler.java:95)
                      at android.os.Looper.loop(Looper.java:154)
                      at android.app.ActivityThread.main(ActivityThread.java:6119)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

And here is the code:
class GetProductDetails extends AsyncTask<String, String, String> {
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(PlaceItem.this);
            pDialog.setMessage("Loading product details. Please wait...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(false);
            pDialog.show();
        }
        protected String doInBackground(String[] params) {
            runOnUiThread(new Runnable() {
                public void run() {
                    Log.d("CONNECT: ", "START");
                    try {
                        //GET
                        OkHttpClient client = new OkHttpClient();
                        Request request = new Request.Builder().url(url_db + "?action=get_place_by_id&id=" + pid).build();
                        Response response = client.newCall(request).execute();
                        String result = response.body().string();
                        Log.d("CONNECT: ", "OK");

                        /*JSONArray productObj = new JSONArray(result);
                        try {
                            if (1 == 1) {
                                JSONObject product = productObj.getJSONObject(0);

                                txtName = (EditText) findViewById(R.id.inputName);
                                txtPrice = (EditText) findViewById(R.id.inputPrice);
                                txtDesc = (EditText) findViewById(R.id.inputDesc);

                                txtName.setText(product.getString(TAG_NAME));
                                txtPrice.setText(product.getString(TAG_PRICE));
                                txtDesc.setText(product.getString(TAG_DESCRIPTION));
                            } else {
                                // продукт с pid не найден
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }*/
                    } catch (Exception e) {
                        Log.d("CONNECT: ", e.getMessage());
                    }
                }
            });
            return null;
        }
        protected void onPostExecute(String file_url) {
            pDialog.dismiss();
        }
    }

Tried to comment execute() - doesn't throw it out. The server gives the data without errors. The previous activity also has a similar okhttp call, it passes without errors.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
I
ipc_ngs, 2017-02-21
@run182

You can tell by the line number where it breaks: PlaceItem.java:107 .
Most likely inside catch when executing Log.d("CONNECT: ", e.getMessage()); due to null e.getMessage().
Leave only e there, and then you will see what kind of exception occurs when executing () .

R
Rou1997, 2017-02-21
@Rou1997

} catch (Exception e) {
    Log.d("CONNECT: ", e.getMessage());
}

Who writes like that?
Not only can this lead to problems like this, but it's also inefficient since the message may not be there at all, or be useless.
God Himself ordered to write like this:
} catch (Exception e) {
   e.printStackTrace();
}

If this is not enough, then write your own util function, but here you need to think well.

A
aol-nnov, 2017-02-20
@aol-nnov

stackoverflow.com/questions/8237080/exception-getm...
will Pushkin put down line numbers? the answer, in fact, in a stacktrace at a distance of one mouse click (unless, of course, you program in a notepad)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question