0

I've been following the Android Studio training tutorial on the official Android Dev site to create a simple "messaging" app (link to tutorial). I reached the end of "Building my first app". When I click the "send button" the app crashes, the contents of the message are irrelevant. I've tried it on my phone and the emulator. Here's the error code:

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.john.myfirstapp, PID: 3180
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.john.myfirstapp/com.john.myfirstapp.DisplayMessageActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Intent.getStringExtra(java.lang.String)' on a null object reference
   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2327)
   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 'java.lang.String android.content.Intent.getStringExtra(java.lang.String)' on a null object reference
   at com.john.myfirstapp.DisplayMessageActivity.<init>(DisplayMessageActivity.java:16)
   at java.lang.Class.newInstance(Native Method)
   at android.app.Instrumentation.newActivity(Instrumentation.java:1067)
   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2317)
   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)

Android Studio also gave me a suggestion on this line:

getSupportActionBar().setDisplayHomeAsUpEnabled(true);

It states:

Method invocation 'getSupportActionBar().setDisplayHomeAsUpEnabled(true)' may produce 'java.lang.NullPointerException'

I'm quite sure that it isn't some small mistake like a curly or missing quote.

Any help is appreciated :)

guipivoto
  • 18,327
  • 9
  • 60
  • 75
  • 1
    Crash is happening here: DisplayMessageActivity.java Line 16... share DisplayMessageActivity.java source code – guipivoto Jul 24 '16 at 03:27

3 Answers3

0

According to Example that you have used, you must do:

Intent intent = getIntent();
String message = intent.getStringExtra(MyActivity.EXTRA_MESSAGE);

So, maybe, you did not started the Activity properly and this intent is null... So, try to add a null pointer checking:

Intent intent = getIntent();
String message = "";
if(intent != null)
    message = intent.getStringExtra(MyActivity.EXTRA_MESSAGE);

Also, update your question with DisplayMessageActivity and MainActivity code.. Then, I can help you to fix this issue properly.

guipivoto
  • 18,327
  • 9
  • 60
  • 75
  • After finding some help from another source (and failing) I just completely rewrote the code. It works now, but thanks for the suggestions anyway!! – John Matthews Jul 25 '16 at 23:00
0

Look at the below log:

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.john.myfirstapp.DisplayMessageActivity.<init>(DisplayMessageActivity.java:16)

That mean you are trying to call function getStringExtra from a null object at line 16 in file DisplayMessageActivity.java. You should check to avoid NPE in that line.

Kingfisher Phuoc
  • 8,052
  • 9
  • 46
  • 86
0

Null pointer exception is thrown when an application attempts to use null in a case where an object is required. These include:

  1. Calling the instance method of a null object.
  2. Accessing or modifying the field of a null object.
  3. Taking the length of null as if it were an array.
  4. Accessing or modifying the slots of null as if it were an array.
  5. Throwing null as if it were a Throwable value.

Applications should throw instances of this class to indicate other illegal uses of the null object.

You can find more on this link. Click Here

Abhishek T.
  • 1,133
  • 1
  • 17
  • 33