Results 1 to 2 of 2

Thread: My first code

  1. #1
    Patoto is offline Member
    Join Date
    Apr 2011
    Posts
    1
    Rep Power
    0

    Default My first code

    Hello
    I just finished a Java tutorial i found, so now i know the basics of the language, so i decided to make my first code for a game, but im having a problem at the beginning and im not sure how to fix it, ill show it to you guys:

    Java Code:
    import java.awt.*;
    import javax.swing.*;
    
    public class Wilson{
    	public static void main(String[] args){
    		Wilson w = new Wilson();
    		w.run();
    		
    	}	
    	
    	private Core c;
    	private KeyTest kt;
    	private Image bg; 
    	private Animation a;
    	private Sprite sprite;
    	private ScreenManager s;
    	
    	//run
    	public void run(){
    		c.init();
    		kt.init();
    		loadImages();
    		mainLoop(c.getRunning());
    	}
    	
    	//Load images and scenes
    	public void loadImages(){
    		bg = new ImageIcon("C:\\Markus\\Imgenes\\WI\\Escenarios\\Playa\\WI_Playa.png").getImage();
    		Image Tiko1 = new ImageIcon("C:\\Markus\\Imgenes\\WI\\Enemigos\\Tiko's\\WI_Tíko.png").getImage();
    		Image Tiko2 = new ImageIcon("C:\\Markus\\Imgenes\\WI\\Enemigos\\Tiko's\\WI_Tíko2.png").getImage();
    		
    		a = new Animation();
    		a.addScene(Tiko1,500);
    		a.addScene(Tiko2,500);
    		
    		sprite = new Sprite(a);
    		sprite.setVelocityX(20);
    		sprite.setVelocityY(20);
    	}
    	
    	public void mainLoop(boolean running){
    		long startTime = System.currentTimeMillis();
    		long cumTime = startTime;
    		
    		while(running){
    			long timePassed = System.currentTimeMillis() - cumTime;
    			cumTime += timePassed;
    			
    			update();
    			
    			Graphics2D g = s.getGraphics();
    			draw(g);
    			g.dispose();
    			s.update();
    			
    			try{
    				Thread.sleep(20);
    			}catch(Exception ex){}
    		}
    	}
    	
    	//update sprite
    	public void update(){
    		if(sprite.getX() < 0){
    			sprite.setVelocityX(Math.abs(sprite.getVelocityX()));
    		}else if(sprite.getX() + sprite.getWidth() > s.getWidth()){
    			sprite.setVelocityX(-Math.abs(sprite.getVelocityX()));
    		}
    		if(sprite.getY() < 0){
    			sprite.setVelocityY(Math.abs(sprite.getVelocityY()));
    		}else if(sprite.getY() + sprite.getHeight() > s.getHeight()){
    			sprite.setVelocityY(-Math.abs(sprite.getVelocityY()));
    		}
    		
    		sprite.uptade(1);
    	}
    	
    	//draws graphics
    	public void draw(Graphics g){
    		g.drawImage(bg,0,-50,null);
    		g.drawImage(sprite.getImage(),Math.round(sprite.getX()), Math.round(sprite.getY()),null);
    	}
    }
    When i try to run that code, an error appears saying: Exception in thread "main" java.lang.NullPointerException
    at Wilson.run(Wilson.java:20)
    at Wilson.main(Wilson.java:7)

    So i know the problem is in the run() and in the c.init(), but right now i just would like to know what does the error mean in the run() part

    I know that when the program talks about Exception maybe the code needs a try catch, but i dont think that is it here, i know it may be a rly noobie problem, well, im pretty noobie right now, but i would rly appreciate some help so i can learn from this error, thx :(bow):
    Last edited by Fubarable; 05-29-2011 at 02:37 AM. Reason: code tags added

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    The exception you get means you are trying to do something to an uninitialized variable.

    I'd like you to also wrap your code in code tags
    [code]
    YOUR CODE HERE
    [/code]

    Also, make sure the exception report is always the entire stack trace.

    Edit:please highlight the lines that cause the exception(20 and 7 I believe)

Similar Threads

  1. look @ code and give ideas! Install new code....
    By turbowhat in forum New To Java
    Replies: 2
    Last Post: 04-27-2011, 03:08 AM
  2. Replies: 0
    Last Post: 02-21-2011, 11:50 AM
  3. Replies: 1
    Last Post: 03-04-2010, 11:19 AM
  4. Convert java code to midlet code
    By coldvoice05 in forum Advanced Java
    Replies: 1
    Last Post: 08-09-2009, 01:21 PM
  5. Replies: 1
    Last Post: 04-26-2007, 03:52 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
  •