0

I'm passing an object from activity to another. Then from that activity to another one. When I try to access an attribute of the passed object from the 3rd activity, i get a null pointer exception.

I've set the attribute also. So i don't understand the reason for it.

Code from the first activity:

private Task t;

public void onClick(View v) {

    if(v == this.btnAdd){
        this.addItem(this.txtTask.getText().toString());
        this.addTask(this.txtTask.getText().toString());
    }

}

private void addTask(String taskName){
    if(taskName.length()>0){
        t = new Task(taskName);

        this.tasks.add(t);
    }
}

 public void onItemClick(AdapterView<?> parent, View view, int position,
                long id3) {


        Intent myIntent = new Intent(getApplicationContext(), TabSwitch.class);
        myIntent.putExtra("taskItem", t);
            startActivity(myIntent);
        }

    });

From the second activity:

private TextView selectedTask;

Intent i=getIntent();

    Task taskItem = (Task) i.getSerializableExtra("taskItem");


    i = new Intent().setClass(this, Info.class);
    i.putExtra("taskItem", taskItem);

From the 3rd activity:

Intent i=getIntent();
Task task =(Task) i.getSerializableExtra("taskItem");

selectedTask.setText(task.getTaskName());

Here is the log:

02-13 22:03:02.868: E/AndroidRuntime(572): FATAL EXCEPTION: main
02-13 22:03:02.868: E/AndroidRuntime(572): java.lang.RuntimeException: Unable to start activity ComponentInfo{sam.todo.OnTime/sam.todo.OnTime.TabSwitch}: java.lang.RuntimeException: Unable to start activity ComponentInfo{sam.todo.OnTime/sam.todo.OnTime.Info}: java.lang.NullPointerException
02-13 22:03:02.868: E/AndroidRuntime(572):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
02-13 22:03:02.868: E/AndroidRuntime(572):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
02-13 22:03:02.868: E/AndroidRuntime(572):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
02-13 22:03:02.868: E/AndroidRuntime(572):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
02-13 22:03:02.868: E/AndroidRuntime(572):  at android.os.Handler.dispatchMessage(Handler.java:99)
02-13 22:03:02.868: E/AndroidRuntime(572):  at android.os.Looper.loop(Looper.java:123)
02-13 22:03:02.868: E/AndroidRuntime(572):  at android.app.ActivityThread.main(ActivityThread.java:3683)
02-13 22:03:02.868: E/AndroidRuntime(572):  at java.lang.reflect.Method.invokeNative(Native Method)
02-13 22:03:02.868: E/AndroidRuntime(572):  at java.lang.reflect.Method.invoke(Method.java:507)
02-13 22:03:02.868: E/AndroidRuntime(572):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-13 22:03:02.868: E/AndroidRuntime(572):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-13 22:03:02.868: E/AndroidRuntime(572):  at dalvik.system.NativeStart.main(Native Method)
02-13 22:03:02.868: E/AndroidRuntime(572): Caused by: java.lang.RuntimeException: Unable to start activity ComponentInfo{sam.todo.OnTime/sam.todo.OnTime.Info}: java.lang.NullPointerException
02-13 22:03:02.868: E/AndroidRuntime(572):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
02-13 22:03:02.868: E/AndroidRuntime(572):  at android.app.ActivityThread.startActivityNow(ActivityThread.java:1487)
02-13 22:03:02.868: E/AndroidRuntime(572):  at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:127)
02-13 22:03:02.868: E/AndroidRuntime(572):  at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:339)
02-13 22:03:02.868: E/AndroidRuntime(572):  at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:654)
02-13 22:03:02.868: E/AndroidRuntime(572):  at android.widget.TabHost.setCurrentTab(TabHost.java:326)
02-13 22:03:02.868: E/AndroidRuntime(572):  at android.widget.TabHost.addTab(TabHost.java:216)
02-13 22:03:02.868: E/AndroidRuntime(572):  at sam.todo.OnTime.TabSwitch.onCreate(TabSwitch.java:30)
02-13 22:03:02.868: E/AndroidRuntime(572):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-13 22:03:02.868: E/AndroidRuntime(572):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
02-13 22:03:02.868: E/AndroidRuntime(572):  ... 11 more
02-13 22:03:02.868: E/AndroidRuntime(572): Caused by: java.lang.NullPointerException
02-13 22:03:02.868: E/AndroidRuntime(572):  at sam.todo.OnTime.Info.onCreate(Info.java:34)
02-13 22:03:02.868: E/AndroidRuntime(572):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-13 22:03:02.868: E/AndroidRuntime(572):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
02-13 22:03:02.868: E/AndroidRuntime(572):  ... 20 more
sam
  • 5
  • 1
  • 6
  • 1
    In your first Activity, you assign task in the addTask() method. But it doesn't look like you ever actually call addTask(). That could be your problem – ahodder Feb 13 '12 at 16:55
  • I have called it, I jst missed to post it here. I edited it now at the top. – sam Feb 13 '12 at 16:59

