-3

I'm trying to use this good tutorial in Dialog like this for Login action:

public class MainActivity extends AppCompatActivity {

    private SQLiteHandler db;
    public ProgressDialog pDialog;
    public SessionManager session;
    private DrawerLayout drawerLayout;
    private static final String TAG = MainActivity.class.getSimpleName();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        // Displaying the user details on the screen

        TextView txtName = (TextView) findViewById(R.id.usernamehd);
        TextView txtEmail = (TextView) findViewById(R.id.emailuserr);
        db = new SQLiteHandler(getApplicationContext());
        HashMap<String, String> user = db.getUserDetails();
        String name = user.get("name");
        String email = user.get("email");
        txtName.setText(name);
        txtEmail.setText(email);


        TextView sgnin = (TextView) findViewById(R.id.signinbtn);
        sgnin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                final Dialog d = new Dialog(MainActivity.this);
                d.setContentView(R.layout.dialog);
                String title = getResources().getString(R.string.dialog_title);
                d.setTitle(title);
                d.show();
                // first dialog for showing that dialog Login-register
                final EditText inusremail = (EditText) d.findViewById(R.id.emailuserr);
                final EditText inpass = (EditText) d.findViewById(R.id.passworduser);
                final Button btnLogin = (Button) d.findViewById(R.id.Login);
                pDialog = new ProgressDialog(MainActivity.this);
                pDialog.setCancelable(false);

                btnLogin.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        String email = inusremail.getText().toString();
                        String password = inpass.getText().toString();

                        if (email.trim().length() > 0 && password.trim().length() > 0) {
                            checkLogin(email, password);
                        } else {
                            Toast.makeText(getApplicationContext(), "Please enter the credentials!", Toast.LENGTH_LONG).show();
                        }
                    }
                });
                                                //end the Login button

            }
        });

And here is the function for CheckLogin:

private void checkLogin(final String email, final String password) {
        // Tag used to cancel the request
        String tag_string_req = "req_login";
        pDialog.setMessage("Logging in ...");
        showDialog();

        StringRequest strReq = new StringRequest(Request.Method.POST,
                AppConfig.URL_REGISTER, new Response.Listener<String>() {

            @Override
            public void onResponse(String response) {
                Log.d(TAG, "Login Response: " + response.toString());
                hideDialog();

                try {
                    JSONObject jObj = new JSONObject(response);
                    boolean error = jObj.getBoolean("error");

                    // Check for error node in json
                    if (!error) {
                        // user successfully logged in
                        // Create login session
                        session.setLogin(true);

                        // Launch main activity
                        Intent intent = new Intent(MainActivity.this,
                                MainActivity.class);
                        startActivity(intent);
                        finish();
                    } else {
                        // Error in login. Get the error message
                        String errorMsg = jObj.getString("error_msg");
                        Toast.makeText(getApplicationContext(),
                                errorMsg, Toast.LENGTH_LONG).show();
                    }
                } catch (JSONException e) {
                    // JSON error
                    e.printStackTrace();
                }

            }
        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e(TAG, "Login Error: " + error.getMessage());
                Toast.makeText(getApplicationContext(),
                        error.getMessage(), Toast.LENGTH_LONG).show();
                hideDialog();
            }
        }) {

            @Override
            protected Map<String, String> getParams() {
                // Posting parameters to login url
                Map<String, String> params = new HashMap<String, String>();
                params.put("tag", "login");
                params.put("email", email);
                params.put("password", password);

                return params;
            }

        };

        // Adding request to request queue
        AppController.getInstance().addToRequestQueue(strReq, tag_string_req);
    }

    private void showDialog() {
        if (!pDialog.isShowing())
            pDialog.show();
    }

    private void hideDialog() {
        if (pDialog.isShowing())
            pDialog.dismiss();
    }

But, there is a problem and i dont know what is it:

java.lang.NullPointerException: Attempt to invoke virtual method 'void client.package.helper.SessionManager.setLogin(boolean)' on a null object reference
            at client.package.MainActivity$6.onResponse(MainActivity.java:257)
            at client.package.MainActivity$6.onResponse(MainActivity.java:242)
            at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:60)
            at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:30)
            at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:99)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5254)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

i just change the package name

and of course, Register is working and i can save the user credentials in the server side but, there is a similar problem in this and i dont know how can i fix that in one Acttivity.

Any help would be great.

smyslov
  • 1,279
  • 1
  • 8
  • 29
androiddroid
  • 69
  • 11

2 Answers2

0
session.setLogin(true);

You must make sure that session is initialized..

To be extra careful:

if(session!=null)
  session.setLogin(true);
Sharp Edge
  • 4,144
  • 2
  • 27
  • 41
0

The error says Object of SessionManager is null. You have not initialized the object after declaration.

SessionManager session; //declared


session = New SessionManager(Arguments); //Intialized
Karan
  • 2,120
  • 15
  • 27