Graphics - Method Calling Not WorkingJava NullPointerException

De openkb
Aller à : Navigation, rechercher

Sommaire

Questions

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...

Answers

    • I don t see any where ea is begin initialised...
    http://www.oracle.com/technetwork/java/painting-140037.html http://www.oracle.com/technetwork/java/painting-140037.html

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

Source

License : cc by-sa 3.0

http://stackoverflow.com/questions/18028431/method-calling-not-workingjava-nullpointerexception

Related

Outils personnels
Espaces de noms

Variantes
Actions
Navigation
Outils