I am trying to get a paddleball game working and as i create my paintComponent i have it call a method from a different class and its giving me this error
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at pong.panelc.paintComponent(panelc.java:48)
I pasted the code below minus movement and listener methods and its giving me the same errors.
import java.awt.Color; import java.awt.Graphics; import java.awt.event.ActionListener; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.Timer; public class pongTest extends JPanel{ int displayH; int displayW; int paddlex; panelc display; ballc ball; paddlec paddle; public static void main(String[] args) { // TODO Auto-generated method stub pongTest obj = new pongTest(); } pongTest() { display = new panelc(); displayH = display.getHeight(); displayW = display.getWidth(); paddle = new paddlec(40,300); ball = new ballc(20); } class panelc extends JPanel { int height; int width; JFrame frameD; pongTest control; panelc() { width = getWidth(); height = getHeight(); frameD = new JFrame(); frameD.setSize(640,480); frameD.setResizable(false); frameD.getContentPane().add(this); frameD.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frameD.setVisible(true); } public void paintComponent(Graphics g) { super.paintComponent(g); setBackground(Color.BLACK); g.fillRect(0, 0, frameD.getWidth(), frameD.getHeight()); control.draw(g); } } class paddlec extends JPanel //paddle class { int Length; int width; int positionX; int positionY; int paddleCenter; paddlec(int length, int positionx) { width = 5; Length = length; positionX = positionx; positionY = 30; paddleCenter = (positionx + (length / 2)); } public void paintComponent(Graphics g) { g.fillRect(positionX,positionY,Length,width); g.setColor(Color.ORANGE); } } class ballc extends JPanel //ball class { int size; // size of ball int positionX; // x position of ball int positionY; // y position int movementX; // movement speed in x direction int movementY; // in y direction int pheight; // display panel height int pwidth; // display panel width ballc(int sizex) // constructor setting size position and display size and movement speed { size = sizex; positionX = 0; positionY = 0; movementX = (size / 4); movementY = (size / 4); } public void paintComponent(Graphics g) { g.setColor(Color.BLUE); g.fillOval(positionX, positionY, size, size); } } public void draw(Graphics g) { if ((paddle != null) && (ball != null)) { paddle.repaint(); ball.repaint(); } } }
I can get the black background to show sometimes but not reliably and never with the ball or paddle.
any help is appreciated thanks in advance.