为什么打印为NULL?

时间:2016-06-08 12:53:21

标签: java

我想知道是否有人能告诉我为什么在运行此命令并设置名称时输出'NULL'。我已经做了一个返回方法来从setName返回名称,但它仍然显示为null,就像它们尚未设置一样。

应该发生的是 - 运行主菜单 - 设置名称 - 然后在gameBoard.java中运行Play方法并查看其中的名称 - 但我只能看到null。

主类:

import java.lang.reflect.Array;
import java.util.Random;
import java.util.Scanner;

public class main {


    public static void main(String[] args) {

        setName SN = new setName();
        gameBoard GB = new gameBoard();
        Scanner user_input = new Scanner(System.in);
        int mainMenuChoice = 0;
        String p1Name = null;
        String p2Name = null;

        while(mainMenuChoice >= 0 && mainMenuChoice < 3){

            if(mainMenuChoice == 0){
                if(p1Name != null){
                    System.out.println("Welcome " + p1Name + " and " + p2Name + " to AQADo! ");
                }
                System.out.println("MAIN MENU");
                System.out.println("1. Enter Player Names");
                System.out.println("2. Play Game");
                System.out.println("3. Quit");
                System.out.println("Select your number");
                mainMenuChoice = Integer.parseInt(user_input.next());
            }
            if(mainMenuChoice == 1){
                p1Name = SN.setName1();
                p2Name = SN.setName2();
                mainMenuChoice = 0;
            }
            if(mainMenuChoice == 2){
            int menu = 1;
            int[] p1score = {};
            Random rn = new Random();
                GB.board();
                GB.play();
                String test = user_input.next();
                for(int i = 0; i < 50; i++){
                    p1score[0] = rn.nextInt(6);


                mainMenuChoice = 0;

            }

        }


    }



    }
}

setName class:

    import java.util.Scanner;

public class setName {
    public String GlobalP1;
    public String GlobalP2;


    public String setName1(){
        Scanner user_input = new Scanner(System.in);
        String p11Name;
        System.out.println("Enter the first players name:");
        p11Name= user_input.next();
        GlobalP1 = p11Name;

        return p11Name;
    }
    public String setName2(){
        Scanner user_input = new Scanner(System.in);
        String p22Name;
        System.out.println("Enter the second players name:");
        p22Name= user_input.next();
        GlobalP1 = p22Name;


        return p22Name;
    }
    public String GBName(){
        return GlobalP1;
    }
    public String GBName2(){
        return GlobalP2;
    }
}

gameBoard Class:

public class gameBoard {

    setName SN = new setName();

    char[] array1 = new char[11];{
    array1[0] = 'x';
    array1[1] = 'x';
    array1[2] = 'x';
    array1[3] = 'x';
    array1[4] = 'x';
    array1[5] = 'x';
    array1[6] = 'x';
    array1[7] = 'x';
    array1[8] = 'x';
    array1[9] = 'x';
    array1[10] = 'x';
    }
    char[] array2 = new char[11];{
    array2[0] = 'x';
    array2[1] = 'x';
    array2[2] = 'x';
    array2[3] = 'x';
    array2[4] = 'x';
    array2[5] = 'x';
    array2[6] = 'x';
    array2[7] = 'x';
    array2[8] = 'x';
    array2[9] = 'x';
    array2[10] = 'x';
}

    public void board(){


        System.out.println("           1  2  3  4  (5)  6  7  8  9  10  11");
        System.out.println("Player 1:  " + array1[0]+ "  " + array1[1] + "  " + array1[2]+ "  " + array1[3] + "   " + array1[4]+ "   " + array1[5] + "  " + array1[6]+ "  " + array1[7] + "  " + array1[8]+ "  " + array1[9] + "   " + array1[10]);
        System.out.println("Player 2:  " + array2[0]+ "  " + array2[1] + "  " + array2[2]+ "  " + array2[3] + "   " + array2[4]+ "   " + array2[5] + "  " + array2[6]+ "  " + array2[7] + "  " + array2[8]+ "  " + array2[9] + "   " + array2[10]);


    }
    public void play(){
        String p1Name = SN.GBName();
        String p2Name = SN.GBName2();
        String pArray[] = {p1Name,p2Name};


        System.out.println(p1Name + " test");

    }
}

