*
The moose likes Game Development and the fly likes Adding InputHandler to game throws a java.lang.nullPointerException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Game Development
Bookmark "Adding InputHandler to game throws a java.lang.nullPointerException" Watch "Adding InputHandler to game throws a java.lang.nullPointerException" New topic
Author

Adding InputHandler to game throws a java.lang.nullPointerException

Adam Miszczak
Greenhorn

Joined: Sep 24, 2012
Posts: 24
Hi!

I have a game, and I just added a InputHandler class. The problem is that whenever I run my game, I get a java.lang.nullPointerException in my Player class where the if statement for checking if the key is pressed.

Game



InputHandler



Player



I have tried lots of things but I just cannot figure this out. It's probably something that I just overlooked and can't find. Help is appreciated. Thanks!

This is the error message:

fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11320
    
  16

I don't see where you ever create an InputHandler.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Adam Miszczak
Greenhorn

Joined: Sep 24, 2012
Posts: 24
I'm not really sure what you mean by creating it? I have an InputHandler class.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11320
    
  16

Yes, you have the class defined...but that's like saying "I drafted the blueprint..what do you mean I don't have a house?". Unless you create an InputHandler object, you don't have one. You need to call it's constructor.

Put it another way...what do you think your variable 'input' holds? You declare it:

you pass it to your player class:

you assign it to a another variable named input:

and you try to use it:

Where do you assign it a value?
Adam Miszczak
Greenhorn

Joined: Sep 24, 2012
Posts: 24
Ok I understand now. Thanks!
 
 
subject: Adding InputHandler to game throws a java.lang.nullPointerException