Results 1 to 10 of 10
  1. #1
    Kyl9 is offline Member
    Join Date
    Feb 2014
    Posts
    6
    Rep Power
    0

    Default Problem with code.

    Hi, I'm still a newbie with java, so maybe this problem comes from a stupid error which I can't see. It's a simple program... though I'd like you to help me.This is the code:
    Java Code:
    import javax.swing.*;
    
    import java.awt.*;
    import java.awt.event.*;
    
    public class Sparo extends JFrame implements Runnable
    {
    	private static final long serialVersionUID = 1L;
    	private boolean readyToShoot=true,shot=false;
    	private Image dbi;
    	private Graphics dbg;
    	private int x,y,dx,dy,xr,yr;
    	Rectangle r1;
    	public Sparo()
    	{
    		x=250;
    		y=460;
    
    		yr=440;
    		setTitle("Shot");
    		setSize(500,500);
    		setVisible(true);
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setBackground(Color.white);
    		addKeyListener(new Kl());
    	}
    	public static void main(String[]args)
    	{
    		Sparo s=new Sparo();
    		Thread th= new Thread(s);
    		th.start();
    	}
    	public void run()
    	{
    		try {
    		while(true)
    		{
    			move();
    
    			shoot();
    				Thread.sleep(5);
    			} 
    		}
    			catch (InterruptedException e) 
    			{
    				e.printStackTrace();
    			}
    		}
    	
    	public void move()
    	{
    		x+=dx;
    		y+=dy;
    		xr=x+24;
    		if(x>=getWidth()-55)
    			x=getWidth()-55;
    		
    		if(x<=7)
    			x=7;
    	}
    	public void shoot()
    	{
    		if(shot)
    		{
    			r1.y-=3;
    		}
    		
    		
    			
    	}
    	public void setDirx(int d)
    	{
    		dx=d;
    	}
    	public void setDiry(int d1)
    	{
    		dy=d1;
    	}
    	@Override
    	public void paint(Graphics g)
    	{
    		
    		dbi=createImage(getWidth(),getHeight());
    		dbg=dbi.getGraphics();
    		paintComponent(dbg);
    		g.drawImage(dbi,0,0,this);
    		if(r1.y<20)
    		{
    			readyToShoot=true;
    			shot=false;
    		}
    		
    	}
    	public void paintComponent(Graphics g)
    	{
    		g.setColor(Color.black);
    		g.fillRect(x,y,50, 20);
    		g.fillRect(x+21, y-20,10, 24);
    		if(shot)
    		{
    			g.fillRect(r1.x,r1.y,3,5);
    		}
    		
    		repaint();
    		
    	}
    	public class Kl extends KeyAdapter
    	{
    		@Override
    		public void keyPressed(KeyEvent e)
    		{
    			int keyC=e.getKeyCode();
    			if(keyC==KeyEvent.VK_RIGHT)
    			{
    				setDirx(+1);
    			}
    			if(keyC==KeyEvent.VK_LEFT)
    			{
    				setDirx(-1);
    			}
    			if(keyC==KeyEvent.VK_SPACE)
    			{
    				if(readyToShoot)
    				{					
    					r1=new Rectangle(xr,yr,3,5);
    					shot=true;
    
    				}
    			}
    			
    		}
    		public void keyReleased(KeyEvent e)
    		{
    			int keyC=e.getKeyCode();
    			if(keyC==KeyEvent.VK_RIGHT)
    			{
    				setDirx(0);
    			}
    			if(keyC==KeyEvent.VK_LEFT)
    			{
    				setDirx(0);
    			}
    			if(keyC==KeyEvent.VK_SPACE)
    			{
    				readyToShoot=false;
    				
    			}
    			
    		}
    		
    	}
    }
    This is the error I get(it's the same repeated many times):
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at Sparo.paint(Sparo.java:87)
    at javax.swing.RepaintManager$3.run(Unknown Source)
    at javax.swing.RepaintManager$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(Unknown Source)
    at javax.swing.RepaintManager.paintDirtyRegions(Unkno wn Source)
    at javax.swing.RepaintManager.paintDirtyRegions(Unkno wn Source)
    at javax.swing.RepaintManager.prePaintDirtyRegions(Un known Source)
    at javax.swing.RepaintManager.access$1100(Unknown Source)
    at javax.swing.RepaintManager$ProcessingRunnable.run( Unknown Source)
    at java.awt.event.InvocationEvent.dispatch(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$200(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(U nknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

    The program runs fine, but I still get the error, so please could you help me? Thanks.

  2. #2
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,994
    Rep Power
    5

    Default Re: Problem with code.

    I don't see you actually giving the r1 attribute a value somewhere. I would expect you to assign a new Rectangle() instance to it.

    And no the program does not run fine because you get an error.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  3. #3
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default Re: Problem with code.

    At times when the GUI is drawn r1 is null.
    I would add a null check in the paint method, for a quick fix.

    Edit: @gimbal, in the KeyAdapter.

  4. #4
    Kyl9 is offline Member
    Join Date
    Feb 2014
    Posts
    6
    Rep Power
    0

    Default Re: Problem with code.

    Quote Originally Posted by masijade View Post
    At times when the GUI is drawn r1 is null.
    I would add a null check in the paint method, for a quick fix.
    Actually I don't get it. I initialize a new Rectangle r1 before the paint method, so how do I get this error?
    Could you please try to fix it? Thanks.

  5. #5
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,994
    Rep Power
    5

    Default Re: Problem with code.

    No, you have to fix it.

    The problem is on line 87 as the exception stacktrace is telling you. r1 is clearly null. So when is r1 initialized? When you make a shot. You are missing a particular check in that piece of code.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,600
    Rep Power
    5

    Default Re: Problem with code.

    BTW, you should not be painting on a JFrame. Extend JPanel and override paintComponent, not paint. And add the instance of your class to an instance of JFrame. Read--> Painting in AWT and Swing

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  7. #7
    Kyl9 is offline Member
    Join Date
    Feb 2014
    Posts
    6
    Rep Power
    0

    Default Re: Problem with code.

    Strangely I still don't understand where the error is... As you said I initializa r1 when I make a shot, and only after that it draws the bullet. In theory it's correct... :/
    Sorry if I make those questions, they might seem stupid to you but they help me much..

  8. #8
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,600
    Rep Power
    5

    Default Re: Problem with code.

    Stick some print statements in your paint methods and print out r1.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

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

    Default Re: Problem with code.

    only after that it draws the bullet
    Add a println() statement to the paint() method so you can see when it is called and when r1 must have a value.


    EDIT: I was Too slow.
    If you don't understand my response, don't ignore it, ask a question.

  10. #10
    Kyl9 is offline Member
    Join Date
    Feb 2014
    Posts
    6
    Rep Power
    0

    Default Re: Problem with code.

    I've fixed that, thank you very much for your help, everyone.

Similar Threads

  1. have problem with a code help please...
    By eyal3400 in forum Java Applets
    Replies: 2
    Last Post: 07-19-2011, 02:20 AM
  2. Problem in code
    By Pojahn_M in forum New To Java
    Replies: 12
    Last Post: 06-06-2011, 12:12 AM
  3. Replies: 0
    Last Post: 02-21-2011, 11:50 AM
  4. Replies: 9
    Last Post: 09-21-2010, 04:15 PM
  5. Help me!!!!!!!! Problem with Code!!
    By Miyaki in forum New To Java
    Replies: 0
    Last Post: 03-08-2009, 12:26 AM

Posting Permissions

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