7

So thx for any awnsers given :)
the solution to my problem was, that my line

Intent i = getIntent();

was not in my onCreate() function.
Thank you and have a nice day.






i know, this kind of question was asked quite a lot on the internet, but i couldn't find any answer that helped me, so i will bring you here my special case.

This is my function in my MainActivity, where i create the Intent and Start my second activity:

private void showArticle(String entryUrl, String entryTitle) {
    Intent intent = new Intent(this, ArticleActivity.class);
    intent.putExtra(EXTRA_URL, entryUrl.toString());
    intent.putExtra(EXTRA_TITLE, entryTitle.toString());
    Log.d("DEBUG", intent.getStringExtra(EXTRA_URL));
    Log.d("DEBUG", intent.getStringExtra(EXTRA_TITLE));
    Log.d("DEBUG", "EXTRAS PUTTED");
    startActivity(intent);
    Log.d("DEBUG", "ACTIVITY STARTED");
}

This is the console output:

01-02 15:29:21.808 7108-7108/com.example.myfirstapp D/DEBUG: http://example.com/correct-url
01-02 15:29:21.809 7108-7108/com.example.myfirstapp D/DEBUG: Yeah, we have the correct title hhere
01-02 15:29:21.809 7108-7108/com.example.myfirstapp D/DEBUG: EXTRAS PUTTED
01-02 15:29:21.809 7108-7108/com.example.myfirstapp I/Timeline: Timeline: Activity_launch_request id:com.example.myfirstapp time:1404863384
01-02 15:29:21.815 7108-7108/com.example.myfirstapp D/DEBUG: ACTIVITY STARTED



Sooo... 'til here everything works fine. Then, who guessed it, my second activity opens up:

Intent i = getIntent();

...

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_article);
    getActionBar().setDisplayHomeAsUpEnabled(true);
    if (i.getStringExtra(MainActivity.EXTRA_TITLE) == null) {
        setTitle("MEEP");
    } else {
        setTitle(i.getStringExtra(MainActivity.EXTRA_TITLE));
    }

    ...

But now my App crashes with this error message:

01-02 15:29:21.855 7108-7108/com.example.myfirstapp D/AndroidRuntime: Shutting down VM
01-02 15:29:21.870 7108-7108/com.example.myfirstapp E/AndroidRuntime:
        FATAL EXCEPTION: main
        Process: com.example.myfirstapp, PID: 7108
        java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myfirstapp/com.example.myfirstapp.article.ArticleActivity}: 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:2379)
          at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2442)
          at android.app.ActivityThread.access$800(ActivityThread.java:156)
          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1351)
          at android.os.Handler.dispatchMessage(Handler.java:102)
          at android.os.Looper.loop(Looper.java:211)
          at android.app.ActivityThread.main(ActivityThread.java:5373)
          at java.lang.reflect.Method.invoke(Native Method)
          at java.lang.reflect.Method.invoke(Method.java:372)
          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1020)
          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:815)
        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.example.myfirstapp.article.ArticleActivity.onCreate(ArticleActivity.java:44)
          at android.app.Activity.performCreate(Activity.java:5990)
          at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
          at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2332)
          at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2442) 
          at android.app.ActivityThread.access$800(ActivityThread.java:156) 
          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1351) 
          at android.os.Handler.dispatchMessage(Handler.java:102) 
          at android.os.Looper.loop(Looper.java:211) 
          at android.app.ActivityThread.main(ActivityThread.java:5373) 
          at java.lang.reflect.Method.invoke(Native Method) 
          at java.lang.reflect.Method.invoke(Method.java:372) 
          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1020) 
          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:815)




Now my question is: why do i get a null pointer? i mean, the log clearly shows that the extra is put in the intent, why doesn't my second activity receive the key values? Can anyone help?
And thx for every tip.

for any more information, just write a comment :)

2

4 Answers 4

5

what is your "i" in second activity. Intent? If this intent is what you expect from main activity, then you should put this code in onNewIntent() if your activity is with SIngleTop flag, and this onNewIntent() should be overrriden.

in onCreate(), to get this intent you should do something like this

Intent i = getIntent()
0
2

You are calling the method getStringExtra() on a null Object. To Access the Strings that you added to your intent use the getIntent() function.

Ex

Bundle bundle = getIntent().getExtras();
if (bundle.getString(MainActivity.EXTRA_TITLE) == null) {
    setTitle("MEEP");
} else {
    setTitle(bundle.getString(MainActivity.EXTRA_TITLE));
}
0

You are getting NullPointerException saying java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Intent.getStringExtra(java.lang.String)' on a null object reference So, you have to check your parameter which you have passed via intent as below code:

if (getIntent().getStringExtra(MainActivity.EXTRA_TITLE) == null) {
        setTitle("MEEP");
 } else {
        setTitle(getIntent().getStringExtra(MainActivity.EXTRA_TITLE));
 }
-1

I just had this problem tonight. My Intent didn't have an onResume() calling openOrCreateDatabase paragraph which opens the database and defines the fields.

2
  • As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
    – Parthi
    Nov 9, 2021 at 7:45
  • This does not really answer the question. If you have a different question, you can ask it by clicking Ask Question. To get notified when this question gets new answers, you can follow this question. Once you have enough reputation, you can also add a bounty to draw more attention to this question. - From Review Nov 9, 2021 at 14:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.