0
while(true)
        {
            String input = "";
            try {
                input = in.readLine();

            } catch (IOException e1) {
                // TODO Auto-generated catch block
                System.out.println(e1 + "Exception occured when reading user input");
            }
            // Sleep
            try
            {
                Thread.sleep(USER_THROTTLE);
            }
            catch(Exception e)
            {
                System.out.println(toString()+" has input interrupted.");
            }

            if(input .equals("w")){action_event(input);}
            if(input .equals("a")){action_event(input);}
            if(input .equals("s")){action_event(input);}
            if(input .equals("d")){action_event(input);}

            if(input .equals("eat")){action_event(input);}
            if(input .equals("drink")){action_event(input);}
            if(input .equals("place")){action_event(input);}
            if(input .equals("swim")){action_event(input);}
            if(input .equals("command_kill")){action_event(input);}
            if(input .equals("help")){action_event(input);}
        }
    }

Here is the stack trace

Exception in thread "Thread-1" java.lang.NullPointerException
at Platypus_User$Inport.run(Platypus_User.java:64)

This is being ran in Eclipse on Mac OSX. A Null Pointer Exception occurs following the second catch block where the string is compared to "w" then if it is "w" the action_event method is called.

I have no clue why this would be happening. I would appreciate any advice.

3
  • 3
    Post the full stracktrace. Always post the full stacktrace. Commented Apr 21, 2015 at 16:09
  • 3
    Also, some advice: when you need to write something like this: if(input .equals("w")), it might be better to write it as: if ("w".equals(input)) since it will never be possible for this version to throw a NPE. Commented Apr 21, 2015 at 16:11
  • @Stultuske better use Set#contains() though such code repetitions make code look dirty Commented Apr 21, 2015 at 16:30

4 Answers 4

3

I guess in is a BufferedReader. readLine will return null if End Of The Stream is reached.

See BufferedReader documentation

Sign up to request clarification or add additional context in comments.

2 Comments

Yes "in" is a Buffered Reader. How should I read user input then ?
You should just check if input == null, and skip the checks
1

BufferedReader.readLine() can return null, so check for null on input.

Comments

0

First, avoid repetitions in code. You can collect all allowed inputs in one Set and then just check if it contains particular value--thus making code look much more readable, short and clean.

Second, you need to perform null check, because in.read() can return null as mentioned in other answers. null input could also be used for termination of while loop.

So I would rewrite your code as following:

Set<String> allowedInputs 
    = new HashSet<>(Arrays.asList("w", "a", "s", "d", "eat")); // <- add remaining allowed inputs here

String input = "";

while (input != null) {    
    try {
        input = in.readLine();
    } catch (IOException e1) {
        System.out.println(e1 + "Exception occured when reading user input");
    }
    try {
        Thread.sleep(USER_THROTTLE);
    } catch(Exception e) {
        System.out.println(toString() + " has input interrupted.");
    }
    if (input != null && allowedInputs.contains(input)) { // <- check if input is allowed
         action_event(input);
    }
}

Comments

0

Never invoke methods on objects which are not initialized. input may hold a null value. So before comparing you must make sure that object is not null. Best approach to handle such situations would be by comparing a constant value with "input" instead of comparing input with a constant value. eg "w".equals(input)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.