aspose file tools*
The moose likes Beginning Java and the fly likes trouble with mouseEvents Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "trouble with mouseEvents" Watch "trouble with mouseEvents" New topic
Author

trouble with mouseEvents

mary caldwell
Greenhorn

Joined: Oct 16, 2008
Posts: 9
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

Joined: Dec 02, 2007
Posts: 54
Can you post the full error stack?

Cheers,

Iv�n


Iván Párraga García
SCWCD 5, SCJD, SCJP 5, MySQL 5 DBA
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

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.)


[Jess in Action][AskingGoodQuestions]
mary caldwell
Greenhorn

Joined: Oct 16, 2008
Posts: 9
thank you so much! the clarification between declaring and initializing helped a lot. cheers!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39395
    
  28
Everybody does that sort of thing, so think nothing of it.
And welcome to JavaRanch, Mary Caldwell
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: trouble with mouseEvents