This week's book giveaway is in the Spring forum.
We're giving away four copies of Spring Boot in Practice and have Somnath Musib on-line!
See this thread for details.
Win a copy of Spring Boot in Practice this week in the Spring forum!
  • 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

trouble with mouseEvents

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you post the full error stack?

Cheers,

Iv�n
 
author and iconoclast
Posts: 24204
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you so much! the clarification between declaring and initializing helped a lot. cheers!
 
Marshal
Posts: 76113
362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Everybody does that sort of thing, so think nothing of it.
And welcome to JavaRanch, Mary Caldwell
 
reply
    Bookmark Topic Watch Topic
  • New Topic