-
2 Attachment(s)
Whats wrong??
I am trying to make a code to simulate the hare and tortoise race. I get this error when I run it even though it "passes" compilation.
Attachment 4063
Here is my code:
Code:
import java.awt.*;
import java.applet.Applet;
public class Liang extends Applet {
private int disfromfinh = 50;
private int disfromfint = 50;
private int xh = 5;
private int xt = 5;
private Graphics screen;
private int a1, a2;
private Image hare, tortoise;
public void paint(Graphics screen) {
setBackground( Color.green );
hare = getImage( getDocumentBase(), "hare.gif" );
tortoise = getImage( getDocumentBase(), "tortoise.gif" );
screen.drawImage( hare, 5, 5, this);
screen.drawImage(tortoise, 5, 120, this);
screen.drawLine( 0, 115, 1140, 115);
screen.drawLine( 1140, 0, 1140, 400);
Font f = new Font( "Tahoma", Font.BOLD, 30 );
screen.setFont( f );
screen.setColor( new Color( 240, 15, 179 ));
screen.drawString( "F", 1150, 40);
screen.drawString( "I", 1150, 70);
screen.drawString( "N", 1150, 100);
screen.drawString( "I", 1150, 130);
screen.drawString( "S", 1150, 160);
screen.drawString( "H", 1150, 190);
screen.drawString( "L", 1200, 70);
screen.drawString( "I", 1200, 100);
screen.drawString( "N", 1200, 130);
screen.drawString( "E", 1200, 160);
start();
}
public void start() {
new Thread (
new Runnable() {
public void run() {
race();
}
}
).start();
}
public void race() {
while ( disfromfinh > 0 & disfromfint > 0 ) {
// For the hare
int moveh = (int)(Math.random() * 10) + 1;
int movet = (int)(Math.random() * 10) + 1;
if (moveh == 1 | moveh == 2) {
delay();delay();delay();delay();delay();delay();delay();delay();delay();delay();delay();delay();
a1 = xh + 180;
repaint();
xh = a1;
disfromfinh -= 9;
}
else if (moveh == 3 | moveh == 4 | moveh == 5) {
delay();delay();delay();delay();delay();delay();delay();delay();delay();delay();delay();delay();
a1 = xh + 20;
repaint();
xh = a1;
disfromfinh -= 1;
}
else if (moveh == 6) {
delay();delay();delay();delay();delay();delay();delay();delay();delay();delay();delay();delay();
a1 = xh - 240;
repaint();
xh = a1;
disfromfinh += 12;
if (disfromfinh > 50) {
a1 = 5;
repaint();
disfromfinh = 50;
xh = 5;
}
}
else if (moveh == 7 | moveh == 8) {
delay();delay();delay();delay();delay();delay();delay();delay();delay();delay();delay();delay();
a1 = xh - 40;
repaint();
xh = a1;
disfromfinh += 2;
if (disfromfinh > 50) {
a1 = 5;
repaint();
disfromfinh = 50;
xh = 5;
}
}
else
delay();delay();delay();delay();delay();delay();delay();delay();delay();delay();delay();delay();
//For the turtle
if (5 >= movet & movet >= 1) {
a2 = xt + 60;
repaint();
xt = a2;
disfromfint -= 3;
}
else if (movet == 6 | movet == 7 | movet == 8) {
a2 = xt + 20;
repaint();
xt = a2;
disfromfint -= 1;
}
else {
a2 = xt - 120;
repaint();
xt = a2;
disfromfint += 6;
if (disfromfint > 50) {
a2 = 5;
repaint();
disfromfint = 50;
xt = 5;
}
}
}
}
public void repaint() {
screen.clearRect(xh, 5, xh + 118, 107);
screen.drawImage( hare, a1 , 5, this);
screen.clearRect(xt, 120, xt + 130, 208);
screen.drawImage( tortoise, a2 , 120, this);
}
public void delay() {
for( int i = 0; i <= 999999999; i++ );
}
}
Help Please!!!
-
Re: Whats wrong??
You've got a null variable. First thing to do is to check the line that the NPE exception tells you to check and find which variable is null.
-
Re: Whats wrong??
Quote:
Originally Posted by
javaa
Whats wrong??
Please go through the Forum Rules -- particularly the third paragraph.
db
-
Re: Whats wrong??
I've not done Applets, but I'm pretty sure overriding repaint() is frowned upon.
Also your whole initalisation of the applet strikes me as incorrect.
And you probably ought to be using a JApplet, and paintComponent().
Some of the above might need checking by more applet-savvy people...:)