Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Getting a string from an intent , getting null object reference error?

 
Joshua Harris
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

So I'm simply trying to pass a string to another activity using intent.putExtra(name, value); but for some reason the activity receiving the intent is saying it's a null reference. I have no idea why, because I've basically just copied the exact same code from a different activity which is performing the same task correctly.

Here is my code:





and here is the code for where the intent is received:



here is the error msg: Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Intent.getStringExtra(java.lang.String)' on a null object reference
at com.example.testing_load.TierTwoListView.<init>(TierTwoListView.java:33)

(java line 33 is line 8 in the code snippet i provided)

as I said earlier I am using basically the exact same code to perform this task in other activities and it's working fine. It's probably a very simple and stupid mistake but I'd be grateful if someone could point out why the error is occurring.
 
Maneesh Godbole
Saloon Keeper
Posts: 11331
16
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The first step to debug this would be to confirm the value being set is actually not null. Have you tried checking what is the value at this point?

 
Joshua Harris
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maneesh Godbole wrote:The first step to debug this would be to confirm the value being set is actually not null. Have you tried checking what is the value at this point?



I just edited the code to give me log tags, here they are:

09-08 12:09:55.196 1362-1362/com.example.testing_load D/LOG: class com.example.testing_load.TierOneListView﹕ VALUE OF name IS: ENGINE
09-08 12:09:55.196 1362-1362/com.example.testing_load D/LOG: class com.example.testing_load.TierOneListView﹕ VALUE OF id IS: 3

so the id string is definitely not null.
 
Maneesh Godbole
Saloon Keeper
Posts: 11331
16
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Intent.getStringExtra(java.lang.String)' on a null object reference

Check the getIntent() for null
 
Joshua Harris
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maneesh Godbole wrote:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Intent.getStringExtra(java.lang.String)' on a null object reference

Check the getIntent() for null


Oh right, what do you mean? Sorry I'm a very beginner at android. How do I check getIntent for null?
 
Joshua Harris
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maneesh Godbole wrote:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Intent.getStringExtra(java.lang.String)' on a null object reference

Check the getIntent() for null


Well I just added this code instead:



and it seems to be working now... I don't understand why?
 
Joshua Harris
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maneesh Godbole wrote:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Intent.getStringExtra(java.lang.String)' on a null object reference

Check the getIntent() for null


I realise my mistake now. I was calling the getIntent() method outside of the onCreate method.

As soon as I moved the code calling getIntent() inside the onCreate method it's all working fine. Stupid mistake lol.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic