jordan
jordan

Reputation: 1

Why is this printing NULL?

I was wondering if anyone could tell me why I was getting the output 'NULL' when running this and setting the names. I have done a return method to return names from setName but it still appears as null as if they haven't been set.

What is supposed to happen is - run the main menu - set the names - then run the Play method in gameBoard.java and see the names in there - but I can only see null.

Main Class:

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

    }
}

Console output:

    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

Upvotes: 0

Views: 190

Answers (5)

Boann
Boann

Reputation: 50010

Class gameBoard has its own completely unrelated instance of setName, which is initialized with null values, since you never call setName1(), setName2() on that instance. Notice your program has two separate setName SN = new setName(); lines.

If you want your gameBoard to receive the names typed earlier, you should pass them to it in a constructor or 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);
    }
}

In main create the gameBoard with those names after inputting them:

gameBoard GB = new gameBoard(p1Name, p2Name);

There other ways you could pass this data around, including passing the instance of setName to gameBoard, or have gameBoard itself be responsible for maintaining the single setName instance, although setName is not a great class. Unless there's more complexity or abstraction you're planning to integrate into that class, what it does at the moment is too trivial to justify its existence as a class, and it would be simpler to adsorb its two input methods into the main method, especially since you already have a Scanner instance there. In other words, instead of:

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

Do:

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

And then drop the setName class.

P.S. Please read up on the Java naming conventions as they will make your code clearer (once you get used to them, and especially for others trying to read your code). Class names should start with a capital letter (e.g., GameBoard rather than gameBoard); method and variable names should not (getName1() rather than GBName()); and method names should usually be verbs (displayBoard() rather than board()); while class names should be nouns (NameInput or PlayerInfo rather than setName, if you decide to keep that class).

Upvotes: 2

Igoranze
Igoranze

Reputation: 1486

in your GameBoard class you are make a new object

setName SN = new setName();

but you don't fill the player1 name and player2 name like you do in the main method.

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

To fix this, either ask the player1 name and player2 name again... or give the SN object to board and it should be fine.

like so:

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

the gameBoard class would look like this:

public class gameBoard {

    setName SN;

    public gameBoard(setName  SN) {
        this.SN = SN
    }
...
rest of the code
}

Upvotes: 1

Scott Twombly
Scott Twombly

Reputation: 84

Your gameboard() class is generating a new SN object, which is different from the SN object you created in main(). It's getting null references because you never assigned names to that new SN object. The names were left behind in the old main() SN

Upvotes: 0

hotzst
hotzst

Reputation: 7496

In your main class you create an instance of setName on which you are initializing the names. In your class gameBoard however you also create a new instance of setName, which does not have the names initialized, hence you get null printed out.

An easy workaround would be to pass the SN object created in main to the gameBoard as a constructor argument:

gameBoard GB = new gameBoard(SN);

This would mean that the gameBoard should define something like this as a constructor:

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

Upvotes: 2

Leo
Leo

Reputation: 5235

When you are calling the SN.GBName() method, it is returning GlobalP1 and assigning it to p1Name in the play() method. As GlobalP1 is not initialized or no value is assigned to it, that is why p1Name points to Null.

Upvotes: 1

Related Questions