• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic