Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Regular Coder
    Join Date
    Dec 2003
    Posts
    132
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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

  • #2
    Regular Coder
    Join Date
    Dec 2003
    Posts
    367
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #3
    Regular Coder
    Join Date
    Dec 2003
    Posts
    132
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #4
    Regular Coder
    Join Date
    Feb 2004
    Location
    WA
    Posts
    213
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #5
    Regular Coder
    Join Date
    Dec 2003
    Posts
    132
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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?

  • #6
    Regular Coder
    Join Date
    Dec 2003
    Posts
    367
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •