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);
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);
}
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.