1

I'm making a game and it is giving me a NullPointerException which I believe means the variable or what I'm trying to do is not returning a value? I'm using the code below:

package OurGame;

import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.Timer;

public class Coin extends JPanel implements ActionListener {
/**
     * 
     */
    private static final long serialVersionUID = 1L;

Image img;

int x,y;
Timer t;
Random r;

        public Coin() {
            x = 50;
            y = 50;

            System.out.println("New coin created: " + x + ", " +y);

            ImageIcon i = new ImageIcon("C:/coin.png");
            img = i.getImage();
            System.out.println(i);

            t = new Timer(3000,this);
            t.start();
        }

        @Override
        public void actionPerformed(ActionEvent arg0) {
            move();

        }

        public void move() {

            setX(r.nextInt(640));
            setY(r.nextInt(480));


        }

        public void setX(int xs) {
            x = xs;
        }

        public void setY(int ys) {
            y = ys;
        }
        public Image getImage(){
            return img;
        }

        public int getX(){
            return x;
        }

        public int getY() {
            return y;
        }


}

The error is happening here:

        setX(r.nextInt(640));

The full error output is below:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at OurGame.Coin.move(Coin.java:46)
    at OurGame.Coin.actionPerformed(Coin.java:40)
    at javax.swing.Timer.fireActionPerformed(Unknown Source)
    at javax.swing.Timer$DoPostEvent.run(Unknown Source)
    at java.awt.event.InvocationEvent.dispatch(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

I dont see any reason for this to happen but maybe you can help.

Thanks.

3 Answers 3

6

You are using r without ever saying r = new ....

Sign up to request clarification or add additional context in comments.

Comments

4

You need to initialise the variable r. Check out the constructors for java.util.Random, for example:

Random r = new Random();

Comments

2

r is null. Initialize it first. Put this in your constructor:

r = new Random();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.