• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

Adding InputHandler to game throws a java.lang.nullPointerException

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:

 
lowercase baba
Posts: 12911
64
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't see where you ever create an InputHandler.
 
Adam Miszczak
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not really sure what you mean by creating it? I have an InputHandler class.
 
fred rosenberger
lowercase baba
Posts: 12911
64
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok I understand now. Thanks!
 
Would you like to try a free sample? Today we are featuring tiny ads:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic