Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: Getting "AWT-EventQueue-0" java.lang.NullPointerException error

  1. #1
    Junior Member
    Join Date
    Sep 2009
    Posts
    8
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Getting "AWT-EventQueue-0" java.lang.NullPointerException error

    Here is the error I am getting:
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at Main_Box_Game.mouseEntered(Main_Box_Game.java:149)
    at java.awt.AWTEventMulticaster.mouseEntered(AWTEvent Multicaster.java:283)
    at java.awt.Component.processMouseEvent(Component.jav a:6310)
    at java.awt.Component.processEvent(Component.java:606 6)
    at java.awt.Container.processEvent(Container.java:208 5)
    at java.awt.Component.dispatchEventImpl(Component.jav a:4667)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2143)
    at java.awt.Component.dispatchEvent(Component.java:44 97)
    at java.awt.LightweightDispatcher.retargetMouseEvent( Container.java:4600)
    at java.awt.LightweightDispatcher.trackMouseEnterExit (Container.java:4389)
    at java.awt.LightweightDispatcher.processMouseEvent(C ontainer.java:4246)
    at java.awt.LightweightDispatcher.dispatchEvent(Conta iner.java:4194)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2129)
    at java.awt.Window.dispatchEventImpl(Window.java:2475 )
    at java.awt.Component.dispatchEvent(Component.java:44 97)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java: 635)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:296)
    at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:211)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:201)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:196)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:188)
    at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:122)
    Here is my code:

    import acm.graphics.*;
    import acm.program.*;
    import java.awt.event.*;
    import java.awt.Color;
    import java.awt.*;
    import student.*;
    import java.util.*;
    import acm.util.RandomGenerator;
     
    // -------------------------------------------------------------------------
    /**
     *  This is a game where you will attempt to click inside boxes of varying size 
     *  and will be given different point totals depending on the size of box you hit. the smaller the
     *  box the more points. If you do not hit a box you wil receive and error message
     * 
     *  @author  
     *  @version (2009.09.18)
     */
    public class Main_Box_Game extends GraphicsProgram
    {
        //~ Instance/static variables .............................................
            private RandomGenerator rgen = RandomGenerator.getInstance();
            private GLabel label;
            private GLabel label1;
            private GLabel label2;
            private GLabel label3;
            private GRect rect;
            private GRect rect_M;
            private GRect rect_B;
            private int SCORE, points, clicks, misses, games;
            private boolean largeBoxHit, medBoxHit, smallBoxHit;
     
     
     
     
     
     
     
     
     
     
     
        //~ Constructor ...........................................................
     
        // ----------------------------------------------------------
        /**
         * Creates a new Invisible_Box_Game object.
         */
     
     
     
     
        //~ Methods ...............................................................
     
        public void init() {
     
            addMouseListeners();
            label = new GLabel("Click mouse to hit box and be awarded points or lose 1 point for every miss. If you hit all 3 boxes", 10, 50 );
            label1 = new GLabel("receive 200, but beware they are different sizes. Small, med, little. If you hit the med and small get", 10, 150);
            label2 = new GLabel("150 points, 125 for large and small, 100 for just the small 75 for med, and 50 for large only.you do not", 10, 250);
            label3 = new GLabel("get more points added hitting the boxes more than once, and you can play as many times you want. have fun", 10, 350);
            add (label);
            add (label1);
            add (label2);
            add (label3);
            pause(3500);
            label.setLabel ("");
            label1.setLabel ("");
            label2.setLabel ("");
            label3.setLabel ("");
            rect1();
            add (rect) ;
            rect2();
            add (rect_M);
            rect3();
            add (rect_B);
           largeBoxHit = false;
           medBoxHit = false;
           smallBoxHit= false;
            SCORE= 0;
           clicks= 0;
           misses= -1;
           games= 0;
        } 
     
     
     
     
     
       public void rect1()
            {
               Random gen = new Random() ;
               int smallx = gen.nextInt(470) ;
               int smally= gen.nextInt(470);
               rect = new GRect (30, 30, 30, 30);
               rect.setLocation (smallx, smally); 
               rect.setFilled(true);
               rect.setFillColor (Color.GREEN);
            } 
     
     
     
        public void rect2()
            {
     
               Random gen1 = new Random() ;
               int mediumx = gen1.nextInt(455) ;
               int mediumy= gen1.nextInt(455);
               rect_M = new GRect (45, 45, 45,45) ;
               rect_M.setLocation (mediumx, mediumy);
               rect_M.setFilled(true);
               rect_M.setFillColor (Color.BLUE);
            }  
     
     
     
     
        public void rect3()
            {   
              Random gen2 = new Random() ;
               int bigx = gen2.nextInt(420) ;
               int bigy= gen2.nextInt(420);
              rect_B = new GRect (80, 80, 80,80) ;
               rect_B.setLocation (bigx, bigy);
               rect_B.setFilled(true);
               rect_B.setFillColor (Color.RED);
     
            }
     
     
     
        public void mouseExited(MouseEvent e)
          {
            rect.setVisible (true);
            rect_M.setVisible(true);
            rect_B.setVisible(true);
            games= games + 1;
            println ("games played" + games + ".");  
            println ("Your score is " + SCORE + ".");
            println ("Your clicks are " + clicks + ".");
     
     
          }  
     
     
        public void mouseEntered(MouseEvent e)
              {
     
    Line149**         [COLOR="Red"]   rect.setVisible(false);[/COLOR]
                rect_M.setVisible(false);
                rect_B.setVisible(false);
                SCORE=0;
                clicks=0;
                largeBoxHit = false;
                medBoxHit = false;
                smallBoxHit= false;
                misses=0;
     
             }
     
     
       public void mouseDragged(MouseEvent e)
        {
            rect.setVisible(true);
            rect_M.setVisible(true);
            rect_B.setVisible(true);
        }
     
     
        public void mouseClicked(MouseEvent e)
        {
     
     
          clicks++;
          misses--;
     
         if(rect.contains(e.getX(), e.getY()))
     
          {
               smallBoxHit = true;
           }
     
          if(rect_M.contains(e.getX(), e.getY())) 
     
           {
               medBoxHit = true;
           }
     
          if(rect_B.contains(e.getX(), e.getY())) 
     
          {
               largeBoxHit = true;
          }
            if(largeBoxHit && medBoxHit && smallBoxHit)
           SCORE= 200 + misses;
            else if(medBoxHit && smallBoxHit)
           SCORE= 150+ misses;
     
           else if(largeBoxHit && smallBoxHit)
           SCORE= 125+ misses;
     
           else if(largeBoxHit && medBoxHit)
           SCORE = 110+ misses;
     
           else if (smallBoxHit)
           SCORE=  100 + misses;
           else if (medBoxHit)
           SCORE= 75+ misses;
           else if(largeBoxHit)
           SCORE= 50+ misses;
           else{
           SCORE= misses + 1;
           }
     
        }   
     
    }
    Last edited by Freaky Chris; September 21st, 2009 at 11:16 PM.


  2. #2
    Junior Member
    Join Date
    Sep 2009
    Posts
    8
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Getting "AWT-EventQueue-0" java.lang.NullPointerException error

    I have no idea what the issue is or where to begin. Any help would be greatly appreciated

  3. #3
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Location
    Wales, Bangor & England, Warwickshire
    Posts
    820
    My Mood
    Cynical
    Thanks
    7
    Thanked 104 Times in 90 Posts

    Default Re: Getting "AWT-EventQueue-0" java.lang.NullPointerException error

    When do you get this error?

    Chris
    chris[at]javaprogrammingforums[dot]com

    Prifysgol Bangor University, North Wales

  4. The Following User Says Thank You to Freaky Chris For This Useful Post:

    tryingtoJava (September 21st, 2009)

  5. #4
    Junior Member
    Join Date
    Sep 2009
    Posts
    8
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Getting "AWT-EventQueue-0" java.lang.NullPointerException error

    Quote Originally Posted by Freaky Chris View Post
    When do you get this error?

    Chris

    Hey Chris, thanks for hitting me back. It comes when I run the program, and behind the screen where my game in being played another window opens up called the BlueJ terminal, and that has my printouts for score, games, clicks- etc. Under where it has that info though is the red stuff I posted for the eror. would you like to see a screen shot?

  6. #5
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Location
    Wales, Bangor & England, Warwickshire
    Posts
    820
    My Mood
    Cynical
    Thanks
    7
    Thanked 104 Times in 90 Posts

    Default Re: Getting "AWT-EventQueue-0" java.lang.NullPointerException error

    No, i'm just wondering. If it is as soon as you run the program then it could be that mouse_Entered() is being triggered before rect has been assigned
    chris[at]javaprogrammingforums[dot]com

    Prifysgol Bangor University, North Wales

  7. The Following User Says Thank You to Freaky Chris For This Useful Post:

    tryingtoJava (September 21st, 2009)

  8. #6
    Junior Member
    Join Date
    Sep 2009
    Posts
    8
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Getting "AWT-EventQueue-0" java.lang.NullPointerException error

    Yeah basically it is as soon as I run it. Hmmm, is there a way to prevent that? Should I move the mouse listener statement under init or??

  9. #7
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Location
    Wales, Bangor & England, Warwickshire
    Posts
    820
    My Mood
    Cynical
    Thanks
    7
    Thanked 104 Times in 90 Posts

    Default Re: Getting "AWT-EventQueue-0" java.lang.NullPointerException error

    Ye try adding the mouse listeners after all the init() is run

    Chris
    chris[at]javaprogrammingforums[dot]com

    Prifysgol Bangor University, North Wales

  10. The Following User Says Thank You to Freaky Chris For This Useful Post:

    tryingtoJava (September 21st, 2009)

  11. #8
    Junior Member
    Join Date
    Sep 2009
    Posts
    8
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Getting "AWT-EventQueue-0" java.lang.NullPointerException error

    Quote Originally Posted by Freaky Chris View Post
    Ye try adding the mouse listeners after all the init() is run

    Chris
    Yeah I put that at the bottom and that seems to have done it. The printout still comes on the other screen, but no errors the last few times I have done it. Thanks bro

  12. #9
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Location
    Wales, Bangor & England, Warwickshire
    Posts
    820
    My Mood
    Cynical
    Thanks
    7
    Thanked 104 Times in 90 Posts

    Default Re: Getting "AWT-EventQueue-0" java.lang.NullPointerException error

    Brilliant, can you mark this as solved please.

    Time to sleep its 4.40am

    Regards,
    Chris
    chris[at]javaprogrammingforums[dot]com

    Prifysgol Bangor University, North Wales

  13. #10
    Junior Member
    Join Date
    Sep 2009
    Posts
    8
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Getting "AWT-EventQueue-0" java.lang.NullPointerException error

    Yes sir. I had no idea the order in init mattered. Now I know better.

Similar Threads

  1. Replies: 16
    Last Post: August 27th, 2010, 04:30 PM
  2. Replies: 2
    Last Post: March 23rd, 2010, 02:38 AM
  3. Replies: 4
    Last Post: August 13th, 2009, 06:54 AM
  4. Replies: 4
    Last Post: June 18th, 2009, 10:23 AM
  5. [SOLVED] "GridLayout" problem in Java program
    By antitru5t in forum AWT / Java Swing
    Replies: 3
    Last Post: April 16th, 2009, 11:26 AM