 |
AppletTalk.com Java discussions newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Guest
|
Posted: Mon Feb 27, 2006 5:12 am Post subject: Please help: Exception in thread "Thread-3" java.lang.NullPo |
|
|
I get the following error when I run applet. What am I doing wrong?
Thank You,
Andy
ÏException in thread "Thread-3" java.lang.NullPointerException
ÏÏ§Ï at StarField.updateStar(StarField.java:132)
ÏÏ§Ï at StarField.run(StarField.java:94)
ÏÏ§Ï at java.lang.Thread.run(Thread.java:595)
import javax.swing.JApplet;
import java.awt.Color;
import java.awt.Event;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Point;
import java.awt.Polygon;
import java.awt.Rectangle;
import java.util.Random;
public class StarField extends JApplet implements Runnable
{
boolean initializing = true; //Is Applet in init()
boolean done = false; //Terminate loop for
star field
boolean start = false; //Start Animation
float starRadius; //Radius of star
String name ="Andrew Titus"; //Set name to Andrew
Titus
Thread starThread; //Main star thread
double star_dx, star_dy; //Star velocity
Image doublebuffer; //Offscreen buffer
Image bgImage; //Background image
Font creator; //Font to display
name
Rectangle walls; // Self explanitory.
They're all rectangles.
Point starPoint;
Point mouse;
Polygon star = new Polygon(
new int[]{0,6,24,9,15,0,-15,-9,-24,-6},
new int[]{-25,-8,-8,3,20,9,20,3,-8,-8},10);
//******************************** init()
public void init()
{
int x,y;
mouse = new Point(0,0);
//Create buffer to draw on
doublebuffer = createImage(getWidth(),getHeight());
//Font to display creator, 20% of star field height
creator = new Font("Impact",Font.BOLD,(int)(getHeight()*0.0 );
//Walls inside applet bounds
x = (int)(0.01 * getWidth() );
y = (int)(0.01 * getHeight() );
walls = new Rectangle( x, y, getWidth() - x*2, getHeight() - y*2 );
} // end init()
//******************************** start()
public void start()
{
bgImage = getImage(getCodeBase(),"background");
initializing = false;
starThread = new Thread(this);
starThread.start();
} // end start()
//******************************** stop()
public void stop()
{
done = true;
starThread.stop();
} // end stop()
// Java applet will call mouseDown() whenever you click.
public boolean mouseDown( Event e, int x, int y )
{
if ( start == false )
startStarField();
return true;
}
//******************************** run()
public void run()
{
while( !done )
{
testWalls();
updateStar();
repaint();
try {
Thread.sleep( 1000/60 ); // 1000ms / 60 fps
}
catch( Exception e ){}
}
} // end run()
//******************************** testStarField()
void startStarField()
{
//Starting speed of the ball is scaled according to playfield.
double speedX = 0.006;
double speedY = 0.004;
start = true;
// re-center.
starPoint.x = getWidth() / 2;
starPoint.y = getHeight() / 2;
boolean isEven;
double speedScale;
isEven = ( (int)( Math.random() * 10) % 2 == 0 );
star_dx = speedX*getWidth() * ( isEven ? -1 : 1 );
isEven = ( (int)( Math.random() * 10) % 2 == 0 );
star_dy = speedY*getHeight() * ( isEven ? -1 : 1 );
}//end testStarField()
void updateStar()
{
starPoint.x += star_dx;
starPoint.y += star_dy;
}
//******************************** testWalls()
void testWalls()
{
int left = walls.x;
int top = walls.y;
int right = walls.x + walls.width;
int bottom = walls.y + walls.height;
if ( ( starPoint.x + star_dx - 25 ) <= left )
bounceStar();
if ( ( starPoint.x + star_dx + 25) >= right )
bounceStar();
if ( ( starPoint.y + star_dy - 25) <= top )
bounceStar();
if ( ( starPoint.y + star_dy + 25) >= bottom )
bounceStar();
}//end testWalls()
void bounceStar()
{
System.out.println("OUT");
}
//******************************** update()
public void update(Graphics g)
{
paint(g);
}
public void paint(Graphics g)
{
int x,y;
if(initializing)
{
g.drawString("Inatialzing...",getWidth()-15,getHeight()/2);
return;
}
// Graphics graphics = getGraphics();
Graphics dbG = doublebuffer.getGraphics();
// if (g == null) return;
//Sets background
dbG.setColor( Color.RED ); // Choose color to draw with.
dbG.fillRect(0, 0, getWidth(), getHeight() );
//Draw background image centered
dbG.drawImage(bgImage,0,0,getWidth(),getHeight(),
0,0,bgImage.getWidth(this),
bgImage.getHeight(this),this);
//Draws the rectangle for applet bounds two gray and one white
dbG.setColor( Color.BLACK );
dbG.drawRect( walls.x-1, walls.y-1, walls.width+2, walls.height+2 );
dbG.drawRect( walls.x+1, walls.y+1, walls.width-2, walls.height-2 );
dbG.setColor( Color.WHITE );
dbG.drawRect( walls.x, walls.y, walls.width, walls.height );
//Sets color and font
dbG.setColor(Color.black);
dbG.setFont( creator );
//Sets for center of screen
x = getWidth()/2;
y = getHeight() /2;
//Draws creator name
dbG.drawString(name,x,y);
//Sets for center of screen
x = getWidth()/2;
y = getHeight() /2;
//Draws star
dbG.setColor(Color.white);
dbG.fillPolygon(star);
// Copy doublebuffer to screen.
dbG.drawImage(doublebuffer, 0, 0, this );
dbG.dispose();
}// end paint()
} //end starfield() |
|
| Back to top |
|
 |
Bart Cremers Guest
|
Posted: Mon Feb 27, 2006 7:12 am Post subject: Re: Please help: Exception in thread "Thread-3" java.lang.Nu |
|
|
You're nowhere initializing the startPoint field, so a NullPointer is
quite normal at the given line.
Bart |
|
| Back to top |
|
 |
Guest
|
Posted: Mon Feb 27, 2006 11:12 am Post subject: Re: Please help: Exception in thread "Thread-3" java.lang.Nu |
|
|
I got rid of that error message but still no animation. Not sure what Iam
doing wrong. Please point me in the right direction.
Thank You,
Andy
import javax.swing.JApplet;
import java.awt.Color;
import java.awt.Event;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Point;
import java.awt.Polygon;
import java.awt.Rectangle;
import java.util.Random;
public class StarField extends JApplet implements Runnable
{
boolean initializing = true; //Is Applet in init()
boolean done = false; //Terminate loop for
star field
boolean start = false; //Start Animation
float starRadius; //Radius of star
String name ="Andrew Titus"; //Set name to Andrew
Titus
Thread starThread; //Main star thread
double star_dx, star_dy; //Star velocity
Image doublebuffer; //Offscreen buffer
Image bgImage; //Background image
Font creator; //Font to display
name
Rectangle walls; // Self explanitory.
They're all rectangles.
Point starPoint;
Point mouse;
Polygon star = new Polygon(
new int[]{0,6,24,9,15,0,-15,-9,-24,-6},
new int[]{-25,-8,-8,3,20,9,20,3,-8,-8},10);
//******************************** init()
public void init()
{
int x,y;
mouse = new Point(0,0);
//Create buffer to draw on
doublebuffer = createImage(getWidth(),getHeight());
//Font to display creator, 20% of star field height
creator = new Font("Impact",Font.BOLD,(int)(getHeight()*0.0 );
//Walls inside applet bounds
x = (int)(0.01 * getWidth() );
y = (int)(0.01 * getHeight() );
walls = new Rectangle( x, y, getWidth() - x*2, getHeight() - y*2 );
} // end init()
//******************************** start()
public void start()
{ starThread = new Thread(this);
bgImage = getImage(getCodeBase(),"background");
initializing = false;
//I added this and got rid of null exception error
if (starThread == null) {
starThread = new Thread(this, "Clock");
starThread.start();
}
} // end start()
//******************************** stop()
public void stop()
{
done = true;
// starThread.stop();
} // end stop()
// Java applet will call mouseDown() whenever you click.
public boolean mouseDown( Event e, int x, int y )
{
if ( start == false )
startStarField();
return true;
}
//******************************** run()
public void run()
{
while( !done )
{
testWalls();
updateStar();
repaint();
try {
Thread.sleep( 1000/60 ); // 1000ms / 60 fps
}
catch( Exception e ){}
}
} // end run()
//******************************** testStarField()
void startStarField()
{
start = true;
//Starting speed of the ball is scaled according to playfield.
double speedX = 0.006;
double speedY = 0.004;
// re-center.
starPoint.x = getWidth() / 2;
starPoint.y = getHeight() / 2;
boolean isEven;
double speedScale;
isEven = ( (int)( Math.random() * 10) % 2 == 0 );
star_dx = speedX*getWidth() * ( isEven ? -1 : 1 );
isEven = ( (int)( Math.random() * 10) % 2 == 0 );
star_dy = speedY*getHeight() * ( isEven ? -1 : 1 );
}//end testStarField()
void updateStar()
{
starPoint.x += star_dx;
starPoint.y += star_dy;
}
//******************************** testWalls()
void testWalls()
{
int left = walls.x;
int top = walls.y;
int right = walls.x + walls.width;
int bottom = walls.y + walls.height;
if ( ( starPoint.x + star_dx - 25 ) <= left )
bounceStar();
if ( ( starPoint.x + star_dx + 25) >= right )
bounceStar();
if ( ( starPoint.y + star_dy - 25) <= top )
bounceStar();
if ( ( starPoint.y + star_dy + 25) >= bottom )
bounceStar();
}//end testWalls()
void bounceStar()
{
System.out.println("OUT");
}
//******************************** update()
public void update(Graphics g)
{
paint(g);
}
public void paint(Graphics g)
{
int x,y;
if(initializing)
{
g.drawString("Inatialzing...",getWidth()-15,getHeight()/2);
return;
}
Graphics dbG = doublebuffer.getGraphics();
//Sets background
dbG.setColor( Color.RED ); // Choose color to draw with.
dbG.fillRect(0, 0, getWidth(), getHeight() );
//Draw background image centered
dbG.drawImage(bgImage,0,0,getWidth(),getHeight(),
0,0,bgImage.getWidth(this),
bgImage.getHeight(this),this);
//Draws the rectangle for applet bounds two gray and one white
dbG.setColor( Color.BLACK );
dbG.drawRect( walls.x-1, walls.y-1, walls.width+2, walls.height+2 );
dbG.drawRect( walls.x+1, walls.y+1, walls.width-2, walls.height-2 );
dbG.setColor( Color.WHITE );
dbG.drawRect( walls.x, walls.y, walls.width, walls.height );
//Sets color and font
dbG.setColor(Color.black);
dbG.setFont( creator );
//Sets for center of screen
x = getWidth()/2;
y = getHeight() /2;
//Draws creator name
dbG.drawString(name,x,y);
//Sets for center of screen
x = getWidth()/2;
y = getHeight() /2;
//Draws star
dbG.setColor(Color.white);
dbG.fillPolygon(star);
// Copy doublebuffer to screen.
g.drawImage(doublebuffer, 0, 0, this );
dbG.dispose();
}// end paint()
} //end starfield() |
|
| Back to top |
|
 |
Bart Cremers Guest
|
Posted: Mon Feb 27, 2006 12:12 pm Post subject: Re: Please help: Exception in thread "Thread-3" java.lang.Nu |
|
|
There are several issues with the code:
1. Don't override 'boolean mouseDown()' to handle mouse events. It's
deprecated and for sure doesn't generate an event on my system. Add a
mouselistener (in the init) to the applet instead on implement it's
'mouseClicked()' method.
2. It still generates a NullPointer, because starPoint is never
initialized. Initialize it in the init(): starPoint = new
Point(getWidth() / 2, getHeight() / 2);
3. The thread you is never started. Check you start() method
starThread = new Thread(this);
...
if (starThread == null) { // This can never be null, so start() is
never called on the thread.
starThread = new Thread(this, "Clock");
starThread.start();
}
Regards,
Bart |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|