null pointer exception when starting new activity

13,211

Solution 1

This is your clue, from the stack trace:

12-28 03:47:21.670: ERROR/AndroidRuntime(862): Caused by: java.lang.NullPointerException
12-28 03:47:21.670: ERROR/AndroidRuntime(862): at com.acithium.rss.ShowDescription.onCreate(ShowDescription.java:47)

What's line 47 in ShowDescription.java?

Solution 2

If I understood the code right, you should invoke the findViewById() of the showdescription view that you set for the activity and not of the activity, because both, button and textview, seem to be part of that view:

super.onCreate(icicle);
View showdescription = this.findViewById(R.id.showdescription);
setContentView(showdescription);

// ..

TextView db = (TextView)showdescription.findViewById(R.id.storybox);
db.setText(theStory);

Button backbutton = (Button)showdescription.findViewById(R.id.back);
backbutton.setOnClickListener(new Button.OnClickListener() 
{
    public void onClick(View v) 
    {
        finish();
    }
});
Share:
13,211
acithium
Author by

acithium

Updated on June 04, 2022

Comments

  • acithium
    acithium almost 2 years

    Okay, I'm getting a null pointer exception when I start my third activity. Here is the LogCat message:

    12-28 04:38:00.350: ERROR/AndroidRuntime(776): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.acithium.main/com.acithium.rss.ShowDescription}: java.lang.NullPointerException
    12-28 04:38:00.350: ERROR/AndroidRuntime(776):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2401)
    12-28 04:38:00.350: ERROR/AndroidRuntime(776):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2417)
    12-28 04:38:00.350: ERROR/AndroidRuntime(776):     at android.app.ActivityThread.access$2100(ActivityThread.java:116)
    12-28 04:38:00.350: ERROR/AndroidRuntime(776):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794)
    12-28 04:38:00.350: ERROR/AndroidRuntime(776):     at android.os.Handler.dispatchMessage(Handler.java:99)
    12-28 04:38:00.350: ERROR/AndroidRuntime(776):     at android.os.Looper.loop(Looper.java:123)
    12-28 04:38:00.350: ERROR/AndroidRuntime(776):     at android.app.ActivityThread.main(ActivityThread.java:4203)
    12-28 04:38:00.350: ERROR/AndroidRuntime(776):     at java.lang.reflect.Method.invokeNative(Native Method)
    12-28 04:38:00.350: ERROR/AndroidRuntime(776):     at java.lang.reflect.Method.invoke(Method.java:521)
    12-28 04:38:00.350: ERROR/AndroidRuntime(776):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
    12-28 04:38:00.350: ERROR/AndroidRuntime(776):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:549)
    12-28 04:38:00.350: ERROR/AndroidRuntime(776):     at dalvik.system.NativeStart.main(Native Method)
    12-28 04:38:00.350: ERROR/AndroidRuntime(776): Caused by: java.lang.NullPointerException
    12-28 04:38:00.350: ERROR/AndroidRuntime(776):     at com.acithium.rss.ShowDescription.onCreate(ShowDescription.java:48)
    12-28 04:38:00.350: ERROR/AndroidRuntime(776):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123)
    12-28 04:38:00.350: ERROR/AndroidRuntime(776):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2364)
    12-28 04:38:00.350: ERROR/AndroidRuntime(776):     ... 11 more
    

    Here is the section of code where I call the activity:

    public void onItemClick(AdapterView parent, View v, int position, long id)
     {
         Log.i(tag,"item clicked! [" + feed.getItem(position).getTitle() + "]");
    
         Intent itemintent = new Intent(this,com.acithium.rss.ShowDescription.class);
         //Intent itemintent = new Intent();
         //itemintent.setClassName("com.acithium.main", "com.acithium.rss.ShowDescription");
         Bundle b = new Bundle();
         b.putString("title", feed.getItem(position).getTitle());
         b.putString("description", feed.getItem(position).getDescription());
         b.putString("link", feed.getItem(position).getLink());
         itemintent.putExtra("android.intent.extra.INTENT", b);
    
         startActivityForResult(itemintent,0);
     }
    

    And here is new activity class that is called:

    public class ShowDescription extends Activity 
    {
    
        public void onCreate(Bundle icicle) 
        {
            super.onCreate(icicle);
            setContentView(R.layout.showdescription);
    
            String theStory = null;
    
    
            Intent startingIntent = getIntent();
    
    
            if (startingIntent != null)
            {
                Bundle b = startingIntent.getBundleExtra("android.intent.extra.INTENT");
                if (b == null)
                {
                    theStory = "bad bundle?";
                }
                else
                {
                    theStory =  b.getString("title") + "\n\n" + b.getString("description") + "\n\nMore information:\n" + b.getString("link");
                }
            }
            else
            {
                theStory = "Information Not Found.";
    
            }
            TextView db= (TextView) findViewById(R.id.storybox);
            db.setText(theStory);
    
            Button backbutton = (Button) findViewById(R.id.back);
    
            backbutton.setOnClickListener(new Button.OnClickListener() 
            {
                public void onClick(View v) 
                {
                    finish();
                }
            });        
        }
    }
    

    here is the showdescription.xml file:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        >
        <ScrollView android:layout_width="fill_parent"
                android:layout_height="fill_parent" >
                <LinearLayout 
                    android:orientation="vertical"
                    android:layout_width="fill_parent" 
                    android:layout_height="fill_parent">
            <TextView  
                 android:layout_width="fill_parent" 
                     android:layout_height="wrap_content" 
                     android:autoLink="all"
                     android:text="story goes here ...."
                 android:id="@+id/storybox" />
            <Button
                    android:layout_height="wrap_content" 
                    android:text="Back"
                android:id="@+id/back" 
                android:layout_width="100px" 
                android:layout_marginLeft="100px"/>   
            </LinearLayout>
        </ScrollView>
    </LinearLayout>
    
    • acithium
      acithium over 13 years
      Well, I found a workaround. Initially I had these classes in a separate package, but I moved all of them to the same package and deleted the unused R file associated with that package. Once I did that everything started working. I wouldn't call that "solved" as much as I would call that a workaround.
    • Vinay Pai
      Vinay Pai over 13 years
      Ah! That explains it! If it's in a different package, the "R" you refer to ends up being not the R you need! Hmm on second thoughts that may not be exactly right... that would have failed to compile.
    • acithium
      acithium over 13 years
      Yeah, I don't get it either. When they were in 2 second packages, I added the import statement for the R file, but I was still getting the null pointer. I guess I'll just leave it this way for now. Thanks
  • acithium
    acithium over 13 years
    //line 47 is the blank line between these 2 lines. db.setText(theStory); //blank line Button backbutton = (Button) findViewById(R.id.back);
  • Vinay Pai
    Vinay Pai over 13 years
    acithium: That doesn't make any sense. You must have edited the source after you got that error. Try rebuilding and running it again so we can accurately tell what line it was on.
  • acithium
    acithium over 13 years
    okay, now it says db.setText(theStory); is the line that is causing the problems.
  • acithium
    acithium over 13 years
    i posted the new logcat, which shows it to be on line 48 now. That line of code is: db.setText(theStory);
  • Vinay Pai
    Vinay Pai over 13 years
    I agree with atk. From looking at the code it doesn't seem like there is any path that can lead to theStory being null at that point, so it must be db. Can you double-check that the TextView has the correct ID (or post your layout/showdescription.xml file)
  • acithium
    acithium over 13 years
    posted, hopefully this will shed some light of the issue.