0

Background

Hooray! Pi DAYYY! What I am trying to do is place random dots on a screen. The gui opens in screen state 0 and when I press 1 the screen state changes to screen state 1 which then should theoretically add random dots onto the screen. However, It doesn't work.

The problem

So if you look at the line "dotz.add(new Dot());" the compiler flagged that as a nullpointer exception but I wasn't sure why that was since i'm passing the Dot object

main controller code here-

package piday;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.util.ArrayList;

import com.sun.glass.events.KeyEvent;

public class controllah extends GameDriverV4 {
    int state = 0;
    Font font = new Font("Helvetica Neue", Font.BOLD,50);
    Dot Dot = new Dot();
    ArrayList<Dot> dotz;
    int time = 0;
    int delay = 50000;
    public controllah() {

    }

    public static void main(String[] args) {
        controllah h = new controllah();
        h.start();
    }


    public void draw(Graphics2D win) {
        if (state == 0) {
            win.setColor(Color.BLACK);
            win.drawString("'MONTE CARLO'", 10, 200);
            win.drawString("'PRESS 1'", 10, 200+font.getSize()+10);
            if (GameDriverV4.Keys[KeyEvent.VK_1]) {
                state = 1;
            }
            win.drawString("'Needle Thing'", 10, 200+font.getSize()+10+50);
            win.drawString("'PRESS 2'", 10, 200+font.getSize()+10+50+15);
            if(GameDriverV4.Keys[KeyEvent.VK_2]) {
                state = 2;
            }
        }


        if (state == 1) {
            /*
            if (time>delay) {

                time = 0;
            }
            else {
                time ++;
            }
            */

            dotz.add(new Dot());
            for (int i = 0; i<dotz.size()-1; i++) {
                dotz.get(i).draw(win);
            }

        }

        if (state == 2) {

        }

    }
}

Dot object code here-

package piday;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.util.Random;

public class Dot {
    Color incolor = Color.BLACK;
    Color outcolor = Color.BLUE;
    boolean in = true;
    int x;
    int y;
    public Dot() {
        Random rand = new Random();
        this.x = rand.nextInt(800);
        this.y = rand.nextInt(800);
    }


    public void draw(Graphics2D g) {
        if (in) {
            g.setColor(incolor);
        }
        else {
            g.setColor(outcolor);
        }

        g.fillRect(x-1, y-1, 3, 3);

    }

}

I reasoned that since I've been trying to add a new "Dot" object into the arraylist "dotz" that it would be fine since the new "Dot" is not a null object

Kritikalken
  • 105
  • 5

0 Answers0