使用Volley错误发布数据Occure“NetworkDispatcher.run:Unhandled exception java.lang.NullPointerException:Attempt to invoke virtual method'”

时间:2016-03-12 06:10:55

标签: android android-volley

error“”NetworkDispatcher.run:未处理的异常java.lang.NullPointerException:尝试调用虚方法'int java.lang.String.length()'“” UI包含两个密码编辑文本字段和一个按钮,我打算将用户输入的密码保存到服务器。但是当我点击提交按钮时,StringRequest的函数onErrorResponce会显示上述错误。 以下是将密码保存到服务器的代码。

  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;
        }
}

以下是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)

请帮帮我

1 个答案:

答案 0 :(得分:0)

对我来说就像你的

   error.getMessage()

为空。

在错误回复中尝试使用以下代码

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();

}

看看这是否有效并让我知道。如果不能更深入地研究这个问题。

另一个可能性是

  jsonObject.getString("error_details")

为空或为空。