Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

trouble with mouseEvents

 
mary caldwell
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone,
I'm pretty new to java so I would very much appreciate any and all help.

I'm working on a program to play a game where the user needs to be able to move an object on the screen by moving the mouse. I've spent a while trying to figure out why my code is not working... it throws a null pointer exception error. I've posted my code below (leaving out lots of constants)... thanks very much in advance!



[UD: added linebreak to preserve layout ]
[ October 17, 2008: Message edited by: Ulf Dittmer ]
 
Iván Párraga
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you post the full error stack?

Cheers,

Iv�n
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24213
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by mary confused:
//public GRect paddle;

// here is where i'm having trouble - if i don't initialize
//"paddle" in the line above, it says paddle cannot be resolved,
// even though i add paddle in drawPaddle(). and if i do initialize
// it, then it says paddle returns null. ?

public void mouseMoved(MouseEvent e) {
paddle.setLocation(e.getX(), HEIGHT-PADDLE_Y_OFFSET);
}


Hi,

Welcome to JavaRanch!

The problem is that you don't initialize "paddle", even if you include that line; all you do is declare it. Declaring a variable makes the name meaningful, but it doesn't create an object. You want a GRect for the "paddle" variable to refer to, so somewhere, at some point, you need

paddle = new GRect(...);

and this has to happen before you can call paddle.setLocation(), or you'll get that NullPointerException (which is telling you, in Java-ese, that paddle isn't refering to any object.)
 
mary caldwell
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you so much! the clarification between declaring and initializing helped a lot. cheers!
 
Campbell Ritchie
Marshal
Posts: 52636
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Everybody does that sort of thing, so think nothing of it.
And welcome to JavaRanch, Mary Caldwell
 
We're all out of roofs. But we still have tiny ads:
the new thread boost feature: great for the advertiser and smooth for the coderanch user
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!