3

error ""NetworkDispatcher.run: Unhandled exception java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.length()'" " UI contains two password edit text fields and an button, which i'm intented to save the password entered by the user to the Server. but when ever i clicked the submit button the above error is displayed by the function onErrorResponce of the StringRequest. below is the code to save password to the server.

  private void savePasswordToServer() {//pass1&pass are two edittext field objects
        if(pass1.getText().toString().equals(pass.getText().toString()))
        {
            String url = new URLConstants().BaseURL+"save_password.php";
            StringRequest request = new StringRequest(Request.Method.POST, url,new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    try
                    {
                        JSONObject jsonObject = new JSONObject(response);
                        boolean e = jsonObject.getBoolean("error");
                        if(!e)
                        {
                            Toast.makeText(getApplicationContext(), "password change successful", Toast.LENGTH_SHORT).show();
                            Intent intent = new Intent(getApplicationContext(),HomeScreen.class);
                            startActivity(intent);
                        }
                        else
                            Toast.makeText(getApplicationContext(),jsonObject.getString("error_details"),Toast.LENGTH_LONG).show();
                    }catch (JSONException e)
                    {
                        Toast.makeText(getApplicationContext(),e.getMessage(),Toast.LENGTH_LONG).show();
                    }
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Toast.makeText(getApplicationContext(),"error VOLLEY "+error.getMessage(),Toast.LENGTH_LONG).show();
                }
            })
            {
                @Override
            public Map<String, String> getParams()
                {
                    Map<String, String> params = new HashMap<String, String>();
                    params.put("rollno",rollno);
                    params.put("password",pass1.getText().toString());
                    return params;
                }
            };
            MySingleton.getInstance(this).addToRequestQueue(request);
        }
        else
        {
            pass1.setError("wrong password");
            return;
        }
}

and the following is error generated in AndroiMonitor

03-12 11:35:55.020 2742-2803/? E/Volley: [155] NetworkDispatcher.run: Unhandled exception java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.length()' on a null object reference
                                     java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.length()' on a null object reference
                                         at libcore.net.UriCodec.encode(UriCodec.java:132)
                                         at java.net.URLEncoder.encode(URLEncoder.java:57)
                                         at com.android.volley.Request.encodeParameters(Request.java:413)
                                         at com.android.volley.Request.getBody(Request.java:399)
                                         at com.android.volley.toolbox.HurlStack.addBodyIfExists(HurlStack.java:236)
                                         at com.android.volley.toolbox.HurlStack.setConnectionParametersForRequest(HurlStack.java:210)
                                         at com.android.volley.toolbox.HurlStack.performRequest(HurlStack.java:106)
                                         at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:93)
                                         at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:105)

please help me

hemanth5636
  • 89
  • 2
  • 9
  • Can you show the value of the url? – Ruchira Randana Mar 12 '16 at 06:29
  • Baseuri = "http://10.0.2.2:80/po/" as im accessing the localhost from the emulator the ip address is 10.0.2.2, complete uri is "http://10.0.2.2:80/po/save_password.php". i can access other php script with no error, but only for this the error is showing. – hemanth5636 Mar 12 '16 at 07:04

1 Answers1

0

Seems to me like your

   error.getMessage()

is null.

Try with below code in your error response

if(error!=null && error.getMessage() !=null){
    Toast.makeText(getApplicationContext(),"error VOLLEY "+error.getMessage(),Toast.LENGTH_LONG).show();
}
else{
    Toast.makeText(getApplicationContext(),"Something went wrong",Toast.LENGTH_LONG).show();

}

See if this works and let me know. If it doesn't will have a deeper look into the problem.

One more possiblility is that

  jsonObject.getString("error_details")

is empty or null.

Ragesh Ramesh
  • 3,470
  • 2
  • 14
  • 20
  • 1
    Hey thank you, but im still facing the same error "error volley +" – hemanth5636 Mar 12 '16 at 07:00
  • yha same i think, below is fresh log – hemanth5636 Mar 12 '16 at 07:11
  • 03-12 12:39:21.136 2638-2677/? E/Surface: getSlotFromBufferLocked: unknown buffer: 0xb40566e0 03-12 12:39:33.363 2638-2718/? E/Volley: [137] NetworkDispatcher.run: Unhandled exception java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.length()' on a null object reference java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.length()' on a null object reference at libcore.net.UriCodec.encode(UriCodec.java:132) – hemanth5636 Mar 12 '16 at 07:11