nullPointerException with enums
<p>I'm coding a game where the player has to type something in the dialog field, and java has to interpret it as an action. I have a class named GameEngine where i use a switch :</p>

<pre><code>public void interpretCommand(final String commandLine) 
{
    Command command = parser.getCommand(commandLine);
    CommandWord commandWord = command.getCommandWord();
    switch ( commandWord ) {
      case UNKNOWN: 
         gui.println("Unknown command...");
         return;
      case LOAD: 
         load(); 
         break;
    } 
}
</code></pre>

<p>Enums used here are in an enumclass :</p>

<pre><code>public enum CommandWord
{
    LOAD("charger"),UNKNOWN("?");
    private String commandWord;
 }
</code></pre>

<p>and finally, the CommandWords class :</p>

<pre><code>public class CommandWords
{
    // a HashMap that holds all valid command words
   private HashMap&lt;String, CommandWord&gt; validCommands; 

   public CommandWords()
   {
      validCommands = new HashMap&lt;String, CommandWord&gt;();
      for(CommandWord command : CommandWord.values()) {
          if(command != CommandWord.UNKNOWN){
              validCommands.put(command.toString(), command);
          }
      }
   }


   /**
     * @param commandWord the command word to look for in the valid commands
     * @return the commandWord associated with the word typed
     */
    public CommandWord getCommandWord(final String commandWord)
    {
        CommandWord command =validCommands.get(commandWord);
        if(command !=null) {
           return command;
        }
        else {
           return CommandWord.UNKNOWN;
        }
    }
</code></pre>

<p>the Command class :</p>

<pre><code>public class Command
{
    private CommandWord commandWord;

    /**
     * @param commandWord from enum class
     */
    public Command(CommandWord commandWord)
    {
        this.commandWord = commandWord;
    }

    public CommandWord getCommandWord()
    {
        return commandWord;
    }
}
</code></pre>

<p>My problem is very simple: when I type charger, it works, but when I type a random word, not in the list, I get a <code>NullPointerException</code> in terminal at the line </p>

<pre><code>        switch ( commandWord )
</code></pre>

<p>in interpretCommand.</p>

<p>I can't see what's wrong with my code. If someone could help, thank you.
Please don't blame me, I'm a novice, I just started a few months ago!</p>

<p>Solution : in command modify/add these lines</p>

<pre><code>    public boolean isUnknown()
{
    return (commandWord == null);
}

    public CommandWord getCommandWord()
{
    if(!isUnknown()){

    return commandWord;

}
 else return CommandWord.UNKNOWN;
}
</code></pre>
