1

So I have been looking through StackOverFlow and can't seem to find a topic that completely resolves my problem. Many people have had problems with Null Pointer Exceptions while trying to use custom parcelables. I've seen problems resolved with solutions as simple as adding Static to CREATOR in the parcelable class. However, I have not found anything to help my cause or shed some enlightenment on my situation.

I'm getting a Null Pointer Exception when accessing an ArrayList of coordinates from a custom class called MyPointsList. Before passing MyPointsList I am able to access the ArrayList inside MyPointsList. Upon retrieving the instance I do a quick check to make sure the instance is not null, it's not, then proceed to access the ArrayList where I get the Null Pointer Exception.

Why am I receiving a null pointer exception in the receiving class when accessing the ArrayList inside MyPointsList?

Below is the code for the following classes:

MyPoint.java

public class MyPoint implements Parcelable {
    private int x;
    private int y;

    public MyPoint(){
        this.x = 0;
        this.y = 0;
    }

    public MyPoint(Parcel in){
        this.x = 0;
        this.y = 0;
        readFromParcel(in);
    }

    public void setPoint(int px, int py){
        this.x = px;
        this.y = py;
    }

    public int getX(){return this.x;}

    public int getY(){return this.y;}

    public static final Parcelable.Creator <MyPoint> CREATOR = new Parcelable.Creator <MyPoint> (){
        public MyPoint createFromParcel(Parcel source) {
            // TODO Auto-generated method stub
            return new MyPoint(source);
        }

        public MyPoint[] newArray(int size) {
            // TODO Auto-generated method stub
            return new MyPoint[size];
        }       
    };

    public int describeContents() {
        // TODO Auto-generated method stub
        return 0;
    }

    public void writeToParcel(Parcel dest, int flags) {
        dest.writeInt(x);
        dest.writeInt(y);

    }

public void readFromParcel(Parcel in){
        int count = in.dataAvail();
        for(int i = 0; i < count; i++){
            x = in.readInt();
            i++;
            y = in.readInt();
        }
    }
}

MyPointsList.java

public class MyPointsList implements Parcelable {

    private ArrayList<MyPoint> arrList = new ArrayList<MyPoint>();
    private int myInt = 0;
    private String myString = null;

    public String getString(){return myString;}

    public void setString(String str){this.myString = str;}

    public MyPoint getMyPoint(int index){return this.arrList.get(index);}

    public ArrayList<MyPoint> getArrayList(){return arrList;}

    public void setArrayList(ArrayList<MyPoint> arr){this.arrList = arr;}

    public int getMyInt(){return myInt;}

    public void setMyInt(int newInt){this.myInt = newInt;}

    MyPointsList(){this.arrList = new ArrayList<MyPoint>();}

    public MyPointsList(Parcel in){
        myInt = in.readInt();
        myString = in.readString();
        arrList = new ArrayList<MyPoint>();
        in.readTypedList(arrList, MyPoint.CREATOR);
    }

    public int describeContents() {
        // TODO Auto-generated method stub
        return 0;
    }

    public void writeToParcel(Parcel out, int flags) {
        // TODO Auto-generated method stub
        out.writeInt(myInt);
        out.writeString(myString);
        out.writeTypedList(arrList);
    }

    public static final Parcelable.Creator<MyPointsList> CREATOR = new Parcelable.Creator<MyPointsList>(){

        public MyPointsList createFromParcel(Parcel source) {
            // TODO Auto-generated method stub
            return new MyPointsList(source);
        }

        public MyPointsList[] newArray(int size) {
            // TODO Auto-generated method stub
            return new MyPointsList[size];
        }   
    };
}

Packaging Segment: points is an ArrayList of MyPoints

public void onClick(View v){
    MyPointsList object = new MyPointsList();
        object.setMyInt(100);
        object.setArrayList(points);
        Log.d("DEBUG", object.getMyPoint(5).getX() + " " + object.getMyPoint(5).getY() + " ");
        Intent goBack = new Intent(SignHereActivity.this, Menu.class);
        goBack.putExtra("parcel" , object);             
        SignHereActivity.this.startActivity(goBack);
    }
});

Retreiving Segment

public void getIntentData(){
    Intent i = this.getIntent();
    if(i.hasExtra("parcel")){
        Bundle b = getIntent().getExtras();
        object = b.getParcelable("parcel");
        Log.d("Debug", object.getArrayList().get(5).getX() + " " + object.getArrayList().get(5).getY());
    }
}

As mentioned, I've been browsing through whatever information I can find related to my problem. I greatly appreciate any input and responses. I apologize for any annoying syntax or confusing segments in my code. I am still new to java and Android as a whole.

Thanks!

yizzlez
  • 8,757
  • 4
  • 29
  • 44
LawrenceBaker
  • 49
  • 1
  • 6

1 Answers1

0

I feel like a dummy, but if anyone else stumbles upon this, here's the solution.

Remove from MyPoint:

public void readFromParcel(Parcel in){
    int count = in.dataAvail();
    for(int i = 0; i < count; i++){
        x = in.readInt();
        i++;
        y = in.readInt();
    }
}

Insert to MyPoint:

public void readFromParcel(Parcel in){
    x = in.readInt();
    y = in.readInt();
}

That's it.

Kyle Falconer
  • 8,302
  • 6
  • 48
  • 68
LawrenceBaker
  • 49
  • 1
  • 6
  • I fixed the answer by moving the solution he had in the question to this answer, which was basically a "I solved it" post -.- – Kyle Falconer Jun 13 '14 at 20:34