I put both classes in the same file, changed their names, compiled, ran and got this in the console:
C:\jexp>javac playrx.java
C:\jexp>java PlayRx
Exception in thread "main" java.lang.NullPointerException
at ShowRx.guess(playrx.java:69)
at PlayRx.main(playrx.java:20)
Count down to line 69 to find
if ( !guess.equals(strings[currentString]) &&
strings[currentString].contains(guess) )
Something in this line was null. It is a member variable which was hidden/shadowed by a local variable.
class ShowRx
{
// Member variable declaration.
// Member variables have class scope.
private String[] strings;
...
public ShowRx()
{
// Local variable of same name is declared and instantiated.
// This hides/shadows the member variable with same name and
// its (the member variable) value remains null. The scope of
// this local variable is within the curley braces of this
// constructor, ie, no one outside the constructor can see
// this local variable.
String[] strings = {
"cat", "dog", "mouse", "bear", "moose", "bird",
"frog", "fish", "slug", "emu"
};
How to fix this?
private String[] strings;
...
public ShowRx()
{
// Instantiate the member variable.
strings = new String[] {
"cat", "dog", "mouse", "bear", "moose", "bird",
"frog", "fish", "slug", "emu"
};
Now everyone in ShowRx can see "strings" and it has been instantiated and its value is therefore non–null.