java数组的getter返回null

时间:2017-08-09 01:26:44

标签: java arrays

当我使用getter返回数组的值时,它声明为null。但是,如果我在类构造函数之外定义数组,则不会发生这种情况,不幸的是,它由for循环定义,它必须在构造函数中。 吸气者:

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

定义数组的for循环:

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];

循环是在其他类中引用的:

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);
    }

整个&#34;骰子&#34;如果需要的话:

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;
    }
}

1 个答案:

答案 0 :(得分:2)

在构造函数中初始化静态成员通常不是一个好主意。 您在dispLetter构造函数中初始化之前引用Dice()。从static及其getter方法

中删除dispLetter修饰符

然后,在此更改后代码应该可以正常工作:
替换

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

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


如果你真的认为dispLetter应该保持static,那么将构造函数中的代码移动到静态初始化块:

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() {
}