Reputation: 1
Am passing a string from on activity to another, its working but if i try and open the activity without passing the strings It throws a Null pointer exception, Kindly Assit
Bundle gotBasket = getIntent().getExtras();
gotPassenger= gotBasket.getString("passenger");
gotStaffNumber= gotBasket.getString("clientcode");
etPassenger.setText(""+ gotPassenger );
etStaffNumber.setText("" + gotStaffNumber);
Upvotes: 0
Views: 117
Reputation: 8028
if i try and open the activity without passing the strings It throws a Null pointer exception
right, becuase you didnt send any data
check if the gotBasket is NULL before assigning
like this:
Bundle gotBasket = getIntent().getExtras();
if(gotBasket != null){
gotPassenger= gotBasket.getString("passenger");
gotStaffNumber= gotBasket.getString("clientcode");
etPassenger.setText(""+ gotPassenger );
etStaffNumber.setText("" + gotStaffNumber);
}
Upvotes: 5
Reputation: 24012
Instead of:
Bundle gotBasket = getIntent().getExtras();
better use this:
if(getIntent().hasExtras("passenger")){
//get Extras here
}
That way, you wont get NPE as you only try to get the Bundle Extras only if they were passed
Upvotes: 2