-3

Basically I am creating a simply game, I want circles to appear but they can not overlap each other when they are printed on the screen. The objects are created and then put into an arraylist and when ever another is created it needs to check its coordinates with all the others already in the arraylist to make sure its not the same. The method to check is called isCollidingWith and there is a setPosition method that actually sets the position. This is what I have.

This is how the object is created, I can't even get one to be created, once I get it working I would create more of these objects, hence the whole point of this.

    Circle circle = new Circle(rng,circles);
    circles.add(circle);

This is the constructor for the object takes in a random number and and arraylist of circles. It starts with a while loop and sets a position for this circle it then goes into a for loop and checks if it is colliding with and of the other ones, if it is, it breaks and goes back to the top of the while loop and sets a different positions and then repeats checking if it is colliding with any of the other ones already in the arraylist.

    public Planet(Random rng, ArrayList<Circle> circles){

        boolean key = true;
        while(key){

        this.graphic.setPosition(rng.nextFloat()*GameEngine.getWidth(),rng.nextFloat()*GameEngine.getHeight());

        for(int i = 0; i<circles.size();i++){
            if(this.graphic.isCollidingWith(circles.get(i).graphic)){
                key = true;
                break;
            }
            else key = false;

        }

        }


    }

The problem is I keep getting a NullPointerException at the line in the constructor where it says this.graphic.setPosition. But I am guessing it has to do with the arraylist, because eclipse usually isn't that accurate with error positioning. If anyone could tell me what looks to be wrong here that would be great.

John Smith
  • 37
  • 1
  • 8

1 Answers1

0

1)If you feel like "eclipse isnt that accurate with error positionning" you should make a clean of your project, rebuild it abd refresh (F5). Its just a matter of sources unsynced because not refreshed. 2) to understand better yoyr error you should decompose the line in question in several steps : first line to compute the value second line to set it. 3) looks like this.graphic is never initialized thus the nullpointerexception ?

ChapL
  • 28
  • 4