0
public class DisplayInformation extends MainActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.second_screen);

        confirmCode = (Button)findViewById(R.id.SmsCode); // Why is this null?
        confirmCode.setOnClickListener(this); // NullPointerException here
    } 
}

The logcat:

Process: com.example.user.callarocket, PID: 2096
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.user.callarocket/com.example.user.callarocket.DisplayInformation}: java.lang.NullPointerException
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
            at android.app.ActivityThread.access$800(ActivityThread.java:135)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5017)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
            at com.example.user.callarocket.DisplayInformation.onCreate(DisplayInformation.java:55)
            at android.app.Activity.performCreate(Activity.java:5231)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
            at android.app.ActivityThread.access$800(ActivityThread.java:135)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5017)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
            at dalvik.system.NativeStart.main(Native Method)

I was trying to run this piece of code which connects to a PHP script and i got this error:

Caused by: java.lang.NullPointerException.

I couldn't find any mistakes here as I have tried the same piece of code in another project and it works.

Fernando Carvalhosa
  • 1,098
  • 1
  • 15
  • 23
sof123
  • 3
  • 3
  • 7
    can you post the full stack trace? – ashosborne1 Mar 03 '15 at 13:02
  • and mark the line whose number is reported in the stack trace. – 18446744073709551615 Mar 03 '15 at 13:05
  • @ashosborne1 logcat there – sof123 Mar 03 '15 at 13:05
  • i do not know exactly but `getIntent()` should only work on UI Thread, so pass your values to the Asynctask using its params – A.S. Mar 03 '15 at 13:05
  • Which line is 55 in your code? at com.example.user.callarocket.DisplayInformation.onCreate(DisplayInformation.java:55) – Fernando Carvalhosa Mar 03 '15 at 13:06
  • @FernandoCarvalhosa new CreateUser().execute(); – sof123 Mar 03 '15 at 13:08
  • 1
    It reads `DisplayInformation.onCreate(DisplayInformation.java:55)` so line 55 cannot be `new CreateUser().execute();` – 18446744073709551615 Mar 03 '15 at 13:09
  • im sorry. its this confirmCode.setOnClickListener(this); @18446744073709551615 – sof123 Mar 03 '15 at 13:10
  • `new CreateUser().execute();` is in the `DisplayInformation.onCreate()` method. Check that line again, please – Fernando Carvalhosa Mar 03 '15 at 13:10
  • 1
    it looks like one of your _findViewById()_ returns `null` – 18446744073709551615 Mar 03 '15 at 13:11
  • 1
    `(Button) findViewById(R.id.SmsCode);` is returning `null`. Check if that id `SmsCode` really exists in your xml and then try cleaning/rebuilding your project (android messes up with your binaries really often) – Fernando Carvalhosa Mar 03 '15 at 13:11
  • 1
    @FernandoCarvalhosa thanks man. I messed up the id – sof123 Mar 03 '15 at 13:15
  • No problem. I'm wondering why @JonSkeet closed your question. Although you problem is very specific, tt has nothing to do with the duplicate link he posted. – Fernando Carvalhosa Mar 03 '15 at 13:18
  • @FernandoCarvalhosa: In what way is it nothing to do with that? It's a NullPointerException, which you should understand and then fix in the same way - work out what's null, work out why it's null, and then fix it. There's no indication in the question that the OP had taken those basic steps. A question of "This method call is returning null and I don't know why" would be very different - there's no point in going into the details before the OP has performed basic diagnosis. – Jon Skeet Mar 03 '15 at 13:21
  • @JonSkeet I agree on that, it's a malformed question that leads to a duplicate of a normal `NullPointerException`. But the actual problem is happening in between parsing the xml to his java code. I do believe it's a duplicate of ANY `Android findViewById() NPE` question we can find, but not a generic NPE. Don't you think it would be more informative to flag a proper duplicate? – Fernando Carvalhosa Mar 03 '15 at 13:26
  • @FernandoCarvalhosa: No, I don't. If the OP didn't know enough about NPEs to try to work out what was null, the linked question is more useful to them. If they *did* know enough to do that, then they should have performed that diagnosis themselves. When I closed the question, it didn't even have a stack trace - it wasn't worth my time (or that of other members) to go through the code and try to guess where it was actually failing. If the question had been "findValueById is returning null, why?" then it would have been a good duplicate for an Android-specific question. – Jon Skeet Mar 03 '15 at 13:31
  • @JonSkeet: Agreed. Thanks for clarifying that. He should have read that duplicate, ran another diagnosis and then posted another question with that issue we detected for him (and with the stacktrace PLEASE) – Fernando Carvalhosa Mar 03 '15 at 13:35
  • 1
    @FernandoCarvalhosa: Or edited the question to be more sensible, then posted a comment - at which point the question could have been reopened. No need for a new question, really. – Jon Skeet Mar 03 '15 at 13:36

0 Answers0