Results 1 to 4 of 4

Thread: Whats wrong??

  1. #1
    javaa is offline Member
    Join Date
    Aug 2012
    Posts
    28
    Rep Power
    0

    Default 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.
    Whats wrong??-errors.png

    Here is my code:
    Java 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!!!
    Attached Thumbnails Attached Thumbnails Whats wrong??-errors.png  
    Last edited by javaa; 08-29-2012 at 03:02 AM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default 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.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,242
    Rep Power
    19

    Default Re: Whats wrong??

    Quote Originally Posted by javaa View Post
    Whats wrong??
    Please go through the Forum Rules -- particularly the third paragraph.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default 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...:)
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. whats wrong with my GUI?
    By EscSequenceAlpha in forum New To Java
    Replies: 12
    Last Post: 05-31-2012, 08:20 PM
  2. I cant do this.Whats wrong?
    By gaston7eze in forum Java 2D
    Replies: 5
    Last Post: 05-30-2012, 12:34 AM
  3. Whats wrong with this?
    By Alkor in forum AWT / Swing
    Replies: 7
    Last Post: 04-03-2012, 05:49 AM
  4. whats wrong
    By atenv in forum New To Java
    Replies: 6
    Last Post: 06-15-2010, 01:55 PM
  5. whats is wrong with this app??
    By mrajan in forum New To Java
    Replies: 4
    Last Post: 06-09-2010, 10:56 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •