Issue
I am making a 'Who Wants To Be A Millionaire' game. I am at the later stages, and I am trying to store the user names and scores, but when I call getUser() from another method it comes up as null, I printed the value of getUser() out to make sure it actually storing the values. Here is my code
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package assignment1;
import java.util.HashMap;
/**
*
* @author ac918
*/
public class User {
// Earnings earning;
String namee;
private String user;
private Integer scores;
boolean repeat = true;
GameMethods getGameMethods = new GameMethods();
HashMap <String, Integer> storeDetails = new HashMap<>();
public String storeUser()
{
System.out.println("Before we begin what is your name?");
setUser(getGameMethods.getUserInput());
//user = this.getUser();
namee = this.getUser();
System.out.println("This is getUser in storeUser method" + namee);
if (this.getUser().matches(".*[a-zA-Z]+.*"))
{
System.out.println("Goodluck to you " + this.getUser() + " on your quest to win a million dollars");
}
else
{
while(repeat)
{
System.out.println("Invalid Input!");
System.out.println("Name must be letters");
System.out.println("Please input name to begin");
setUser(getGameMethods.getUserInput());
if (this.getUser().matches(".*[a-zA-Z]+.*"))
{
System.out.println("Goodluck to you " + this.getUser() + " on your quest to win a million dollars");
repeat = false;
}
}
}
return this.getUser();
}
/**
* @return the user
*/
public String getUser() {
return user;
}
/**
* @param user the user to set
*/
public void setUser(String user) {
this.user = user;
}
/**
* @return the score
*/
public Integer getScore() {
return scores;
}
public int setUserEarnings (int count)
{
setScore(count);
switch (count) {
case 0:
System.out.println("Unfortunately, you have won nothing !!!");
break;
case 1:
System.out.println("Congratulations!!! you have won " + Earnings.ONEHUNDRED.getValue());
break;
case 2:
System.out.println("Congratulations!!! you have won " + Earnings.TWOHUNDRED.getValue());
break;
case 3:
System.out.println("Congratulations!!! you have won " + Earnings.THREEHUNDRED.getValue());
break;
case 4:
System.out.println("Congratulations!!! you have won " + Earnings.FIVEHUNDRED.getValue());
break;
case 5:
System.out.println("Congratulations!!! you have won " + Earnings.ONETHOUSAND.getValue());
break;
case 6:
System.out.println("Congratulations!!! you have won " + Earnings.TWOTHOUSAND.getValue());
break;
case 7:
System.out.println("Congratulations!!! you have won " + Earnings.FOURTHOUSAND.getValue());
break;
case 8:
System.out.println("Congratulations!!! you have won " + Earnings.EIGHTTHOUSAND.getValue());
break;
case 9:
System.out.println("Congratulations!!! you have won " + Earnings.SIXTEENTHOUSAND.getValue());
break;
case 10:
System.out.println("Congratulations!!! you have won " + Earnings.THIRTYTWOTHOUSAND.getValue());
break;
case 11:
System.out.println("Congratulations!!! you have won " + Earnings.SIXTYFOURTHOUSAND.getValue());
break;
case 12:
System.out.println("Congratulations!!! you have won " + Earnings.ONETWENTYFIVETHOUSAND.getValue());
break;
case 13:
System.out.println("Congratulations!!! you have won " + Earnings.TWOFIFTYTHOUSAND.getValue());
break;
case 14:
System.out.println("Congratulations!!! you have won " + Earnings.FIVEHUNDREDTHOUSAND.getValue());
break;
case 15:
System.out.println("Congratulations!!! you have won " + Earnings.ONEMILLION.getValue());
break;
default:
System.out.println("Unfortunately, you have won nothing !!!");
break;
}
return this.getScore();
}
public void storeUserHighscore(String name, int score)
{
System.out.println("This is getUser in storeUserHighscore method" + namee);
name = this.getUser();
score = this.getScore();
storeDetails.put(name, score);
System.out.println(storeDetails);
}
/**
* @param score the score to set
*/
public void setScore(Integer score) {
this.scores = score;
}
}
Here is the output
Before we begin what is your name?
John
This is getUser in storeUser method John
Goodluck to you John on your quest to win a million dollars
What does 'NFL' stand for
a) National Food League
b) National Federation League
c) National Football League
d) National Fighting League
a
Unfortunately this answer is wrong
Unfortunately, you have won nothing !!!
Thank you for playing
This is getUser in storeUserHighscore method null
{null=-5}
Here is where storeUserHighscore is called
public class GameMethods
{
int count = 0;
String name = "";
int score = 0;
public void reviewUserInput(String input, String answer, HashMap <Integer, LifeLines> lifelines)
{
User getUser = new User();
// System.out.println(input);
GameMethods getGameMethods = new GameMethods();
boolean loop = true;
if (input.equalsIgnoreCase("x"))
{
System.exit(0);
}
else if (input.equalsIgnoreCase("l"))
{
input = getGameMethods.hasLifelines(lifelines, answer);
}
else if (!input.equalsIgnoreCase("a") && !input.equalsIgnoreCase("b") && !input.equalsIgnoreCase("c") && !input.equalsIgnoreCase("d"))
{
while(loop)
{
System.out.println("Invalid Input try again!!");
input = getGameMethods.getUserInput();
if (input.equalsIgnoreCase("a") || input.equalsIgnoreCase("b") || input.equalsIgnoreCase("c") || input.equalsIgnoreCase("d") || input.equalsIgnoreCase("l") || input.equalsIgnoreCase("x"))
{
loop = false;
if (input.equalsIgnoreCase("x"))
{
System.exit(0);
}
else if (input.equalsIgnoreCase("l"))
{
// input = " ";
input = getGameMethods.hasLifelines(lifelines, answer);
}
}
}
}
// System.out.println("this is ans " +answer);
// System.out.println("This is user " +input);
if (input.equalsIgnoreCase(answer))
{
count++;
System.out.println("Correct answer!!!");
getUser.setUserEarnings(count);
}
else if (!input.equalsIgnoreCase(answer))
{
count-=5;
System.out.println("Unfortunately this answer is wrong");
getUser.setUserEarnings(count);
System.out.println("Thank you for playing");
getUser.storeUserHighscore(name, score);
System.exit(0);
}
}
}
also here is the main method
public class Game
{
public static LifeLines lifeLine ;
public static void main(String[] args)
{
String answer;
String userInput;
HashMap <Integer, LifeLines> lifelines = new HashMap <> ();
lifelines.put(0, lifeLine);
lifelines.put(1, lifeLine);
lifelines.put(2, lifeLine);
GameMethods getGameMethods = new GameMethods();
InputOutput getInputMethods = new InputOutput();
User getUserMethods = new User();
getInputMethods.readIntroduction();
getInputMethods.clearBuffer();
getUserMethods.storeUser();
for(int counter = 0; counter < 16; counter++)
{
getInputMethods.readQuestion();
getInputMethods.readA();
getInputMethods.readB();
getInputMethods.readC();
getInputMethods.readD();
answer = getInputMethods.readAnswer();
userInput = getGameMethods.getUserInput();
getGameMethods.reviewUserInput(userInput, answer, lifelines);
}
}
}
Solution
You are creating a new User User getUser = new User();
object in reviewUserInput(String input, String answer..)
of GameMethods
, and you are not setting any name to that newly created User
object, that's why you are getting null
value for the name.
To solve this issue you can add another parameter(User
) to this method reviewUserInput()
. like this,
public void reviewUserInput(User user, String input, String answer, HashMap <Integer, LifeLines> lifelines);
call this method from this getGameMethods.reviewUserInput(userInput, ...);
method in Game
class. like this,
getGameMethods.reviewUserInput(getUserMethods, userInput, answer, lifelines);
So, you can use the user
object in this method reviewUserInput()
so, you won't get any null
here, you will get the acutal name (John) here.
Answered By - Mahesh
Answer Checked By - Candace Johnson (JavaFixing Volunteer)