控制台输出:

    MAIN MENU
1. Enter Player Names
2. Play Game
3. Quit
Select your number
1
Enter the first players name:
Jordan
Enter the second players name:
David
Welcome Jordan and David to AQADo! 
MAIN MENU
1. Enter Player Names
2. Play Game
3. Quit
Select your number
2
           1  2  3  4  (5)  6  7  8  9  10  11
Player 1:  x  x  x  x   x   x  x  x  x  x   x
Player 2:  x  x  x  x   x   x  x  x  x  x   x
null test

5 个答案:

答案 0 :(得分:2)

main课程中,您将创建一个setName的实例,并在其上初始化名称。在您的班级gameBoard中,您还会创建一个setName的新实例,该实例没有初始化名称,因此您会打印出null

一个简单的解决方法是将SN中创建的main对象作为构造函数参数传递给gameBoard

gameBoard GB = new gameBoard(SN);

这意味着gameBoard应该将此类定义为构造函数:

class gameBoard {
    setName SN;
    public gameBoard(setName sn) {
        this.SN = sn;
    }
    ...

答案 1 :(得分:2)

gameBoard有自己完全不相关的setName实例,该实例使用null值初始化,因为您从未调用setName1()setName2()实例。请注意,您的程序有两个单独的setName SN = new setName();行。

如果您希望gameBoard收到之前输入的名称,则应在构造函数或setter中将它们传递给它:

public class gameBoard {
    public final String p1Name;
    public final String p2Name;

    public gameBoard(String p1Name, String p2Name) {
        this.p1Name = p1Name;
        this.p2Name = p2Name;
    }

    ...

    public void play() {
        System.out.println("In play(): " + p1Name + ", " + p2Name);
    }
}

main中输入后,使用名称创建gameBoard

gameBoard GB = new gameBoard(p1Name, p2Name);

您可以通过其他方式传递此数据,包括将setName的实例传递给gameBoard,或让gameBoard本身负责维护单个setName实例虽然setName不是一个伟大的阶级。除非你有更多的复杂性或抽象,你计划整合到那个类中,它目前做的事情太过微不足以证明它作为一个类的存在,并且吸收它的两个输入方法会更简单进入main方法,特别是因为那里已有Scanner个实例。换句话说,而不是:

p1Name = SN.setName1();
p2Name = SN.setName2();

执行:

System.out.println("Enter the first player's name:");
p1Name = user_input.nextLine();
System.out.println("Enter the second player's name:");
p2Name = user_input.nextLine();

然后删除setName类。

P.S。请阅读Java命名约定,因为它们将使您的代码更清晰(一旦您习惯了它们,特别是对于其他试图读取您的代码的人)。类名应以大写字母开头(例如GameBoard而不是gameBoard);方法和变量名称不应该是getName1()而不是GBName());和方法名称通常应该是动词(displayBoard()而不是board());类名应该是名词(NameInputPlayerInfo而不是setName,如果您决定保留该类。)

答案 2 :(得分:1)

当您调用SN.GBName()方法时,它会返回GlobalP1并在p1Name方法中将其分配给play()。由于GlobalP1未初始化或未分配任何值,因此p1Name指向Null

答案 3 :(得分:1)

在你的GameBoard课程中,你是一个新的对象

features

但是你没有像在main方法中那样填充player1名称和player2名称。

setName SN = new setName();

要解决此问题,请再次询问player1名称和player2名称......或 将SN对象放到板上,应该没问题。

像这样:

if(mainMenuChoice == 1){
    p1Name = SN.setName1();
    p2Name = SN.setName2();
    mainMenuChoice = 0;
}

gameBoard类看起来像这样:

setName SN = new setName();
gameBoard GB = new gameBoard(SN);

答案 4 :(得分:0)

您的gameboard()类正在生成一个新的SN对象,它与您在main()中创建的SN对象不同。它正在获取空引用,因为您从未为该新SN对象分配名称。名字留在旧的main()SN