0

To make sure this doesn't get closed, read this. This isn't a duplicate post because the only other user input setter is in C or c something and if not that it's for a completly different application. How can I set up my setWord method to use user input and not be null. My current code gives off a null pointer because the variable is null, but I can't find out a viable way to set it's value using user input. Current code: Subclass: package hangman;

public class Hangman {

private String word;

public void setWord(String word) {
    this.word = toString();
}

public String getWord() {
    return this.word;
}

@Override
public String toString() {
    System.out.println("Enter secret word: ");
    return (this.getWord());   
}

}

Main

public static void main(String[] args) {
Hangman hangman = new Hangman();
hangman.setWord();
String secretWord = hangman.getWord();
StringBuilder b = new StringBuilder(secretWord.length());
}

Again, the issue is that I can't find a way to set the private String "word" to user input without it ending up being null. Please dont mark this as duplicate I already looked at the generic cookie cutter nullpointerexception threads but haven't helped me at all. I've been stuck on this and it's my last part of my program. The null pointer is always at the stringbuilder, which suggests that secretWord is null.

anon
  • 11
  • 2
  • What is the full NPE trace? Where do you initialize `hangman`? – Carcigenicate Jun 05 '18 at 22:35
  • Sorry I tried making a profram as someone suggested, i'll edit to include other details. Updated – anon Jun 05 '18 at 22:36
  • And again, what's the NPE trace? – Carcigenicate Jun 05 '18 at 22:38
  • A trivial cause for the NPE is that your program **nowhere** attempts to read **anything** - and essentially bites itself in the tail: setword calls tostring that calls getword, but word isn't touched anywhere. Then, overriding toString with code totally unrelated to what toString is supposed to do in Java is a very very bad idea. And lastly, I don't feel a "setter" should do user IO. – fvu Jun 05 '18 at 22:39
  • Oh ya, I guess it would be `secretWord.length()` causing it, so this *is* a duplicate. You never initialize `this.word`. You try to in `setWord`, but you assign it the result of `getWord`, but if `this.word` wasn't set, that will return `null`. – Carcigenicate Jun 05 '18 at 22:40
  • The NPE trace is at the stringbuilders line. – anon Jun 05 '18 at 22:41
  • The code as given doesn't compile, so you can't be getting that error. `setWord` takes an argument, which you don't give in `main`. Change `setWord` to `public void setWord(String word) { this.word = word; }`, and give it a word that you want to set it to. – Carcigenicate Jun 05 '18 at 22:43
  • To phrase it another way, what word are you expecting `secretWord` to be? You never write a word anywhere to set it to. – Carcigenicate Jun 05 '18 at 22:46
  • I am expecting it to call the toString method when the setword is called and then have the user input be set to the word and then using the getWord method in main to set it as a variable in the main. – anon Jun 05 '18 at 22:48
  • Based on what you said would a scanner in that new method you wrote work instead of the toString? I will try it now – anon Jun 05 '18 at 22:49

2 Answers2

0

Think about it: where do you ever set the word? setWord assigns the return of toString, which returns the result of getWord, but getWord just returns what this.word already was! Nowhere do you ever set a word, so it's never initialized! You just set this.word to what it was originally, which was null. This causes an NPE when you call secretWord.length(), since secretWord is null.

You have another problem, which I'm assuming is a typo here, where your call to setWord in your main isn't given an argument. That's illegal and creates an error of its own, so it should never reach the StringBuilder line.

Change setWord to:

public void setWord(String word) {
    this.word = word; ; Set it to its argument
}

Then call it with a word as the argument:

hangman.setWord("Word");
Carcigenicate
  • 43,494
  • 9
  • 68
  • 117
  • I fixed it by making a scanner in the setWord method to get input instead of a toString. – anon Jun 05 '18 at 22:55
  • That will fix it too, but it's a bad idea. Asking for user input inside of a setter is bad, and defeats the purpose of the `String word` argument. Ask for the word outside of `setWord`, pass the word in, then assign it there. You're going to make all you code harder to use if every method is attempting to manually ask for input itself. – Carcigenicate Jun 05 '18 at 22:57
  • I edited my code to have a inputword method and has String word = the scanner next line but then when I call the setword method in main I get the error that it requires a String but found no arguements – anon Jun 05 '18 at 23:05
  • @anon Again, you need to give the string to `setWord`, like (for example): `hangman.setWord("Word");`, where `"Word"` is what the scanner gives you. – Carcigenicate Jun 05 '18 at 23:06
  • Thanks for the help, but just for the future how would it be harder to use like that – anon Jun 05 '18 at 23:11
  • Its harder to use when you aren't passing the data in because then you're forced to take the input from the user's keyboard, and you're forced to manually type it. What if you ever wanted to get the word from the internet? Or from file? Or what if during testing you want to give a ton of different words to your program and see what happens? If you wanted to test your program against a few thousand words, are you going to sit there and manually type each word by hand? The sane way would be to load the words from a file, and have the program loop over each word automatically. No typing required. – Carcigenicate Jun 05 '18 at 23:13
0

Originally I had a toString which served no functional purpose. And the setWord method required a parameter which I didn't have so to fix it I replaced the setWord code with this

 public void setWord() {
        Scanner scan = new Scanner(System.in);
        System.out.println("Enter the secret word: (Under 7 letters)");
        this.word = scan.nextLine();
        this.word = word; 
    }
anon
  • 11
  • 2