Results 1 to 7 of 7
  1. #1
    scotthdez is offline Member
    Join Date
    Jul 2013
    Posts
    6
    Rep Power
    0

    Default Null Pointer Exception. Help Needed!!

    Hi I'm new to java and im trying to make a ping pong game put when I'm trying to move the ball i keep getting a null pointer exception and I cant figure out how to fix it. If you no how please leave a reply, Thank You!


    Main.java

    Java Code:
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.Image;
    
    import javax.swing.JFrame;
    
    public class Main extends JFrame implements Runnable {
    	private static final long serialVersionUID = 1L;
    	
    	public static String title = "Ping Pong v1.0";
    	public static Dimension size = new Dimension(600, 500);
    	
    	public static boolean running = false;
    	
    	Thread thread;
    	
    	private Image dbImage;
    	private Graphics dbg;
    	
    	public static Ball ball;
    		
    	public Main() {
    		thread = new Thread(this, "Display");
    		setPreferredSize(size);
    		setMaximumSize(size);
    		setMinimumSize(size);
    
    		setTitle(title);
    		pack();
    		setResizable(false);
    		setLocationRelativeTo(null);
    		setFocusable(true);		
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setBackground(Color.BLACK);
    		setVisible(true);
    		
    		start();
    		ball = new Ball(5, 5);		
    	}
    	
    	public synchronized void start() {
    		running = true;
    		thread.start();
    	}
    	
    	public synchronized void stop() {
    		running = false;
    		try {
    			thread.join();
    		} catch (InterruptedException e) {
    			e.printStackTrace();
    		}
    	}
    	
    	@Override
    	public void run() {
    			try {
    				while(running) {
    					tick();
    					Thread.sleep(9);					
    				}
    			} catch (Exception e) {
    				e.printStackTrace();
    			}
    	}
    	
    	public void tick() {
    		ball.tick();
    	}
    	
    	public void paint(Graphics g) {
    		dbImage = createImage(getWidth(), getHeight());
    		dbg = dbImage.getGraphics();
    		render(dbg);
    		g.drawImage(dbImage, 0, 0, this);
    	}
    
    	public void render(Graphics g) {
    		ball.render(g);
    	}
    	
    	public static void main(String[] args) {
    		Main main = new Main();
    	}
    }

    Ball.java


    Java Code:
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Rectangle;
    import java.util.Random;
    
    public class Ball {
    	
    	int width, height;
    	
    	Rectangle Ball = new Rectangle(100, 100, width, height);
    	
    	public int x = Ball.x;
    	public int y = Ball.y;
    	
    	public Ball(int width, int height) {
    		this.width = width;
    		this.height = height;
    	}
    	
    	public void tick() {
    		physics();
    	}
    		
    		public void physics() {
    			Random r = new Random(2);
    			int side = r.nextInt();
    			if (side == 0) {
    				x--;
    			} else {
    				x++;
    			}
    	}
    	
    	public void render(Graphics g) {
    		g.setColor(Color.WHITE);
    		Ball.setBounds(Main.size.width / 2, Main.size.height / 2, width, height);
    		g.fillRect(Ball.x, Ball.y, Ball.width, Ball.height);
    	}
    	
    }
    and the error I keep getting is:

    java.lang.NullPointerException
    at Main.tick(Main.java:69)
    at Main.run(Main.java:60)
    at java.lang.Thread.run(Thread.java:722)

    i just fixed it somehow messing around with it thanks anyways
    Last edited by scotthdez; 07-22-2013 at 02:48 AM.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,453
    Rep Power
    20

    Default Re: Null Pointer Exception. Help Needed!!

    Please go through the Forum Rules, particularly the second paragraph. The other thread you started in Java2D has been removed.

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

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,453
    Rep Power
    20

    Default Re: Null Pointer Exception. Help Needed!!

    Quote Originally Posted by scotthdez View Post
    the error I keep getting is:

    java.lang.NullPointerException
    at Main.tick(Main.java:69)
    at Main.run(Main.java:60)
    at java.lang.Thread.run(Thread.java:722)
    As the code you posted isn't the code you're running, only you know which is line 69. Find which variable on that line is null -- hasn't been initialized --and correct that.

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

  4. #4
    scotthdez is offline Member
    Join Date
    Jul 2013
    Posts
    6
    Rep Power
    0

    Default Re: Null Pointer Exception. Help Needed!!

    ill repost it with the imports so its the exact code, because i have been trying to figure out why it hasnt been working and i cant

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,453
    Rep Power
    20

    Default Re: Null Pointer Exception. Help Needed!!

    You could just inspect line 69.

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

  6. #6
    scotthdez is offline Member
    Join Date
    Jul 2013
    Posts
    6
    Rep Power
    0

    Default Re: Null Pointer Exception. Help Needed!!

    i have but, cant figure out why it isn't working

  7. #7
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    760
    Rep Power
    8

    Default Re: Null Pointer Exception. Help Needed!!

    So what have you found? The 69 line only contain this statement ball.tick(); If that line throwing a NullPointerException then the ball must be null in there. You can start to debug your code why the ball in that line is null.

    One thing that I spot in your code is that you instantiated the Ball object after you started the thread. This might give you a clue.
    Last edited by wsaryada; 07-22-2013 at 04:45 AM.

Similar Threads

  1. Null pointer exception
    By mathidioticz in forum New To Java
    Replies: 12
    Last Post: 02-04-2012, 07:38 PM
  2. Null Pointer Exception
    By jonytek in forum New To Java
    Replies: 5
    Last Post: 03-02-2011, 08:16 AM
  3. null pointer exception
    By anthonym2121 in forum New To Java
    Replies: 7
    Last Post: 04-06-2009, 04:25 AM
  4. Null Pointer Exception
    By ScKaSx in forum New To Java
    Replies: 1
    Last Post: 01-24-2009, 12:27 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
  •