3 Answers3

0

Is the null pointer exception being thrown because the object t is null?! Looks that way. Make sure you're creating t somewhere.

Ken
  • 30,811
  • 34
  • 116
  • 155
0

If all these activities are under the same Application, I would recommend you simply extend the Application class with your own custom application class that stores this object that is persistent through out the application (unless it is interrupted and then closed, read Arhimed's comment below). This is a much more robust solution as it is resilient to activity life cycles.

Here is a link that should help with implementing this: https://stackoverflow.com/a/708317/220710

Community
  • 1
  • 1
Emil Davtyan
  • 13,808
  • 5
  • 44
  • 66
  • 1
    @Emil: Please avoid teaching that `Application` class is "persistent threw out the application". Details are here: http://stackoverflow.com/a/4642069/247013 and here: http://developer.android.com/intl/zh-CN/guide/topics/fundamentals/processes-and-threads.html#Lifecycle – Vit Khudenko Feb 13 '12 at 17:57
  • Thanks, I didn't know that it recreates the activity stack after the application comes back. – Emil Davtyan Feb 13 '12 at 18:08
0

Does your Activity maintain its life cycle properly? Looks like not. If you leave, say, activity A in favor of activity B (so the stack of activities is A > B), then once activity A becomes invisible the OS may decide to kill it (there are hacks to prevent it, but those are hacks, so you better avoid them). So when you come back to the activity A from B, the OS will restore the activity A for you (however it's you who is responsible for proper activity state maintaining - the Activity API has special callbacks for that - persist/restore your tasks at those points). Here is the official tutorial on this: Managing the Activity Lifecycle.

Vit Khudenko
  • 28,288
  • 10
  • 63
  • 91
  • I read the link u gave, but I do not understand what u meant by Activity doesn't seem to be maintaining life cycle properly. Please help me understand. Also, Can I user 'sharedPreferences' for this purpose? I read about it too bt I'm a bit confused. What I need to do in this application is, in one instance of the application I create a 'task' object, and pass that object from the activity which created it to another activity. The later activity needs to retrieve attributes for the object and store additional attributes to it. It would be great if u can gv some idea about it. – sam Feb 14 '12 at 13:00
  • @sam: Please take under account that I don't see your code, so when I tell "your activity doesn't seem to be maintaining life cycle properly" I actually gave a guess. You got a NPE and it looks for me like the reason is activity state being not saved/restored is the reason. However I can be totally wrong, because actually I don't see where exactly you get the NPE within your code. – Vit Khudenko Feb 14 '12 at 13:58
  • @sam: As to `SharedPreferences` - yes you can use it to persist activity state, but it is an overkill, because data saved in `SharedPreferences` will live longer than activity. It's better to use life cycle callbacks from `Activity` API for that, i.e. `onSaveInstanceState()/onRestoreInstanceState()`. – Vit Khudenko Feb 14 '12 at 13:59
  • Thnks. I understand that, jst wanted to make it clear for myself. :) I read about onSaveInstanceState()/onRestoreInstanceState(). Just to clarify, can it be used to save an object or just primitives? – sam Feb 14 '12 at 14:07
  • @sam: it should allow to store anything you want (I don't remember what are the restrictions exactly, check the API), but I can assure I had no problems with storing objects (versus primitives). – Vit Khudenko Feb 14 '12 at 16:12
  • I have used onSaveInstanceState() but still the same error occurs. Application works fine if i use a string in place of the object and do the same thing. I have posted another question with the new code. Please help me to find the issue. [link](http://stackoverflow.com/questions/9294675/null-pointer-exception-while-trying-to-access-a-saved-object) – sam Feb 15 '12 at 13:58
  • @sam: The link is broken (at least SO says "Page Not Found"). – Vit Khudenko Feb 15 '12 at 18:59
  • @sam: Looks like my answer has not solved your issue. So why have you marked it as a solution? :) If you think the issue is not related with a proper Activity life cycle maintaining, then please remove the solution marker from the answer. Once you finally get the issue raeason and it's not related to proper Activity life cycle maintaining feel free to post your solution and accept it as such. – Vit Khudenko Feb 15 '12 at 19:05