0

Here is my main code:

package main;
  import java.awt.*;
  import java.awt.event.*;

  import javax.swing.*;






    public class Panel extends JPanel{
public Thread update;
public YourPaddle pa;
public EnemyPaddle ea;
public static int fps;
public static long lasttimechecked = System.nanoTime();
public static int frames = 0;
public Panel(){
    addKeyListener(new TAdapter());
    setFocusable(true);
    setBackground(Color.BLACK);
    setDoubleBuffered(true);

    pa = new YourPaddle();
    update = new Thread(){
        public void run(){
            for (int i = 0; i < 2; ) {
                pa.gor();
                try{
                ea.gorg();
                }catch(Exception e){

                }
                repaint();
                try {
                    Thread.sleep(2);
                }catch(Exception e){

                            }
                frames++;
                if (System.nanoTime() - lasttimechecked >= 1000000000){
                    fps = frames;
                    frames = 0;
                    lasttimechecked = System.nanoTime();
                }

            }

        }
    };

    update.start();
}

public void paint(Graphics g){      
    super.paint(g);
    g.setColor(Color.white);
    g.drawLine(300, 10, 300, 340);      
    g.drawImage(pa.i, 20, pa.getY(), this);

    g.drawImage(ea.i, 550, ea.getYy(), this);

    g.drawString("FPS: " + fps + "   Y: " + pa.getY(), 10,17);
    Toolkit.getDefaultToolkit().sync();
    g.dispose();
}

 private class TAdapter extends KeyAdapter {

        public void keyReleased(KeyEvent e) {
            pa.keyReleased(e);
            ea.keyReleased(e);
       }

       public void keyPressed(KeyEvent e) {
            pa.keyPressed(e);
            ea.keyReleased(e);
        }
    }

        }

Here's the thing basically, the4 game is pong and there is one paddle named pa in this class. The pa paddle works, but i copied the same thing into another class but renamed all of the methods. When i run the program, pa paddle works but the enemypaddle(ea) gives me an error Saying the method is a null pointer exception. Here's the stack trace:

    Exception in thread "Thread-2" java.lang.NullPointerException
at main.Panel$1.run(Panel.java:31)
     Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at main.Panel.paint(Panel.java:61)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JLayeredPane.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paintToOffscreen(Unknown Source)
at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(Unknown Source)
at javax.swing.RepaintManager$PaintManager.paint(Unknown Source)
at javax.swing.RepaintManager.paint(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at java.awt.GraphicsCallback$PaintCallback.run(Unknown Source)
at sun.awt.SunGraphicsCallback.runOneComponent(Unknown Source)
at sun.awt.SunGraphicsCallback.runComponents(Unknown Source)
at java.awt.Container.paint(Unknown Source)
at java.awt.Window.paint(Unknown Source)
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.doIntersectionPrivilege(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.prePaintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.access$1000(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.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

Can anyone say why only one paddle wont work? it's the same thing...

3
  • The "method" isn't a NullPointerException, but rather it throws this. To find out why show the line that causes this. Commented Aug 3, 2013 at 1:47
  • Format your code properly, before you paste it here. And if you can read it easily, you might be able to find the bug yourself.. Commented Aug 3, 2013 at 2:06
  • Did you look at the indicated lines (lines 31 and 61) to see what variable might be null, and why? READ AND INVESTIGATE ERROR MESSAGES. And try calling your class something other than 'Panel'. A multi-purpose class or Java UI component might correctly be called 'Panel'.. your class should not. Call it 'PongGame', 'PongPanel' or something applicable. 'Panel' on its own is incorrect. Commented Aug 3, 2013 at 2:10

1 Answer 1

2
  1. I don't see any where ea is begin initialised...
  2. I would avoid overriding paint and use paintComponent instead. Check out Performing Custom Painting and Painting in AWT and Swing.

This might be a good time to start working with the debugger ;)

Sign up to request clarification or add additional context in comments.

1 Comment

Correct, 'pa' is initialized but 'ea' is not.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.