[Solved] Can't fix this error

Hello everyone,
this my real first OOP game, i used to do all my programs in one class, i followed some tutorials and then tried to remake a pong game that i already made but everything was in one class, but i don’t want to do that anymore, things becomes really complicated when i want to fix or re-do something with it, i always get lost in the code,
so,
here is all the codes for the new pong and the error is :

[quote]Exception in thread “AWT-EventQueue-0” java.lang.NullPointerException
at Screen.paintComponent(Screen.java:28)
[/quote]
Frame.Java


import java.awt.Dimension;

import javax.swing.JFrame;


public class Frame extends JFrame  {

	public static Dimension size = new Dimension(800,600);
	public static String title ="Pong2.0";
	public Frame(){
		
		this.setSize(size);
		this.setTitle(title);
		this.setLocationRelativeTo(null);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		init();
		
	}
	
	public void init(){
		
		Screen screen = new Screen();
		
		this.add(screen);
		
		
		
		this.setVisible(true);
		
	}
	
	
	
	
	public static void main(String args[]){
		Frame frame = new Frame();
	}
	
}

Screen.java

import java.awt.Color;
import java.awt.Graphics;

import javax.swing.JPanel;


public class Screen extends JPanel implements Runnable {
	public static Thread thread = new Thread();
	
	public static Ball ball;

	
	public Screen(){
		thread.start();
	}

	public void define(){
		
		ball = new Ball(0, 0, 50, 50);
	}
	
	public void paintComponent(Graphics g){
		
		
		g.setColor(new Color(0,0,0));
		g.fillRect(0, 0, Frame.size.width, Frame.size.height);
		
		g.setColor(new Color(0,50,0));
		ball.draw(g);
		
	}
	
	
	@Override
	public void run() {
		while(true){
			repaint();
			
			
			try {
				Thread.sleep(1);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
}

Ball.java



import java.awt.*;


public class Ball extends Rectangle {
	
	
	public Ball(int x,int y,int width,int height){
		this.setBounds(x, y, width, height);
		
		
	}

	public void draw (Graphics g){
		
		g.drawRect( x, y, width,height);
		
		
	}
}

thank you very much for your help

That’s some crazy OO style you have there.

You are constructing Screen, which starts the thread immediately. This will then reference frame, which has likely not yet actually been assigned.

Cas :slight_smile:

The Ball class member in your Screen class is never created which causes the NPE I think. You have a define() method to create it but it never gets called as far as I can tell.

EDIT: Although I would have then expected the error to report line 29 in that case, if cas in the previous post is correct it would be line 27, odd :clue:

+1 ::slight_smile:
as i said it’s my first time :-
advice, articles, suggestions are very welcome ;D

i don’t understand ???

I’ve highlighted the offending lines, assuming I correctly identified the problem (I didn’t try to compile/run the code).

public class Screen extends JPanel implements Runnable {
public static Thread thread = new Thread();

public static Ball ball; <-- this is the class member that never gets created

public Screen(){
thread.start();
}


This method doesn’t seem to get called anywhere, so ‘ball’ is null.
public void define(){
ball = new Ball(0, 0, 50, 50);
}

public void paintComponent(Graphics g){

  g.setColor(new Color(0,0,0));
  g.fillRect(0, 0, Frame.size.width, Frame.size.height);
  
  g.setColor(new Color(0,50,0));

ball.draw(g); <-- which causes the null pointer exception (NPE) here (I think)

}

@Override
public void run() {
while(true){
repaint();

     try {
        Thread.sleep(1);
     } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
     }
  }

}
}

What are you using to develop the code? Do you have a debugger so you can step through, that’s the best way of tracking down bugs?

Hope this helps.

  • stride

EDIT: Why can’t I make stuff bold within a code block ffs :wink:

[quote]
This method doesn’t seem to get called anywhere, so ‘ball’ is null.
public void define(){
ball = new Ball(0, 0, 50, 50);
}

[/quote]
Biiiiiiig thanx for you :slight_smile: !!!
and even bigger oops for me :stuck_out_tongue:

thank you very much