Getting "AWT-EventQueue-0" java.lang.NullPointerException error
Here is the error I am getting:
Quote:
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:
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;
}
}
}
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
Re: Getting "AWT-EventQueue-0" java.lang.NullPointerException error
When do you get this error?
Chris
Re: Getting "AWT-EventQueue-0" java.lang.NullPointerException error
Quote:
Originally Posted by
Freaky Chris
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?
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
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??
Re: Getting "AWT-EventQueue-0" java.lang.NullPointerException error
Ye try adding the mouse listeners after all the init() is run
Chris
Re: Getting "AWT-EventQueue-0" java.lang.NullPointerException error
Quote:
Originally Posted by
Freaky Chris
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
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
Re: Getting "AWT-EventQueue-0" java.lang.NullPointerException error
Yes sir. I had no idea the order in init mattered. Now I know better.