0

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
jordan
  • 1
  • 1
  • 2
    Your `SN` from `main` is a very different instance than `SN` from `gameBoard`, so the `SN` from `gameBoard` doesn't know the names you've entered into `SN` from `main`. – Tom Jun 08 '16 at 13:04
  • You should make that an answer and elaborate on it. – Zircon Jun 08 '16 at 13:04
  • @Zircon *"and elaborate on it"* and that's the problem, I'm too lazy right now :P. Maybe you want to go ahead and write a nice answer :)? – Tom Jun 08 '16 at 13:06
  • @Tom Ah, that makes sense. Do you have a suggestion on what I could do to fix it? – jordan Jun 08 '16 at 13:06
  • Jordan, I'll give you a hint: `public void play(setName SN) {...}` ;). Think about that this might mean and then implement it :). – Tom Jun 08 '16 at 13:08
  • 2
    Note: Java language conventions are that type names (Class names, enums, interfaces) start with a capital letter, e.g.`SetName`, `Main`, `GameBoard`; Variables and method names should start with a lowercase letter and not have underscores, e.g. `userInput`,`sn` (better named `setName` of type `SetName`), and only constants should have all-capitals and underscores (so a name like `GB` should only be given to a constant, though a more readable name like `GAME_BOARD` would be preferred if it was a constant). – RealSkeptic Jun 08 '16 at 13:09

5 Answers5

2

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;
    }
    ...
hotzst
  • 7,238
  • 9
  • 41
  • 64
  • you dont need to use this.SN because the sn you are passing to the gameBoard is in lowecase while the SN object from the gameBoard class is in uppercase. – Igoranze Jun 08 '16 at 13:12
  • While technically, you are correct, I find that 'this' is sometimes a nice reminder and makes for more readable code. It certainly doesn't hurt – Scott Twombly Jun 08 '16 at 13:15
  • @Igoranze you are right of course. I still think it adds a bit more clarity especially if there is some renaming later on. – hotzst Jun 08 '16 at 13:16
2

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).

Boann
  • 48,794
  • 16
  • 117
  • 146
1

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.

Leo
  • 5,017
  • 6
  • 32
  • 55
  • So how would I go about initiating it? – jordan Jun 08 '16 at 13:09
  • Inside setName class change **public String GlobalP1;** and **public String GlobalP2;** to **public static String GlobalP1;** and **public static String GlobalP2;** – Leo Jun 08 '16 at 13:14
1

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
}
Igoranze
  • 1,506
  • 13
  • 35
0

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