2

Getting the error in Listener whenever the fb_flag is 1, m just beginner; learning volley

NullPointerException ::: Have tried to solve the error; but couldn't solve it

LoginCall.java

public class Login_Call {
    Context con;
    String email ="", pass = "";
    String fb_flag = String.valueOf(0);
    String URL = "";
    UserData userData;
    String Message = "Success";

Constructor for setting the data items;

    public Login_Call(Context context, String email, String password, String flag) {
        con = context;
        this.email = email;`enter code here`
        pass = password;
        fb_flag = flag;
        Toast.makeText(con,fb_flag, Toast.LENGTH_SHORT).show();
    }

    public UserData getUserDataObject() {
        return userData;
    }

Getting error in this method ; Method is for mapping the string data with url;

    public String getLoginAcc() {
        RequestQueue queue = Volley.newRequestQueue(con);
        StringRequest postRequest = new StringRequest(Method.POST, URL, new Listener<String>() {
            @Override
            public void onResponse(String s) {
                Log.d("LoginCheck", s);
                Toast.makeText(con,s, Toast.LENGTH_SHORT).show();
                authenticateLoginResponse(s);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Toast.makeText(con, "Server Not Responding" + volleyError, Toast.LENGTH_SHORT).show();
            }
        }) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> params = new HashMap<String, String>();
                params.put("fb_flag", fb_flag);
                params.put("email", email);
                params.put("password", pass);
                return params;

            }
        };
        postRequest.setTag("Publiko Login");
        postRequest.setRetryPolicy(new DefaultRetryPolicy(5000, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
        queue.add(postRequest);
        return Message;
    }

    private void authenticateLoginResponse(String data) {
        String response;
        try {
            JSONObject responseObject = new JSONObject(data);
            response = responseObject.getString("response");
            if (response.equals("success")) {
                // ToDo succes response Login Acces Granted
                JSONArray responseArray = responseObject.getJSONArray("data");
                JSONObject reponseInnerObject = responseArray.getJSONObject(0);
                UserData user = new UserData();
                user.setUsername(reponseInnerObject.getString("user_name"));
                user.setEmail(reponseInnerObject.getString("email"));
                user.setAddress(reponseInnerObject.getString("address"));
                user.setPhone(reponseInnerObject.getString("phone"));
                user.setUserid(reponseInnerObject.getString("user_id"));
                userData = user;
            } else {
                // ToDo Failed Response Invalid Email/Password
                this.Message = responseObject.getString("message");
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}

thanks in advance :)

Joy Gohil
  • 69
  • 1
  • 2
  • 6
  • @Marcin Orlowski, It is not just a NPE, it belongs to Volley API for android and there could be several reasons for this. – A_J Apr 26 '16 at 20:59
  • @A_J Without the logcat, you can't say it belongs to Volley – OneCricketeer Apr 27 '16 at 02:47
  • @cricket_007, it is already mentioned in title that it belongs to volley, "com.android.volley.VolleyError:java.lang.NullPointerException:Attempt to invoke virtual method 'in java.lang.String.length()' on object reference" – A_J Apr 27 '16 at 10:14

1 Answers1

0

Double check your URL and see if it is valid URL. In my case I missed base url.

A_J
  • 1,635
  • 1
  • 18
  • 31
  • This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post - you need to gain [reputation](http://stackoverflow.com/faq#reputation) before you can comment on others’ posts to prevent abuse; why don’t you try and get some by [answering a question](http://stackoverflow.com/unanswered)? – Marcin Orlowski Apr 26 '16 at 20:56