0

My program is crashing when I click on a button to bring me to another page. My OnClickListener seems correct but I know I've missed something. Can anyone see where I have went wrong?

error:

  08-20 13:40:30.622: E/AndroidRuntime(1026): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.LoginScr.Example/com.LoginScr.Example.CredentialsActivity}: java.lang.NullPointerException

from main activity used to call 2nd activity:

  lsRegstr.setOnClickListener (new OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent i = new Intent(LoginScrExample.this, CredentialsActivity.class);
                    startActivity(i);       
            }

credentials Activity:

     protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.register);

rName = (EditText)findViewById(R.id.reg_uname);
rCode= (EditText)findViewById(R.id.reg_pswd);

bReg = (Button) findViewById(R.id.reg_button);
bReg.setOnClickListener(this);
bRtn = (Button) findViewById(R.id.rtn_button);
bRtn.setOnClickListener(this);

Bundle RegBundle = getIntent().getExtras();

bundleRegName = RegBundle.getString(rUsername);
bundleRegCode = RegBundle.getString(rPasscode);

rName.setText(bundleRegName);
rCode.setText(bundleRegCode);
    }

@Override
public void onClick (View v) {

    rUsername = rName.getText().toString();
    rPasscode = rCode.getText().toString();

    RegDetails regDetails = new RegDetails();
    regDetails.setlName(bundleRegName);
    regDetails.setlCode(bundleRegCode);
    if(v.getId()==R.id.rtn_button){
        finish();

    }else if(v.getId()==R.id.reg_button){
        insertCredentials(regDetails);
    }
}
    private void insertCredentials(RegDetails regDetails){

        LoginDB androidOpenDBHelper = new LoginDB(this);

        SQLiteDatabase sqliteDB = androidOpenDBHelper.getWritableDatabase();

        ContentValues contentValues = new ContentValues();

        contentValues.put(LoginDB.COLUMN_NAME_USERNAME, rUsername);
        contentValues.put(LoginDB.COLUMN_NAME_PASSWORD, rPasscode);

        String[] whereClauseArgument = new String[1];
        whereClauseArgument[0] = regDetails.getlName();

        System.out.println(" Credentials Saved!" + whereClauseArgument[0]);

        sqliteDB.close();
        finish();
    }   

  }
Kamal
  • 5,462
  • 8
  • 45
  • 58
user1165694
  • 1,255
  • 2
  • 18
  • 29
  • 4
    It will be good if you can provide more exception stacktrace. It usually shows the exact line where error occurred. – Kamal Aug 21 '12 at 18:32

2 Answers2

1

There is very limited information(as stack trace) and too many possibilities of errors. You are getting NullPointerException it means some where in your code you are trying to access a null object.
One genuine possibility is at this part of the code in the 2nd activity where you are trying to get extra data from the intent. As you have not added any extra data in the 1st activity you will be receiving null and then you are trying to get string out of the bundle. Try to put a check for string to be null at this point of the code.

Bundle RegBundle = getIntent().getExtras();
if(RegBundle != null) {
    bundleRegName = RegBundle.getString(rUsername);
    bundleRegCode = RegBundle.getString(rPasscode);
}
karn
  • 5,963
  • 3
  • 22
  • 29
0

With that limited info, it could be a number of things. One of the most likely causes is not declaring the new activity in your androidmanifest.xml. Have you declared the activity you're trying to launch in your androidmanifest.xml?

Emir Kuljanin
  • 3,881
  • 1
  • 25
  • 30