Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Null Pointer Exception Error in Android App

    • 0
    • 0
    • 0
    • 4
    • 0
    • 0
    • 0
    • 2.72k
    Answer it

    I have faced a problem, when I clicked on login button on login activity. Below is my debug logact mentioned: 

     

    04-15 15:07:45.924 2533-2533/volleyjson.androidhive.info.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                             Process: volleyjson.androidhive.info.myapplication, PID: 2533
                                                                                             java.lang.NullPointerException: Attempt to invoke virtual method 'boolean

     

    android.app.ProgressDialog.isShowing()' on a null object reference
                                                                                                 at volleyjson.androidhive.info.myapplication.login.showProgressDialog(login.java:55)
                                                                                                 at volleyjson.androidhive.info.myapplication.login.makeJsonObjReq(login.java:64)
                                                                                                 at volleyjson.androidhive.info.myapplication.login.onClick(login.java:120)
                                                                                                 at android.view.View.performClick(View.java:5637)
                                                                                                 at android.view.View$PerformClick.run(View.java:22429)
                                                                                                 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 this is my login.java:

     

    1. package volleyjson.androidhive.info.myapplication;
    2.  
    3. import android.app.Activity;
    4. import android.app.ProgressDialog;
    5. import android.content.Intent;
    6. import android.os.Bundle;
    7. import android.util.Log;
    8. import android.view.View;
    9. import android.widget.Button;
    10. import android.widget.EditText;
    11. import android.widget.TextView;
    12.  
    13. import com.android.volley.AuthFailureError;
    14. import com.android.volley.Request;
    15. import com.android.volley.Response;
    16. import com.android.volley.VolleyError;
    17. import com.android.volley.VolleyLog;
    18. import com.android.volley.toolbox.JsonObjectRequest;
    19. import volleyjson.androidhive.info.myapplication.app.AppController;
    20.  
    21. import org.json.JSONObject;
    22.  
    23. import java.util.HashMap;
    24. import java.util.Map;
    25.  
    26. @SuppressWarnings("ALL")
    27. public class login extends Activity implements View.OnClickListener{
    28. private final String TAG = login.class.getSimpleName();
    29. private static final String LOGIN_URL = "http://www.nishkarsh.co.in/api/login";
    30. private static final String KEY_EMAIL = "email";
    31. private ProgressDialog pDialog;
    32. private TextView t1;
    33. private TextView t2;
    34. private TextView t3;
    35. private EditText ed1;
    36. private Button b1;
    37. private String email;
    38.  
    39.  
    40. @Override
    41. protected void onCreate(Bundle savedInstanceState) {
    42. super.onCreate(savedInstanceState);
    43. setContentView(R.layout.login_dialogue);
    44. t1 = (TextView) findViewById(R.id.textView1);
    45. t2 = (TextView) findViewById(R.id.textView2);
    46. t3 = (TextView) findViewById(R.id.textView3);
    47. ed1 = (EditText) findViewById(R.id.RegEmail);
    48. b1 = (Button) findViewById(R.id.btnLogin);
    49. b1.setOnClickListener(this);
    50. ProgressDialog pDialog = new ProgressDialog(this);
    51. pDialog.setMessage("Loading...");
    52. pDialog.setCancelable(false);
    53. }
    54. private void showProgressDialog() {
    55. if (!pDialog.isShowing())
    56. pDialog.show();
    57. }
    58.  
    59. private void hideProgressDialog() {
    60. if (pDialog.isShowing())
    61. pDialog.hide();
    62. }
    63. private void makeJsonObjReq() {
    64. showProgressDialog();
    65. JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.POST,
    66. LOGIN_URL , null,
    67. new Response.Listener<JSONObject>() {
    68.  
    69. @Override
    70. public void onResponse(JSONObject response) {
    71. Log.d(TAG, response.toString());
    72. openProfile();
    73. pDialog.hide();
    74. }
    75. }, new Response.ErrorListener() {
    76.  
    77. @Override
    78. public void onErrorResponse(VolleyError error) {
    79. VolleyLog.d(TAG, "Error: " + error.getMessage());
    80. hideProgressDialog();
    81. }
    82. }) {
    83.  
    84. /**
    85. * Passing some request headers
    86. * */
    87. @Override
    88. public Map<String, String> getHeaders() throws AuthFailureError {
    89. HashMap<String, String> headers = new HashMap<>();
    90. headers.put("Content-Type", "application/json");
    91. return headers;
    92. }
    93. @Override
    94. protected Map<String, String> getParams() {
    95. Map<String, String> params = new HashMap<>();
    96. params.put(KEY_EMAIL, email);
    97.  
    98. return params;
    99. }
    100.  
    101. };
    102.  
    103. // Adding request to request queue
    104. String tag_json_obj = "jsonObjReq ";
    105. AppController.getInstance().addToRequestQueue(jsonObjReq,
    106. tag_json_obj);
    107.  
    108. // Cancelling request
    109. // ApplicationController.getInstance().getRequestQueue().cancelAll(tag_json_obj);
    110. }
    111. private void openProfile(){
    112. Intent intent = new Intent(login.this, MainActivity.class);
    113. intent.putExtra(KEY_EMAIL,email);
    114. startActivity(intent);
    115. }
    116.  
    117. @Override
    118. public void onClick(View v) {
    119.  
    120. makeJsonObjReq();
    121. }
    122. }

 4 Answer(s)

  • I agree with @programmer

    ProgressDialog pDialog = new ProgressDialog(this);
    pDialog.setMessage("Loading...");
    pDialog.setCancelable(false);

    Re declaring pDialog has a local variable, overshadows the instance variable and prevents it from being initialized. Therefore the solution his to just initialize it.  

    pDialog = new ProgressDialog(this);
    pDialog.setMessage("Loading...");
    pDialog.setCancelable(false);
  • Change

    b1.setOnClickListener(this); 
    ProgressDialog pDialog = new ProgressDialog(this); 
    pDialog.setMessage("Loading..."); 
    

    to

    b1.setOnClickListener(this); 
    pDialog = new ProgressDialog(this); 
    pDialog.setMessage("Loading..."); 
    

    pDialog must be class member, in you case you shadow it with a local variable. 
    image

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Reset Password
Fill out the form below and reset your password: