0

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.

shmosel
  • 49,289
  • 6
  • 73
  • 138
Heliangs
  • 21
  • 4
  • `objectlist` is uninitialized. – shmosel Mar 21 '17 at 22:11
  • The best way to debug issues like this is with your debugger. Are you using NetBeans? Eclipse? Or IntelliJ? Either way what you want to do is first find where your exception is - line 85. That's what `PinballObject.java:85` means. Now click next to line 85 in `PinballObject.java` in the left column and you should be able to place what's called a `break point`. Once the break point is set instead of running your program choose "debug". Your program will run like normal but stop at the line you've selected where the break point is. Now you should be able to inspect all the declared variables. – fIwJlxSzApHEZIl Mar 21 '17 at 22:13
  • I'm a super slow learner, do you mean in the PinballObject class? and what am i lacking to initialize it and dispose of it? @shmosel – Heliangs Mar 21 '17 at 22:14
  • You can then find which ones are null and which ones aren't. All you then need to do is place the break point at previous points in your code trading the null variable back to where it was declared. Someone between where it was declared and line 85 in PinballObject.java it was set to null. Or it was never initialized in the first place. But debugging through your code and tracing the steps of code execution backwards will help you figure this out. – fIwJlxSzApHEZIl Mar 21 '17 at 22:15
  • I am using BlueJ @anon58192932, i will look into your suggestion asap. – Heliangs Mar 21 '17 at 22:16
  • `private ObjectList objectlist = new ObjectList();` – shmosel Mar 21 '17 at 22:17
  • Ah BlueJ is a little more primitive my apologies I don't know the extent of the debugging that BlueJ has. Also don't forget you have a typo in your `collection()` function. It should be `pinballTypeOneList.add(firstTypeOne); pinballTypeOneList.add(secondTypeOne); pinballTypeOneList.add(firstTypeOne);` – fIwJlxSzApHEZIl Mar 21 '17 at 22:18
  • And definitely review the recommended answer about NullPointerExceptions. Remember that in Java any non-primitive value must be initialized. This is in addition to being declared. Imagine this: `String s; // s is null` versus `String s = "hello"; // s == 'hello'` You have a lot of Classes that you've declared like `PinballObject` and `Machine`. These variables have to be initialized manually with `"new Machine()"` or their values have to be set by a constructor of the containing class. – fIwJlxSzApHEZIl Mar 21 '17 at 22:22
  • @anon58192932 I have corrected my typo, thank you. And yes i know non-primitive values must be initialized but i'm having a hard time grasping the concept when juggling information between different classes. – Heliangs Mar 21 '17 at 22:30
  • @shmosel trying what you said gave me a stackoverflow error instead, i'm even more confused. : / – Heliangs Mar 21 '17 at 22:31
  • That makes sense. Both classes are recursively creating instances of each other. You should probably pass `this` into the the `PinballObject` constructor so they're referencing the same object. Side note: `Object` is a redundant suffix, since every class instance is an object. Side note 2: Why on earth do you have a non-static `main(String[] args)`? – shmosel Mar 21 '17 at 22:36
  • @shmosel Thank you, i will look into it. Also, i made the main(String[] args) non static because attempting to access the collection() method through main would deny me access to the ArrayLists when calling them on the PinballObject class as you can't pull non-static values into a static field. Didn't know how to do it otherwise. – Heliangs Mar 21 '17 at 22:45
  • @Heliangs don't get discouraged. Static vs. non-static is extremely confusing when getting started. My recommendation is the following. Have an outer class like `Start.java` which declares `public static void main(String[] args)` which is the normal java entry point for java projects. And then inside `main(String[] args)` you can declare a new Class like `Pinball` with `Pinball pinball = new Pinball()`. Then you can kick off your game with something like `pinball.run()`. Then you can initialize all your starting values in the `Pinball` constructor and then `.run()` can get the ball rolling. – fIwJlxSzApHEZIl Mar 21 '17 at 23:04

0 Answers0