Sorry if this is a stupid question, but I'm very new to java. Basically we are learning about interfaces and maps right now in my java class. I created an interface that is really simple, and have also created other classes that implement that interface. In my class that is a main actor, I am creating a map and I've created instances of these as you can see below. I'm getting a NPE and I am not sure why.
private Scanner input;
private Map<String, Command> map;
//~ Constructors ..........................................................
// ----------------------------------------------------------
/**
* Creates a new Rocket object that reads commands
* from a predefined URL (the URL is in the lab assignment).
*/
public Rocket()
{
this(IOHelper.createScannerForURL(
"http://courses.cs.vt.edu/~cs1114/Fall2012/rocket-commands.txt")
);
}
// ----------------------------------------------------------
/**
* Creates a new Rocket object that reads commands
* from the given scanner.
* @param scanner The scanner to read commands from.
*/
public Rocket(Scanner scanner)
{
input = scanner;
map = new HashMap<String, Command>();
ForwardCommand forward = new ForwardCommand(this);
map.put("forward", forward);
LeftCommand left = new LeftCommand(this);
map.put("left", left);
RightCommand right = new RightCommand(this);
map.put("right", right);
}
//~ Methods ...............................................................
// ----------------------------------------------------------
/**
* Reads one word from the scanner (if there is any), and executes
* the corresponding command. If the scanner has no words remaining,
* then nothing happens.
*/
public void act()
{
if (input.hasNext())
{
String word = input.next();
map.get(word).execute();
}
}
}
The error is coming on the line map.get(word).execute();
I'm sure it is something obvious, but I am very new to coding so any help would be appreciated!