0

While running the public tests for my code I have received 2 errors on similar lines. The errors are:

Null Pointer Exception Errors and the line in which they appear is the one with getCurrentLocation & getName in it.

assertEquals("Initially, Parrade is at Fun Fire", "Fun Fire", parrade.getCurrentLocation().getName());

parrade.move();

The full code of my constructors as well as getCurrentLocation & getName are as follows:

Code:

public abstract class Ride {
private String name;
private int duration;
private int batchSize;
private ArrayList<Amuser> currentAmusers;
private int ridesToMaintain;
private FunRide location;

public Ride() {
    this.name = "Serpent";
    this.duration = 5;
    this.batchSize = 20;
    this.ridesToMaintain = 10;
    this.currentAmusers = new ArrayList<Amuser>();
}

public Ride(String name, int duration, int batchSize) {
    this.name = name;
    this.duration = duration;
    this.batchSize = batchSize;
    this.ridesToMaintain = 10;
    this.currentAmusers = new ArrayList<Amuser>();
}

public FunRide getCurrentLocation() {
    return this.location;
}

Any idea how to solve this?

Community
  • 1
  • 1
  • possible duplicate of [What is a Null Pointer Exception?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception) – aliteralmind Mar 20 '14 at 16:53

2 Answers2

0

If this is the line causing a NullPointerException

assertEquals("Initially, Parrade is at Fun Fire", "Fun Fire", parrade
   .getCurrentLocation().getName());

then either parrade or parrade.getCurrentLocation() is equal to null or parrade.getCurrentLocation().getName() is throwing one (the latter of which seems unlikely). But since this variable is nowhere in your posted code, I'm afraid we won't be able to help you more than that.

You must have something like this already (assuming parrade is the culprit)

private Parade parrade = null;

or

private Parade parrade;

Meaning you've declared this variable, but you've not initialized it. Initialize it with something like:

parrade = new Parade();
aliteralmind
  • 19,847
  • 17
  • 77
  • 108
0

The reason you are getting a null pointer is because the value of either location or name is never being set. When you declare all your instance variables, they are initially null, so if you try and use them before instantiating them, it will throw a null pointer exception.

9er
  • 1,604
  • 3
  • 20
  • 37