bpfuels
bpfuels

Reputation: 5

Getter for java array returns null

When I use a getter to return the value of an array, it declares null. However, this does not occur if I define the array outside the class constructor, unfortunately as it is defined by a for loop it must be in the constructor. The getter:

public static String[] getLetters(){
    return dispLetter;
}

The for loop that defines the array:

for(int i=0; i<16; i++){
        int letterSelect = (int) (Math.random()*6+1);
        System.out.print(letterSelect+",");
        dispLetter[i]=letters[dice[i]-1][letterSelect-1];

The loop were it is refrenced in the other class:

for(int i=0; i<16;i++){
        grid[i]=new JLabel(" "+Dice.getLetters()[i]+" ");
        grid[i].setFont(new Font("Arial", Font.BOLD, 68));
        grid[i].setHorizontalAlignment(SwingConstants.CENTER);
        grid[i].setVerticalAlignment(SwingConstants.CENTER);
    }

The entire "Dice" class if needed:

package excersize.pkg9;

import java.util.Random;

public class Dice {
    private static String dispLetter[] = new String[16];

    public Dice() {
        int dice[] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16};

        for (int i = 0; i < dice.length; i++) {
            dice[i] = i + 1;
        }

        Random rnd = new Random();

        for (int i = 0; i < dice.length; i++) {
            int changeBy = rnd.nextInt(dice.length);
            int value = dice[i];
            dice[i] = dice[changeBy];
            dice[changeBy] = value;
        }

        for (int i = 0; i < dice.length; i++) {
            System.out.print(dice[i] + ",");
        }

        String letters[][] = new String[][]{
                {"A","F","P","K","F","S"},
                {"E","T","T","R","L","Y"},
                {"D","E","Y","L","V","R"},
                {"C","P","O","H","A","S"},
                {"I","Y","S","D","T","T"},
                {"N","E","E","H","G","W"},
                {"R","N","Z","N","L","H"},
                {"R","D","I","X","E","L"},
                {"Qu","N","M","I","H","U"},
                {"T","S","E","I","S","O"},
                {"T","T","O","A","O","W"},
                {"V","T","H","R","W","E"},
                {"S","I","E","N","E","U"},
                {"T","U","I","C","O","M"},
                {"B","O","A","J","O","B"},
                {"G","A","E","A","N","E"},
        };

        System.out.println();

        for(int i=0; i<16; i++){
            int letterSelect = (int) (Math.random()*6+1);
            System.out.print(letterSelect+",");
            dispLetter[i]=letters[dice[i]-1][letterSelect-1];
        }

        System.out.println();

        for(int i=0; i<dispLetter.length; i++){
            System.out.print(dispLetter[i]+",");
        }
    }

    public static String[] getLetters(){
        return dispLetter;
    }
}

Upvotes: 0

Views: 373

Answers (1)

Neeraj
Neeraj

Reputation: 2558

Its generally not a good idea to initialize a static member in a constructor. You are referencing dispLetter before it has been initialized in the Dice() constructor. Remove the static modifier from dispLetter and its getter method

Then, the code should work fine after this change :
Replace

grid[i]=new JLabel(" "+Dice.getLetters()[i]+" ");

with

grid[i]=new JLabel(" "+(new Dice().getLetters()[i])+" ");

OR
If you really think dispLetter should remain static , then move the code in the constructor to a static initialzer block :

static {
int dice[] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16};
    for (int i = 0; i < dice.length; i++) {
        dice[i] = i + 1;
    }
    Random rnd = new Random();
    for (int i = 0; i < dice.length; i++) {
        int changeBy = rnd.nextInt(dice.length);
        int value = dice[i];
        dice[i] = dice[changeBy];
        dice[changeBy] = value;
    }
    for (int i = 0; i < dice.length; i++) {
        System.out.print(dice[i] + ",");
    }

    String letters[][] = new String[][]{
            {"A","F","P","K","F","S"},
            {"E","T","T","R","L","Y"},
            {"D","E","Y","L","V","R"},
            {"C","P","O","H","A","S"},
            {"I","Y","S","D","T","T"},
            {"N","E","E","H","G","W"},
            {"R","N","Z","N","L","H"},
            {"R","D","I","X","E","L"},
            {"Qu","N","M","I","H","U"},
            {"T","S","E","I","S","O"},
            {"T","T","O","A","O","W"},
            {"V","T","H","R","W","E"},
            {"S","I","E","N","E","U"},
            {"T","U","I","C","O","M"},
            {"B","O","A","J","O","B"},
            {"G","A","E","A","N","E"},

    };

    System.out.println();
    for(int i=0; i<16; i++){
        int letterSelect = (int) (Math.random()*6+1);
        System.out.print(letterSelect+",");
        dispLetter[i]=letters[dice[i]-1][letterSelect-1];
    }
    System.out.println();
    for(int i=0; i<dispLetter.length; i++){
        System.out.print(dispLetter[i]+",");
    }
    }

public Dice() {
}

Upvotes: 2

Related Questions