• 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

Error Getting Time From TimePicker Dialog - "Attempt to invoke virtual method...on a null object..."

 
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Howdy, y'all!

Please forgive my incompetence but I seem to be having an issue properly getting hour and minutes from a TimePicker dialog. I am receiving the following error (which I know has a simple fix staring me in the face):

java.lang.NullPointerException: Attempt to invoke virtual method 'int android.widget.TimePicker.getHour()' on a null object reference
                     at j5tech.co.offthehook.AddNewActivity$3.onClick(AddNewActivity.java:125)
                     at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:163)
                     at android.os.Handler.dispatchMessage(Handler.java:102)
                     at android.os.Looper.loop(Looper.java:148)
                     at android.app.ActivityThread.main(ActivityThread.java:5417)
                     at java.lang.reflect.Method.invoke(Native Method)
                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)



Error provided, let me also show you the code I am working with:



I have been staring at and tinkering with this issue for a few hours now to no avail, so I figured it is probably time to ask for help. Thanks!
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There's this line at the top of the stack trace:



It says the exception occurred in a method named "onClick", at line 125 of the source code.

So far you posted only 65 lines, not including an "onClick" method. It looks like you posted the whole method, but on the other hand what you posted isn't what's throwing the exception. You might want to look into what's up with that.
 
Adam Wentz
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:There's this line at the top of the stack trace:



It says the exception occurred in a method named "onClick", at line 125 of the source code.

So far you posted only 65 lines, not including an "onClick" method. It looks like you posted the whole method, but on the other hand what you posted isn't what's throwing the exception. You might want to look into what's up with that.




Paul, thanks for replying!

I only shared relevant code (that related to the error). The "onClick" method the error is referring to is found starting at line 51 here. To be more precise, line 125 in the error message correlates with line 52 in the code I posted. If I can't figure this out in the next hour or so, I am going to scrap much of this code anyhow and tackle time picking another way- using Calendar.
 
Saloon Keeper
Posts: 7585
176
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Check that "timePicker" is not null after line 14.
 
Adam Wentz
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Moores wrote:Check that "timePicker" is not null after line 14.



Tim, using a simple if/else statement and a Toast, I was able to deduce that the timePicker IS, in fact, null.
I am a bit perplexed by that fact, though, because when it appears, it displays the current device time.

Why is it null?
 
Adam Wentz
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Alright, I got this issue squared away...

I was trying to work with an object (TimePicker) that's actually in a separate view.

I inflate the view here...



However, I was neglecting to appropriately refer to it while grabbing the TimePicker...



Hence, the null object reference...since it couldn't be found in the parent view. So, to fix it, I simply needed to tweak the line as such...



And voila!
 
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for posting the solution.
 
Get me the mayor's office! I need to tell him about this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic