I am trying to make a class which will generate ArrayLists that will be called and iterated on a different class. I'm afraid i have a problem into how the ArrayLists were made or how i'm attempting to call the ArrayList into a different class. Program crashes when a PinballObject hits the assigned left wall.
java.lang.NullPointerException at PinballObject.move(PinballObject.java:85)
"for(PinballObject po : objectlist.getPinballTypeOneList())"
public class PinballObject
{
private int currentXLocation;
private int currentYLocation;
private int speedXTravel;
private int speedYTravel;
private Color colour;
private int radius;
private Machine machine;
private ObjectList objectlist;
private final int leftWallPosition;
private final int bottomWallPosition;
private final int topWallPosition;
private final int rightWallPosition;
/**
* Constructor for objects of class Pinball_Obj
*
* @param xPos the horizontal coordinate of the object
* @param yPos the vertical coordinate of the object
* @param xVel the horizontal speed of the object
* @param yVel the vertical speed of the object
* @param objectRadius the radius (in pixels) of the object
* @param objectColor the color of the object
* @param theMachine the machine this object is in
*/
public PinballObject(int xPos, int yPos, int xVel, int yVel, Color objectColor, int objectRadius, Machine theMachine)
{
currentXLocation = xPos;
currentYLocation = yPos;
speedXTravel = xVel;
speedYTravel = yVel;
colour = objectColor;
radius = objectRadius;
machine = theMachine;
leftWallPosition = machine.getLeftWall();
bottomWallPosition = machine.getBottomWall();
topWallPosition = machine.getTopWall();
rightWallPosition = machine.getRightWall();
}
/**
* Move this object according to its position and speed and redraw.
**/
public void move()
{
// remove from universe at the current position
machine.erase(this);
// compute new position
currentYLocation += speedYTravel;
currentXLocation += speedXTravel;
// check if it has hit the leftwall
if(currentXLocation <= (leftWallPosition + radius))
{
currentXLocation = leftWallPosition + radius;
speedXTravel = -speedXTravel;
for(PinballObject po : objectlist.getPinballTypeOneList())
{
if(this.equals(po))
{
colour = Color.ORANGE;
}
}
}
machine.draw(this);
}
You will find the ObjectList class as well below.
public class ObjectList
{
private PinballObject pinballobject;
private Machine machine;
private ArrayList<PinballObject> pinballTypeOneList = new ArrayList<PinballObject>();
private ArrayList<PinballObject> pinballTypeTwoList = new ArrayList<PinballObject>();
private ArrayList<PinballObject> bumperList = new ArrayList<PinballObject>();
PinballObject firstTypeOne = new PinballObject(50, 200, -5, 3, Color.RED, 10, machine);
PinballObject secondTypeOne = new PinballObject(100, 300, 1, 2, Color.BLUE, 55, machine);
PinballObject thirdTypeOne = new PinballObject(450, 125, -1, -1, Color.YELLOW, 40, machine);
PinballObject firstTypeTwo = new PinballObject(200, 50, 3, -5, Color.MAGENTA, 25, machine);
PinballObject secondTypeTwo = new PinballObject(300, 100, 2, -1, Color.PINK, 45, machine);
PinballObject thirdTypeTwo = new PinballObject(250, 250, 4, -2, Color.GREEN, 30, machine);
PinballObject firstBumper = new PinballObject(120, 135, 0, 0, Color.GRAY, 23, machine);
PinballObject secondBumper = new PinballObject(344, 400, 0, 0, Color.GRAY, 13, machine);
public void main(String[] args)
{
collection();
}
public void collection()
{
pinballTypeOneList.add(firstTypeOne);
pinballTypeOneList.add(firstTypeOne);
pinballTypeOneList.add(firstTypeOne);
pinballTypeTwoList.add(firstTypeTwo);
pinballTypeTwoList.add(secondTypeTwo);
pinballTypeTwoList.add(thirdTypeTwo);
bumperList.add(firstBumper);
bumperList.add(secondBumper);
}
public ArrayList<PinballObject> getPinballTypeOneList()
{
return pinballTypeOneList;
}
public ArrayList<PinballObject> getPinballTypeTwoList()
{
return pinballTypeTwoList;
}
public ArrayList<PinballObject> getBumpers()
{
return bumperList;
}
}
I think it's because the iteration is finding 0 stored objects within the ArrayList or perhaps how i'm trying to implemented these ArrayLists. I have read through what is a NullPointerException and how to fix it but i still can't pull through.
Any answer/suggestion will be more than welcome, i will be active with replies and i hope this case can help other learning coders in the future.