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.68k
    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:

     

    package volleyjson.androidhive.info.myapplication;
    
    import android.app.Activity;
    import android.app.ProgressDialog;
    import android.content.Intent;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.TextView;
    
    import com.android.volley.AuthFailureError;
    import com.android.volley.Request;
    import com.android.volley.Response;
    import com.android.volley.VolleyError;
    import com.android.volley.VolleyLog;
    import com.android.volley.toolbox.JsonObjectRequest;
    import volleyjson.androidhive.info.myapplication.app.AppController;
    
    import org.json.JSONObject;
    
    import java.util.HashMap;
    import java.util.Map;
    
    @SuppressWarnings("ALL")
    public class login extends Activity implements View.OnClickListener{
        private final String TAG =  login.class.getSimpleName();
        private static final String LOGIN_URL = "http://www.nishkarsh.co.in/api/login";
        private static final String KEY_EMAIL = "email";
        private ProgressDialog pDialog;
        private TextView t1;
        private TextView t2;
        private TextView t3;
        private EditText ed1;
        private Button b1;
        private String email;
    
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.login_dialogue);
            t1 = (TextView) findViewById(R.id.textView1);
            t2 = (TextView) findViewById(R.id.textView2);
            t3 = (TextView) findViewById(R.id.textView3);
            ed1 = (EditText) findViewById(R.id.RegEmail);
            b1 = (Button) findViewById(R.id.btnLogin);
            b1.setOnClickListener(this);
            ProgressDialog pDialog = new ProgressDialog(this);
            pDialog.setMessage("Loading...");
            pDialog.setCancelable(false);
        }
        private void showProgressDialog() {
            if (!pDialog.isShowing())
                pDialog.show();
        }
    
        private void hideProgressDialog() {
            if (pDialog.isShowing())
                pDialog.hide();
        }
        private void makeJsonObjReq() {
            showProgressDialog();
            JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.POST,
                    LOGIN_URL , null,
                    new Response.Listener<JSONObject>() {
    
                        @Override
                        public void onResponse(JSONObject response) {
                            Log.d(TAG, response.toString());
                            openProfile();
                            pDialog.hide();
                        }
                    }, new Response.ErrorListener() {
    
                @Override
                public void onErrorResponse(VolleyError error) {
                    VolleyLog.d(TAG, "Error: " + error.getMessage());
                    hideProgressDialog();
                }
            }) {
    
                /**
                 * Passing some request headers
                 * */
                @Override
                public Map<String, String> getHeaders() throws AuthFailureError {
                    HashMap<String, String> headers = new HashMap<>();
                    headers.put("Content-Type", "application/json");
                    return headers;
                }
                @Override
                protected Map<String, String> getParams() {
                    Map<String, String> params = new HashMap<>();
                    params.put(KEY_EMAIL, email);
    
                    return params;
                }
    
            };
    
            // Adding request to request queue
            String tag_json_obj = "jsonObjReq ";
            AppController.getInstance().addToRequestQueue(jsonObjReq,
                    tag_json_obj);
    
            // Cancelling request
            // ApplicationController.getInstance().getRequestQueue().cancelAll(tag_json_obj);
        }
        private void openProfile(){
            Intent intent = new Intent(login.this, MainActivity.class);
            intent.putExtra(KEY_EMAIL,email);
            startActivity(intent);
        }
    
        @Override
        public void onClick(View v) {
    
            makeJsonObjReq();
        }
    }
    
    

 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
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: