Results 1 to 6 of 6
  1. #1
    robertpas is offline Member
    Join Date
    Jan 2012
    Posts
    2
    Rep Power
    0

    Default Help regarding a java game

    Hello,

    This is my first post on Java Forums and I really need help. I have made a small java game using Eclipse, but i have a problem. It randomly doesn't start.
    When I hit run it either works as expected or the game freezes.

    When that happens i get these errors

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at Game.Board.checkCollisions(Board.java:92)
    at Game.Board.actionPerformed(Board.java:44)
    at javax.swing.Timer.fireActionPerformed(Timer.java:3 13)
    at javax.swing.Timer$DoPostEvent.run(Timer.java:244)
    at java.awt.event.InvocationEvent.dispatch(Invocation Event.java:226)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.j ava:647)
    at java.awt.EventQueue.access$000(EventQueue.java:96)
    at java.awt.EventQueue$1.run(EventQueue.java:608)
    at java.awt.EventQueue$1.run(EventQueue.java:606)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectio nPrivilege(AccessControlContext.java:105)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java: 617)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:275)
    at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:200)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:190)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:185)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:177)
    at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:138)

    What is the cause of all this?


    I am aware that my style of programming is a bit sloppy, but I am new to java and don't know how to write a program efficiently.

    Help please
    Last edited by robertpas; 01-17-2012 at 05:04 PM.

  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 Re: Help regarding a java game

    Is it a lot of code? If not, please post it here and wrap it in code tags ([code] YOUR CODE HERE [/code])

    Generally a NullPointerException means something hasn't been initialized before being called. Follow the line numbers from the stack trace and make sure everything is correctly initialized (i.e not null). Don't be afraid to use print statements to find the value of objects.

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

    Default Re: Help regarding a java game

    First you need to inspect the line that's throwing the NPE (null pointer exception) and check to see which variable is null on that line. Next trace back through your program to see why it's null.

  4. #4
    robertpas is offline Member
    Join Date
    Jan 2012
    Posts
    2
    Rep Power
    0

    Default Re: Help regarding a java game

    ok, so here's where Eclipse has problems

    Java Code:
     
    public void checkCollisions()
    	{
    		Rectangle r1=en.getBounds();

    getBounds is defined as

    Java Code:
     public Rectangle getBounds()
        {
        	return new Rectangle(x,y,184,147);
        }

    Here's the whole Board class:

    Java Code:
    package Game;
    
    import java.awt.*;
    import java.awt.event.*;
    
    import javax.swing.*;
    
    public class Board extends JPanel implements ActionListener, Runnable{
    	Dude p;
    	Image img;
    	Timer time;
    	int v = 600;
    	
    	int ok=0;
        Thread animator;
        
        Enemy en;
        Enemy en2;
        Enemy en3,en5;
        Enemy2 en4,en6,healthpack;
        Platform p1;
        boolean lost=false;
        
        boolean k=false;
    	public Board(){
    		p=new Dude();
    		addKeyListener(new AL());
    		setFocusable(true);
    		ImageIcon i =new ImageIcon("Background.jpg");
    		img=i.getImage();
    		time= new Timer(5, this);
    		time.start();
    		p1=new Platform(550,430,"platform.png");
    		en=new Enemy(1400,550,"enemy.png");
    		en2=new Enemy(-197,550,"enemy.png");
    		en3=new Enemy(880,280,"enemy.png");
    		en4=new Enemy2(298,280,"enemy.png");
    		en5=new Enemy(1400,50,"fighter-jet.png");
    		en6=new Enemy2(-197,50,"fighter-jet2.png");
    		healthpack=new Enemy2(633,650,"toolbox.png");
    		
    	}
    	public void actionPerformed(ActionEvent e){
    	checkCollisions();
    
    		p.move();
    		
    	     en.move(3);
    		 en2.move(-3);
    		if (en3.x>=300)
    		    en3.move(3);
    		else if(en3.x<=300){
    			
    			en3.isAlive=false;
    			en4.isAlive=true;
    			en4.move(-3);
    			
    			if(en4.x>=880)
    			{
    				en4.x=298;
    				en4.isAlive=false;
    			    en3.isAlive=true;
    			    en3.x=880;
    			    en3.move(3);
    			}
    		}
    		if (en5.x>=-197)
    		    en5.move(6);
    		else if(en5.x<=-197){
    			
    			en5.isAlive=false;
    			en6.isAlive=true;
    			en6.move(-6);
    			
    			if(en6.x>=1400)
    			{
    				en6.x=-197;
    				en6.isAlive=false;
    			    en5.isAlive=true;
    			    en5.x=1400;
    			    en5.move(3);
    			}
    		}
    	
    		if(p.score>1999 && p.score%1000==0)
    			healthpack.isAlive=true;
    		repaint();
    	}
    	
    	public void checkCollisions()
    	{
    		Rectangle r1=en.getBounds();
    		Rectangle r2=en2.getBounds();
    		Rectangle r3=p1.getBounds();
    		Rectangle r4=en3.getBounds();
    		Rectangle r5=en4.getBounds();
    		Rectangle r6=en5.getBounds();
    		Rectangle r7=en6.getBounds();
    		Rectangle h=healthpack.getBounds();
    		Rectangle d=p.getBounds();
    		if(d.intersects(r1)||d.intersects(r2))
    		{
    			if(v>=en.y-100)
    			  p.hp=p.hp-1;
    			if (p.hp==0)
    			  lost=true;
    			if(p.hp<0)
    				p.hp=0;
    		}
    		if(d.intersects(h)&& healthpack.Alive()==true)
    		{
    			p.hp=p.hp+25;
    			if(p.hp>100)
    				p.hp=100;
    			healthpack.isAlive=false;
    		}
    		if((d.intersects(r4)&& en3.isAlive==true)||(d.intersects(r5)&&en4.isAlive==true))
    		{
    			if(v>=en3.y-100)
    			  p.hp=p.hp-1;
    			if (p.hp==0)
    			  lost=true;
    			if(p.hp<0)
    				p.hp=0;
    		}
    		if((d.intersects(r6)&& en5.isAlive==true)||(d.intersects(r7)&&en6.isAlive==true))
    		{
    			if(v>=en5.y-100)
    			  p.hp=p.hp-1;
    			if (p.hp==0)
    			  lost=true;
    			if(p.hp<0)
    				p.hp=0;
    		}
    		if(d.intersects(r3) && p.y==330)
    		{
    			
    				ok=1;
    				p.y=329;
    				
    				
    			
    		}
    		else if(p.y==329 && p.x>=539 &&p.x<=785)
    				 ok=2;			  
    		else if(p.y==329 && (p.x<539 ||p.x>785))
    			ok=3;
    		else ok=0;
    		
    			 
    			
    		     
    		
    		
    	}
     
    	static Font font = new Font("SanSerif",Font.BOLD,24);	
    	static Font font2 = new Font("SanSerif",Font.BOLD,60);
    	public void paint(Graphics g){
    		
    		if (p.dy==3 && k==false)
    		{
    			k=true;
    			animator=new Thread(this);
    			animator.start();
    		}
    		super.paint(g);
    			Graphics2D g2d = (Graphics2D) g;
    		g2d.drawImage(img, 0, 0, null);
    		g2d.drawImage(p.getImage(), p.getX(), v, null);
    		if(en.Alive()==true)
    				g2d.drawImage(en.getImage(),en.getX(),en.getY(),null);
    		if(en2.Alive()==true)
    			g2d.drawImage(en2.getImage(),en2.getX(),en2.getY(),null);
    		if(en3.Alive()==true)
    			g2d.drawImage(en3.getImage(),en3.getX(),en3.getY(),null);
    		if(en4.Alive()==true)
    			g2d.drawImage(en4.getImage(),en4.getX(),en4.getY(),null);
    		if(en5.Alive()==true)
    			g2d.drawImage(en5.getImage(),en5.getX(),en5.getY(),null);
    		if(en6.Alive()==true)
    			g2d.drawImage(en6.getImage(),en6.getX(),en6.getY(),null);
    		if(healthpack.Alive()==true)
    			g2d.drawImage(healthpack.getImage(),healthpack.getX(),healthpack.getY(),null);
    		g2d.drawImage(p1.getImage(),p1.getX(),p1.getY(),null);
    		g2d.setFont(font);
    		g2d.setColor(Color.RED);
    		g2d.drawString("Health :"+p.hp, 10, 740);
    		g2d.setColor(Color.YELLOW);
    		g2d.drawString("Score :"+p.score, 800, 740);
    		
    		if(lost){
    			g2d.setFont(font2);
    			g2d.setColor(Color.RED);
    			g2d.drawString("YOU LOST", 500, 350);
    			g2d.setFont(font);
    			g2d.setColor(Color.RED);
    			g2d.drawString("Press ESC to exit",550,450);
    			
    		}
    		else {
    			p.score++;
    		}
    	
    		
    	}
    	private class AL extends KeyAdapter{
    		 public void keyReleased(KeyEvent e){
    			 p.keyReleased(e);
    		 }
    		 public void keyPressed(KeyEvent e){
    			 p.keyPressed(e);
    		 }
    	}
    	boolean h=false;
    	boolean done=false;
    	
    	int hi=600;
    	public void cycle() {
            if (h == false)
            	{
                    v=v-3;
                    p.y=v;
            }
            if (v == hi-300||ok==1||ok==3)
            {
                  
                    h = true;
                    
                    if(ok==1)
                    	hi=v;
                   
            }
            
          
            if (h == true && v <= 600) {
            	if(ok==0 || ok==3){
                    v=v+3;
                    if (v>600)
                    	v=600;
                    p.y=v;
            	}
            	     
            		 if(v==600 && ok==0)
            		 { done=true;
            		    hi=v;
            		 }
            		 else 
                       if (v == hi && ok==2) {
                            done = true;
                    }
            }
            
    	 }
    
    
    	
    	public void run() {
    		long beforeTime, timeDiff, sleep;
    		 
            beforeTime = System.currentTimeMillis();
    
            while (done == false) {
    
                    cycle();
    
                    timeDiff = System.currentTimeMillis() - beforeTime;
                    sleep = 10 - timeDiff;
    
                    if (sleep < 0)
                            sleep = 2;
                    try {
                            Thread.sleep(sleep);
                    } catch (Exception e) {
                            System.out.println("interrupted");
                    }
    
                    beforeTime = System.currentTimeMillis();
            }
            done = false;
            h = false;
            k= false;
            
    		
    	}
    
    }

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,397
    Rep Power
    25

    Default Re: Help regarding a java game

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at Game.Board.checkCollisions(Board.java:92)
    What variable on line 92 is null?

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

    Default Re: Help regarding a java game

    At some point in your code 'en' is set to null after the Board instance is created.

Similar Threads

  1. Replies: 1
    Last Post: 06-02-2011, 09:29 AM
  2. help Java game
    By rawan in forum JCreator
    Replies: 1
    Last Post: 12-25-2009, 06:43 PM
  3. Java 2d game
    By Mr.Beans in forum Java 2D
    Replies: 0
    Last Post: 08-05-2009, 09:27 PM
  4. java game
    By mayhewj7 in forum New To Java
    Replies: 1
    Last Post: 04-10-2009, 07:01 AM
  5. Help with my game in java
    By lenny in forum New To Java
    Replies: 1
    Last Post: 07-23-2007, 04:40 PM

Tags for this Thread

Posting Permissions

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