Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 05-20-2004, 10:39 PM   PM User | #1
warhammerdude20
Regular Coder

 
Join Date: Dec 2003
Posts: 132
Thanks: 0
Thanked 0 Times in 0 Posts
warhammerdude20 is an unknown quantity at this point
java mousePressed() question

Hey, I have this applet that starts as soon as the page is loaded. but i wanted it so you have to click the applet before it starts(getting keyboard focus) or else its just a race to click the applet before you die... so i tryed the following:

Code:
public class Whatever implements Runnable,KeyListener,MouseListener{
...
Graphics begin;
Thread th;
...
public void init(){
 ...
 begin.drawString("click to start",100,50);
 addMouseListener(this);
}
...
public void mousePressed(MouseEvent e){
 begin.dispose();
 th.start();
}
and i got a NullPointerException... whatever that is. so everything else works fine.

could anyone tell me a better way to go about this or a way to fix it?
thanks
warhammerdude20 is offline   Reply With Quote
Old 05-21-2004, 08:11 AM   PM User | #2
shmoove
Regular Coder

 
Join Date: Dec 2003
Posts: 367
Thanks: 0
Thanked 0 Times in 0 Posts
shmoove is an unknown quantity at this point
NullPointerException means that you're trying to use an object that is null, ie, you haven't "new"-ed it.
You're not showing all of the code, and you didn't say when exactly you're getting the exception (immediately, or when you click the applet with the mouse?). Are you sure you created th, because I don't see that in the code, and if you didn't then th.start() would throw an NullPointerException because th is null.

shmoove
shmoove is offline   Reply With Quote
Old 05-21-2004, 06:06 PM   PM User | #3
warhammerdude20
Regular Coder

 
Join Date: Dec 2003
Posts: 132
Thanks: 0
Thanked 0 Times in 0 Posts
warhammerdude20 is an unknown quantity at this point
hey,the whole code is pretty big...

i just put in the things that actually matter, everything that you dont see works fine.

and im getting the NullPointerException at the beggining when i run the applet, but it compiles fine.
warhammerdude20 is offline   Reply With Quote
Old 05-21-2004, 06:41 PM   PM User | #4
Unit
Regular Coder

 
Join Date: Feb 2004
Location: WA
Posts: 213
Thanks: 0
Thanked 0 Times in 0 Posts
Unit is an unknown quantity at this point
If it happens before you even press the mouse, then its happening somewhere else and not in the code you just showed us. I am not familiar with java, but did you initialize the Graphics object "begin"? a listing of the init() might help others figure out your problem.
__________________
Nobody is Perfect. I am Nobody.
Unit is offline   Reply With Quote
Old 05-22-2004, 02:03 AM   PM User | #5
warhammerdude20
Regular Coder

 
Join Date: Dec 2003
Posts: 132
Thanks: 0
Thanked 0 Times in 0 Posts
warhammerdude20 is an unknown quantity at this point
well, i did initalize 'begin'. should i just post all the code or put it in an attachment? I just want to know how it could be done, the clicking then beggining. am i going about it the right way?
warhammerdude20 is offline   Reply With Quote
Old 05-23-2004, 08:35 AM   PM User | #6
shmoove
Regular Coder

 
Join Date: Dec 2003
Posts: 367
Thanks: 0
Thanked 0 Times in 0 Posts
shmoove is an unknown quantity at this point
Wrap all the functions that might get called up at the beginning in try..catch blocks, and print the stack trace when you catch an exception:
Code:
void someFunctionThatGetsCalledAtTheBeginning() {
  try {
    // function body...
  }
  catch (Exception e) {
    System.out.println("Exception in someFunctionThatGetsCalledAtTheBeginning() : ");
    e.printStackTrace();
  }
}
This will give you a pretty good idea of what line exactly is causing the exception.

shmoove
shmoove is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:19 AM.


Advertisement
Log in to turn off these ads.