Applet Not Initialized and Null Pointer Exception?
843807Oct 20 2005 — edited Oct 31 2005Hi,
I've written a short applet which should move ten small
ellipses around a small screen(200x200) and they change direction
when they hit the outer bounds
Everything compiles, but when I try to run the applet(in appletviewer) I get an "Applet Not Initialized" error message in the status bar. Upon deugging, there is a Null Pointer exception at the bBalls[count].setBBall(n,n,n,n) method in the overridden init() function.
can anyone see what I'm doing wrong? I'd be very grateful for any help.
the code is below, many thanks Graham
package gram;
/**
*
* @author gram
*/
import java.applet.*;
import java.awt.*;
import java.util.Random;
public class bouncyBalls extends java.applet.Applet implements Runnable //thread support for applet
{
class bBall
{
int xPos; //used to track bouncy ball
int yPos; //position
int speed_x; //speed of current bouncy ball set at initialisation
int speed_y;
void setBBall(int Pos_x, int Pos_y, int Y_speed, int X_speed)
{
this.xPos = Pos_x;
this.yPos = Pos_y;
this.speed_x = X_speed;
this.speed_y = Y_speed;
}
void setPosition(int newX, int newY)
{
this.xPos = newX + speed_x;
this.yPos = newY + speed_y;
}
int getXPos()
{
return this.xPos;
}
int getYPos()
{
return this.yPos;
}
void changeXSpeed()
{
this.speed_x = speed_x * -1;
}
void changeYSpeed()
{
this.speed_y = speed_y * -1;
}
}
//variables
Thread a_thread; //animation thread
bBall bBalls[] = new bBall[10]; //create an array of 10 unitiailised bBalls
public void start()
{
a_thread = new Thread();
a_thread.start();
}
public void init()
{
try
{
for(int count = 0; count < 10; count++)
{
Random rna = new Random(301278);
int a = rna.nextInt();
int b = rna.nextInt();
int c = rna.nextInt();
int d = rna.nextInt();
bBalls[count].setBBall(a%90, b%90, c%10, d%10); //initialise function with random values
//and max speed of 8
}
}
catch(NullPointerException e)
{
bBalls[0].setBBall(10,10,9,9);
bBalls[1].setBBall(10,10,6,8);
bBalls[2].setBBall(10,10,4,6);
bBalls[3].setBBall(10,10,8,7);
bBalls[4].setBBall(10,10,9,3);
bBalls[5].setBBall(10,10,1,2);
bBalls[6].setBBall(10,10,9,6);
bBalls[7].setBBall(10,10,6,9);
bBalls[8].setBBall(10,10,6,2);
bBalls[9].setBBall(10,10,8,4);
}
}
public void run()
{
setBackground(Color.yellow);
while(true)
{
try
{
for(int i_count = 0; i_count < 10; i_count++)
{
//if ball hits side, then rebound!
if(bBalls[i_count].getXPos()==190 || bBalls[i_count].getXPos()==0)
{
bBalls[i_count].changeXSpeed();
}
if(bBalls[i_count].getYPos()==190 || bBalls[i_count].getYPos()==0)
{
bBalls[i_count].changeYSpeed();
}
bBalls[i_count].setPosition(bBalls[i_count].getXPos(), bBalls[i_count].getYPos());
}
repaint();
}
catch(Exception e)
{
stop();
}
}
}
public void paint(Graphics g)
{
for(int a_count = 0; a_count < 10; a_count++)
{
g.setColor(Color.blue);
g.fillOval(bBalls[a_count].getXPos(), bBalls[a_count].getYPos(), 10, 10);
}
}
public void stop()
{
if(a_thread != null)
{
a_thread.stop();
a_thread = null;
}
}
}