0

I am tasked with creating a fairly simple dice based board game using Java. I have initialized all the dice objects in the constructor, however when I try to a method from the dice class inside the game class, I get the error (object name) cannot be resolved. Forgive me if that explanation isn't clear but hopefully the code will make more sense. The error is encountered in the rollDice() method of the qwixx class.

public class qwixx {

    public qwixx() {
        dice white1 = new dice();
        dice white2 = new dice();
        dice red = new dice();
        dice yellow = new dice();
        dice green = new dice();
        dice blue = new dice();
    }

public void rollDice() {
        white1.rollDice();
        white2.rollDice();
        red.rollDice();
        yellow.rollDice();
        green.rollDice();
        blue.rollDice();
    }

}

public class dice {

String colour;
    int currentSide;

    public dice() {
        colour = "white";
        rollDice();
    }

public int rollDice() {
        currentSide = (int)(Math.random() * 6 + 1);
        return currentSide;
    }

}
0

1 Answer 1

1

All your variables in the constructor must be declared at the class level.

public class qwixx {
    // declare the dice variables at the class level (as 'fields')
    dice white1;
    // same for other dice : declare them here

    public qwixx() {
        // in the constructor you actually create the object and assign references to the class variables
        white1 = new dice();
        // idem for others
    }
}

That's how all the methods in the class can have access to these fields.

Otherwise, your dice references would only be visible in the method where they were declared, in the constructor, and of course that's not what you want, and the cause of your errors.

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

9 Comments

Thanks for the quick solution, say I wanted to print out the values of all the dice current sides. Why does something like this not work?
System.out.println("Red dice: " + red.currentSide + " | Yellow dice: " + yellow.currentSide + " | Green dice: " + green.currentSide + " | Blue dice: " + blue.currentSide + " | White1 dice: " + white1.currentSide + " | White1 dice: " + white2.currentSide);
the need to use a method like "getCurrentSide()" instead of accessing directly the field is a better practice in Object Oriented programming. But as you already noticed, that's not the cause of a NullPointerException.
"doesn't work" is extremely vague, please avoid using this phrase when describing code problems. If you mean that you have nullPointerException, then it means that you are trying to execute the above code before you have created at least one of the objects (with yourfield = new dice();). some reference is still null and you are trying to call a method on a null reference.
Got it. And yes I meant returns the nullPointerException.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.