Win a copy of Java Persistence with Spring Data and Hibernate this week in the Spring forum!
  • 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
  • Ron McLeod
  • Tim Cooke
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Jeanne Boyarsky
Saloon Keepers:
  • Stephan van Hulst
  • Carey Brown
  • Tim Holloway
  • Piet Souris
Bartenders:

Android: activity crashes when called.

 
Ranch Hand
Posts: 62
2
Mac Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

I am making a Rock-Paper-Scissors game.  When I call the activity with no code, it calls ok.  But when I add this bit of code, it crashes straight away:


Can anyone see any problems, or have any advice on how to sort this.  Thank you
 
Bod MacNeil
Ranch Hand
Posts: 62
2
Mac Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I should add that that it not all of the code.  I commented out most of it as a test and it still didn't work.  It complies without any errors also.  
 
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What error is there in the logs?
There should be a stacktrace and exception.
 
Bod MacNeil
Ranch Hand
Posts: 62
2
Mac Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


12-14 10:02:54.690 21533-21533/macson.fifteendaysofjava E/AndroidRuntime: FATAL EXCEPTION: main
                                                                         java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{macson.fifteendaysofjava/macson.fifteendaysofjava.Rock_Paper_Scissor}: java.lang.NullPointerException
                                                                             at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1880)
                                                                             at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
                                                                             at android.app.ActivityThread.access$600(ActivityThread.java:123)
                                                                             at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
                                                                             at android.os.Handler.dispatchMessage(Handler.java:99)
                                                                             at android.os.Looper.loop(Looper.java:137)
                                                                             at android.app.ActivityThread.main(ActivityThread.java:4424)
                                                                             at java.lang.reflect.Method.invokeNative(Native Method)
                                                                             at java.lang.reflect.Method.invoke(Method.java:511)
                                                                             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
                                                                             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
                                                                             at dalvik.system.NativeStart.main(Native Method)
                                                                          Caused by: java.lang.NullPointerException
                                                                             at android.app.Activity.findViewById(Activity.java:1794)
                                                                             at macson.fifteendaysofjava.Rock_Paper_Scissor.<init>(Rock_Paper_Scissor.java:19)
                                                                             at java.lang.Class.newInstanceImpl(Native Method)
                                                                             at java.lang.Class.newInstance(Class.java:1319)
                                                                             at android.app.Instrumentation.newActivity(Instrumentation.java:1023)
                                                                             at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1871)
                                                                             at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981) 
                                                                             at android.app.ActivityThread.access$600(ActivityThread.java:123) 
                                                                             at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147) 
                                                                             at android.os.Handler.dispatchMessage(Handler.java:99) 
                                                                             at android.os.Looper.loop(Looper.java:137) 
                                                                             at android.app.ActivityThread.main(ActivityThread.java:4424) 
                                                                             at java.lang.reflect.Method.invokeNative(Native Method) 
                                                                             at java.lang.reflect.Method.invoke(Method.java:511) 
                                                                             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 
                                                                             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 
                                                                             at dalvik.system.NativeStart.main(Native Method) 

 
Dave Tolls
Rancher
Posts: 4801
50
  • 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
at android.app.Activity.findViewById(Activity.java:1794)
at macson.fifteendaysofjava.Rock_Paper_Scissor.<init>(Rock_Paper_Scissor.java:19)

There is an issue with the view you are attempting to get on line 19.
Does it exist on the page associated with this activity?
 
Bod MacNeil
Ranch Hand
Posts: 62
2
Mac Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hmmm that means this line is causing the problem:
I've just looked and there is a rock2 ImageView in my XML:


above is code from the activity_rock__paper__scissor.xml which has been added to my activity

 
Bartender
Posts: 7488
171
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Line 19 seems to be about scissors2, not rock2.
 
Bod MacNeil
Ranch Hand
Posts: 62
2
Mac Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I missed out the package name when I posted the code here so its a little off.  line 19 is for rock2 on my Android Studio.  I was thinking maybe there is a problem which is triggering this which I haven't spotted yet?
 
Bod MacNeil
Ranch Hand
Posts: 62
2
Mac Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've tried commenting out line 19 but when I re-run the program the error moved to line 20 and so on.  Its just going thought my instant variables.  Has it anything to do with where I'm putting them?
 
Bod MacNeil
Ranch Hand
Posts: 62
2
Mac Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The code works ok when I comment out these :


Its weird that it only happens at run time.  Any ideas?
 
Bod MacNeil
Ranch Hand
Posts: 62
2
Mac Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry for the constant posts.  But I fixed the problem by putting them in onCreate().  If anyone could explain why this works that would be good

 
Marshal
Posts: 77541
372
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't know, unless onCreate is some sort of analogue of a constructor. Initialising all fields in the constructor is one way to avoid nulls, so maybe the same happens in onCreate. Otherwise, don't know.
No doubt somebody else will know.
 
Tim Moores
Bartender
Posts: 7488
171
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh, I completely missed that.

You had two variables for each GUI element. The ones defined at class level (rock2 etc.) get instantiated when the object is constructed - when there is no layout, and hence they are null.

The objects instantiated in onCreate (rockButton etc) are accessible only in that method, and hence of little use.

The latest code version is doing it correctly.
 
Dave Tolls
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ha!
Of course.
That's what happens when you don't do this stuff for a while...you miss the forest for the trees...
 
Tongue wrestling. It's not what you think. And here, take this tiny ad. You'll need it.
The Low Tech Laboratory Movie Kickstarter is LIVE NOW!
https://www.kickstarter.com/projects/paulwheaton/low-tech
reply
    Bookmark Topic Watch Topic
  • New Topic