Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android

Unexpected Null Pointer Exception when trying to set variable in another class

So I keep getting a Null Pointer Exception for god knows what reason.

My Code is below

public class MyRide {

    private ParseUser mCurrentUser;
    private ArrayList<ParseObject> mParseRides;
    private ParseQuery<ParseObject> mMyRides;
    private List<ParseObject> mRides;

    public Rides[] mRideList;

mCurrentUser = ParseUser.getCurrentUser();

        mMyRides = mCurrentUser.getRelation("MyRides").getQuery();
        mMyRides.orderByAscending("Year");
        mMyRides.findInBackground(new FindCallback<ParseObject>() {
            @Override
            public void done(List<ParseObject> rides, ParseException e) {
                if (e == null) {
                    mRides = rides;
                    Double[] StartLat = new Double[mRides.size()];
                    mRideList = new Rides[mRides.size()];


                    int i = 0;
                    for (ParseObject Ride : mRides) {
                        StartLat[i] = (Double) Ride.get("StartLat") ;
                       mRideList[i].setStartLat(StartLat[i]);
                         i++}
                  ...........
}

I get an error in the line

mRideList[i].setStartLat(StartLat[i])

java.lang.NullPointerException: Attempt to invoke virtual method 'void com.dhruv.ridealong.UI.MyRide.Rides.setStartLat(java.lang.Double)' on a null object reference

I know the StartLat[i] has a value because I've logged it and seen that it does. I've also tried manually putting a value.


My Rides class consists of

public class Rides {

Double StartLat;

    public Double getStartLat() {
        return StartLat;
    }

    public void setStartLat(Double startLat) {
        StartLat = startLat;
    }
}

2 Answers

Hi Dhruv,

I think it's saying that mRideList[i] might be null - difficult to say. Do you have this in a git repo that I can pull down to replicate the error?

Steve.

P.S. I edited your post to make the code easier to read.

Nope :P

I really should take the Git class here, but have never gotten around to it.

Is there anything in particular you would want me to post? I put what I considered relevant code, and both of the two classes above are extremely abridged.

Yo I got it,

So I initiated my array, but every element inside of it was null

So I added the line in the for command mRideList[i] = new Rides();

I suspected that was where the problem was!

Glad you got it fixed.