Null pointer Exception Android - android

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

Related

android kotlin - start activity and getting data from intent

I want to simply start an activity and send some data to my second activity , this is my code :
Activiy main:
val intent = Intent(this#MainActivity, Cards::class.java)
intent.putExtra("title", catItem.name)
intent.putExtra("catId", catItem.id)
startActivity(intent)
catItem is not null and every item in it has value , i've debugged and I'm sure about it .
the second activity that I need to get data :
val bl:Bundle=intent.extras
catId=bl.getString("catId")
title=bl.getString("title")
it crashes on the second line :
bl.getString("catId") must not be null
I debugged and the bundle is completely empty .
what is wrong with this code ?
To retrieve data on second activity you just need to access directly intent's extra data as follows:
val catId = intent.getStringExtra("catId")
Also, be sure that "catId" type is String (an ID usually is an Integer or Long), because if it is not an String, you will get the same error.
According to your activity start code(just copy of your code):
val intent = Intent(this#MainActivity, Cards::class.java)
intent.putExtra("title", catItem.name)
intent.putExtra("catId", catItem.id)
startActivity(intent)
In Cards Activity onCreate method:
if they are
Required:
val arguments = requireNotNull(intent?.extras){"There should be parameters or your more meaningful message."}
with(arguments){
catId = getString("catId")
title = getString("title")
}
Not Required:
catId = intent?.extras?.getString("catId").orEmpty()
title= intent?.extras?.getString("title").orEmpty()
or
catId = intent?.extras?.getString("catId","default Cat").orEmpty()
title= intent?.extras?.getString("title","default Title").orEmpty()
Change getExtra to getStringExtra
bl.getStringExtra("catId")
I think in your code catItem.id is null, because of it catId is null, which causes of crash.
Please try below code to overcome the crash:
var bundle :Bundle ?=intent.extras
var title = bundle!!.getString("title")
var catId = bundle!!.getString("catId")
I hope its work for you.

Android: Adding more than one Bundle to a new activity

I'm trying to pass in two separate pieces of information to a new activity in my Android application.
I currently have this:
Bundle dataBundle = new Bundle();
Bundle extras = getIntent().getExtras(); // student id
dataBundle.putInt("id", 0);// lesson id
Intent intent = new Intent(getApplicationContext(),com.example.ltss.dyslexia.app.LessonNotes.class);
intent.putExtras(dataBundle);
intent.putExtras(extras);
startActivity(intent);
I then have the code accessing this information. However, adding the second bundle overrides the first one.
Bundle extras = getIntent().getExtras();
Bundle studentId = getIntent().getExtras();
Log.d("LessonID: ", String.valueOf(extras));
Log.d("StudentID: ", String.valueOf(studentId));
I need to have the information passed in separately as I need to check if one of them is null.
Can what i'm asking be done? Any ideas as to how to do this? Or another way to do this? (parsing maybe?)
Thanks
you could use putExtra("bundle1", bundle1) and putExtra("bundle2", bundle2) and then use getIntent().getBundleExtra("bundle1"); and getIntent().getBundleExtra("bundle2"); to retrieve both
Maybe you're over thinking this. You can put a ton of information in 1 bundle.
Bundle bundle = new Bundle();
bundle.putString("studentid", "Student0983");
bundle.putInt("lessonid", 0);
bundle.putString("moreinfo", "needed some extra data on that student");
bundle.putInt("studentincome", 4250);
Intent intent = new Intent(getApplicationContext(),com.example.ltss.dyslexia.app.LessonNotes.class);
intent.putExtras(bundle);
startActivity(intent);
Now to get that data in the new activity
Bundle bundle = getIntent().getExtras();
Log.d("studentid: ", bundle.getString("studentid"));
Log.d("lessonid: ", bundle.getInt("lessonid"));
Log.d("moreinfo: ", bundle.getString("moreinfo"));
Log.d("studentincome: ", bundle.getInt("studentincome"));

Passing data between activities in Android?

I wish to pass the value of score from one activity to another. I addExtras to the intent and getExtras in the new activity but it doesn't seem to get the value.
Activity 1;
Intent intent = new Intent(Game.this, EndGame.class);
intent.putExtra("put_score", score);
startActivity(intent);
Game.this.finish();
Activity 2;
Bundle extras = getIntent().getExtras();
if (extras != null) {
score = extras.getString("put_score");
}
setContentView(R.layout.endgame);
scoreResult = (TextView) findViewById(R.id.scoreNum);
scoreResult.setText(score);
You problem is coming from the following piece of code in Bundle.java:
try {
return (String) o;
} catch (ClassCastException e) {
typeWarning(key, o, "String", e);
return null;
}
Here o is the object you put to the bundle (bundle actually has a core storage of type Map<String, Object>, so, due to autoboxing, when you put int to the bundle, it will become Integer). But, unfortunately, Integer cannot be casted to String explicitly, so you get null instead. In other words: if you put int, then use getInt to retrieve the value.
you placed data in intent using putExtra not putExtras
so read them the same way
use
getXXExtra()
XX is the dataType your data is,
based on the example, if score is Integer, then use:
getIntExtra("put_score", 0);//0 zero is default in case data was not found
http://developer.android.com/reference/android/content/Intent.html

get the value from Intent of android

Thanks in advance.
When I print Log.d("me",getIntent().toString());
I am getting:
Intent { act=android.intent.action.CALL_PRIVILEGED dat=tel:888 flg=0x13800000 cmp=com.ninetology.freecall.two/.CallFinalActivity }
I am trying to fetch the value which is associated with "dat" but I am getting NullPointer exception.
//the code I am using is
getIntent().getStringExtra("dat"); // no use
//i tried
getIntent().getExtras("dat").toString(); // NullPointer exception
I tried with "tel" as key in above code still no use.
it seems you're doing this wrong.
The getExtras() function returns a bundle that you can extract data from and not a function that returns a specific String.
dat is NOT a String value as you can see from the data that was printed. it's a Uri,
try parsing it as you should and I'm sure you'll be able to get the data.
public void onCreate(Bundle b) { //mistyped
super.onCreate(b);
Uri data = getIntent().getData();
// OR USE THIS
String data = getIntent().getDataString();
// DO STUFF
}
try getIntent().getExtras().get("dat");
First of all, Its not necessary the string from Intent your are getting in log have a object with values..
So its better to just check its not a null, like,
Bundle bundle = getIntent().getExtras();
if(bundle ! = null)
{
// Now check you bundle object which has a values or not
}
else
{
// 1. get data in form of Uri
Uri data = getIntent().getData();
// 2. OR get string of Uri
String dataString = getIntent().getDataString();
// 3. Or split the data string
// The logic from this part may be different on your requirement.. I only suggests you to get data from string.. (Actual logic may different on your case)
String data = getIntent().toString();
data = data.subString(data.indexOf(":"), data.indexOf("flg")-1);
Log.e("tel:", data);
}
When you want to pass the data with the intent just add the below code before starting activity
intent.putExtra("dat", value); //value=the value you want to send
And when you want to fetch the same value in another activity just do:
Bundle bundle=getIntent().getExtras();
if(bundle!=null){
String string=bundle.getString("dat");
}
By doing this, you wont get the null pointer exception and will help you.

NullPointerException from getIntent().getExtras().getString("to")

InboxDetailActivity.java:
Intent i = new Intent(InboxDetailActivity.this,Compose.class);
Bundle b = new Bundle();
b.putString("To", ConstantData.inbox_from);
Log.d("From Value", ConstantData.inbox_from);
b.putString("Subject", "RE:" + ConstantData.inbox_subject);
Log.d("Subject Value", ConstantData.inbox_subject);
b.putString("FromId", ConstantData.inbox_fromid);
Log.d("From Id Value",ConstantData.inbox_fromid);
i.putExtras(b);
startActivity(i);
Compose.java:
Intent i = getIntent();
Bundle b = i.getExtras();
to = b.getString("To");
subject = b.getString("Subject");
toId = b.getString("FromId");
I am getting NullPointerException at to = b.getString("To");
Bundle b = i.getExtras();
getExtras() returns null.
Agree with John's answer adding possible solution.
What you are doing is create a bundle , insert values in this and then pass this bundle.
And than you just fetch all the values one by one using its keys.
I am working with bundles but I simply add desired values directly using putExtra method. And I haven't got any problem till date. I recommend you to use put extra and check whether it works or not.
I would like to know what makes you to apply this way for bundles? Have you just read it somewhere and started applying this method ? or you have got some options and after some consideration you found it better to apply this method OR your requirement states that. Because normally me and my peers doesn't use bundles and directly pass the extras. And that works for me every time.
using this instead of bundle
i.putString("To", ConstantData.inbox_from);
Log.d("From Value", ConstantData.inbox_from);
i.putString("Subject", "RE:" + ConstantData.inbox_subject);
Log.d("Subject Value", ConstantData.inbox_subject);
i.putString("FromId", ConstantData.inbox_fromid);
Log.d("From Id Value",ConstantData.inbox_fromid);
and in another class..
to = getIntent().getString("To");

Categories

Resources