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

Aggravating Nonsense, as my app shuts down on my VM and my phone (App won't start)

 
Naziru Gelajo
Ranch Hand
Posts: 127
1
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I get this error message:

/com.gelajo.africancurrencyconverter E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.gelajo.africancurrencyconverter, PID: 2249
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.gelajo.africancurrencyconverter/com.gelajo.africancurrencyconverter.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
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)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at com.gelajo.africancurrencyconverter.MainActivity.onCreate(MainActivity.java:35)
at android.app.Activity.performCreate(Activity.java:6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
at android.app.ActivityThread.-wrap11(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
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) 
01-18 20:09:22.272 2249-2249/? I/Process: Sending signal. PID: 2249 SIG: 9

The app compiled just fine, however the app failed to open on both my VM and on my actual Nexus 5. I'm completely new to Android Development and Java, so please bare with me. I do plan on getting much better. Thanks!
 
Paul Clapham
Sheriff
Posts: 21581
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is just an ordinary NullPointerReference exception. Remember, just because your code compiles correctly that doesn't mean it's going to work correctly. Compiling is only the first step. The majority of your time is going to be spent finding and fixing problems in code which compiles correctly.

The stack trace tells you which line of code is the problem:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at com.gelajo.africancurrencyconverter.MainActivity.onCreate(MainActivity.java:35)


It tells you that line 35 of your MainActivity class throws the exception, and it also tells you that it's in the onCreate() method. So look at that line of code and find out why you're calling a method (or something like that) on a variable which hasn't had anything assigned to it yet.

If you can't tell, then post the code here and ask about it.
 
Naziru Gelajo
Ranch Hand
Posts: 127
1
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:This is just an ordinary NullPointerReference exception. Remember, just because your code compiles correctly that doesn't mean it's going to work correctly. Compiling is only the first step. The majority of your time is going to be spent finding and fixing problems in code which compiles correctly.

The stack trace tells you which line of code is the problem:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at com.gelajo.africancurrencyconverter.MainActivity.onCreate(MainActivity.java:35)


It tells you that line 35 of your MainActivity class throws the exception, and it also tells you that it's in the onCreate() method. So look at that line of code and find out why you're calling a method (or something like that) on a variable which hasn't had anything assigned to it yet.

If you can't tell, then post the code here and ask about it.


Hey Paul, thanks for responding. The NullPointerReference exception at line 35 is a setOnClickListener method for another method that I wrote (that method is void). Shouldn't it still work? Just started Java 2 weeks ago lol.
 
Naziru Gelajo
Ranch Hand
Posts: 127
1
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is the method that is called on the setOnClickListener(new View.OnClickListener() method:

 
Paul Clapham
Sheriff
Posts: 21581
33
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Naziru Gelajo wrote:This is the method that is called on the setOnClickListener(new View.OnClickListener() method:


As I said, the problem is with line 35, in the onCreate() method. There's no point yet in looking at other methods.

Shouldn't it still work?


No... the first thing you should learn is that you are very likely to write code which doesn't work. That's especially true when you're a beginner but experts aren't immune to writing code which doesn't work either.
 
Naziru Gelajo
Ranch Hand
Posts: 127
1
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:
Naziru Gelajo wrote:This is the method that is called on the setOnClickListener(new View.OnClickListener() method:


As I said, the problem is with line 35, in the onCreate() method. There's no point yet in looking at other methods.

Shouldn't it still work?


No... the first thing you should learn is that you are very likely to write code which doesn't work. That's especially true when you're a beginner but experts aren't immune to writing code which doesn't work either.


Got it, thanks Paul I'll see what I can do to fix the error.
 
Maneesh Godbole
Saloon Keeper
Posts: 11341
16
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I suppose you are using the layout xml.
Looks like you have forgotten to hook it up with your code (Hint: findViewById)

On a related note, you can do the coupling in the xml itself! Check out androidnClick
However, instead of applying an OnClickListener to the button in your activity, you can assign a method to your button in the XML layout, using the androidnClick attribute. For example:

More about it here
http://developer.android.com/reference/android/widget/Button.html
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic