0

I'm making a simple Android application for a project. I have an activity that show a List View of conversation and when I click on one of the listed conversation I want to show messages in it. For this purpose I used a Parcelable object.

import android.content.Context;
import android.os.Parcel;
import android.os.Parcelable;
import android.widget.Toast;

import java.util.concurrent.ConcurrentHashMap;

public class ParcelableChat implements Parcelable {
private Chat c;


public Chat getChat (){
    return this.c;
}

public ParcelableChat(Chat c){
    this.c = c;
}

public ParcelableChat(Parcel in){
    c = new Chat();
    c.setObject(in.readString());
    c.setMessageManager((MessageManager)in.readParcelable(MessageManager.class.getClassLoader()));

}

public static final Creator<ParcelableChat> CREATOR =
        new ClassLoaderCreator<ParcelableChat>() {

            @Override
            public ParcelableChat createFromParcel(Parcel parcel) {
                return new ParcelableChat(parcel);
            }

            @Override
            public ParcelableChat[] newArray(int i) {
                return new ParcelableChat[i];
            }

            @Override
            public ParcelableChat createFromParcel(Parcel parcel, ClassLoader classLoader) {
                return new ParcelableChat(parcel);
            }
        };

@Override
public int describeContents() {
    return 0;
}

@Override
public void writeToParcel(Parcel parcel, int i) {

}

 }

In the mainActivity I do:

 intent intent = new Intent(this,MessageActivity.class);
 intent.putExtra("chat", new ParcelableChat(item));//Where item is the Chat object from the ListView.  
 startActivity(intent);

In the new Activity (MessageActivity) I do:

 Bundle extras = getIntent().getExtras();
 ParcelableChat parcelableChat= (ParcelableChat)extras.getParcelable("chat");
 Chat chat = parcelableChat.getChat();

and when I try to display chat's last message using :

       Toast.makeText(getApplicationContext(),"Value : "+ chat.getLastMessage(),Toast.LENGTH_SHORT ).show();

LogCat:

        Process: project.ingsw.maboo, PID: 31172
        java.lang.RuntimeException: Unable to start activity ComponentInfo{project.ingsw.maboo/project.ingsw.maboo.MessageActivity}: java.lang.NullPointerException
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2212)
      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2271)
      at android.app.ActivityThread.access$800(ActivityThread.java:144)
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1205)
      at android.os.Handler.dispatchMessage(Handler.java:102)
      at android.os.Looper.loop(Looper.java:136)
      at android.app.ActivityThread.main(ActivityThread.java:5146)
      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:732)
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:566)
      at dalvik.system.NativeStart.main(Native Method)
    Caused by: java.lang.NullPointerException
      at project.ingsw.maboo.Chat.getMessage(Chat.java:36)
      at project.ingsw.maboo.MessageActivity.onCreate(MessageActivity.java:37)
      at android.app.Activity.performCreate(Activity.java:5231)
      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2169)
      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2271)
      at android.app.ActivityThread.access$800(ActivityThread.java:144)
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1205)
      at android.os.Handler.dispatchMessage(Handler.java:102)
      at android.os.Looper.loop(Looper.java:136)
      at android.app.ActivityThread.main(ActivityThread.java:5146)
      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:732)
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:566)
      at dalvik.system.NativeStart.main(Native Method)

I get NullPointerExeption.... what's the problem ?

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
HaZe
  • 45
  • 7

1 Answers1

0

Just Modify your new activity like this:

ParcelableChat parcelableChat= (ParcelableChat)getIntent().getParcelableExtra("chat");
Chat chat = parcelableChat.getChat();

the problem is while starting activity you are attaching tha parcelable to intent but while getting you are reading from extras. so you are getting null there.

Rathan Kumar
  • 2,567
  • 2
  • 17
  • 24