Question

I am creating a session.class for my session management for a login class, but my logcat showing error java.lang.NullPointerException and the apps can't run on emulator, please help

Here's my session.java:

public class Session {

    private SharedPreferences prefs;
    Editor editor = prefs.edit();

    public Session(Context cntx) {
        // TODO Auto-generated constructor stub
        prefs = PreferenceManager.getDefaultSharedPreferences(cntx);

    }

    public void setkdanggota(String kdanggota) {
        editor.putString("kdanggota", kdanggota).commit();
        editor.commit();
    }

    public String getusename() {
        String kdanggota = prefs.getString("kdanggota",null);
        return kdanggota;
    }
}

And here's my login.java:

public class login extends Activity{
    EditText kode,pw;
    TextView error;
    Button login;
    String i;
    private Session session;


    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);
        Context cntx = getApplicationContext();
        session = new Session(cntx );
        kode = (EditText) findViewById(R.id.kode);
        pw = (EditText) findViewById (R.id.password);
        login = (Button) findViewById (R.id.login);
        error = (TextView) findViewById (R.id.error);
        login.setOnClickListener(new View.OnClickListener(){
        @Override     
            public void onClick(View v) {
            ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
            postParameters.add(new BasicNameValuePair("kode", kode.getText().toString()));
            postParameters.add(new BasicNameValuePair("password", pw.getText().toString()));
            String response = null; 
            try { 
                response = CustomHttpClient.executeHttpPost("http://10.0.2.2/koperasidb/login.php", postParameters);
                String res = response.toString(); 
                res = res.trim();
                res = res.replaceAll("\\s+","");
                error.setText(res);
                if (res.equals("1")){
                    error.setText("Correct Username or Password");
                    session.setkdanggota(kode.getText().toString());
                    berhasil(v);  
                }
                else { 
                    error.setText("Sorry!! Wrong Username or Password Entered");
                }
            }
            catch (Exception e) {
                kode.setText(e.toString());
            }
            }
        });
    }

            public void berhasil (View theButton)
            {
                Intent s = new Intent (this, Home.class);
                startActivity(s);
            }
        }
Was it helpful?

Solution

pref is uninitialised/ null when you do this and hence editor is set to null.

Editor editor = prefs.edit();

so moving your editor initialization to constructor after you initialise prefs should help

Change this:

private SharedPreferences prefs;
    Editor editor = prefs.edit();

    public Session(Context cntx) {
        // TODO Auto-generated constructor stub
        prefs = PreferenceManager.getDefaultSharedPreferences(cntx);

    }

to

private SharedPreferences prefs;
    Editor editor;

    public Session(Context cntx) {
        // TODO Auto-generated constructor stub
        prefs = PreferenceManager.getDefaultSharedPreferences(cntx);
       editor = prefs.edit();

    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top