线程“AWT-EventQueue-0”中的Java异常java.lang.NullPointerException

时间:2013-03-22 19:41:58

标签: java swing nullpointerexception jpanel paintcomponent

好吧我想在这里做的是创建一个游戏,通过用鼠标点击对象来控制对象/球,拉回鼠标(鼠标被拉回的距离将决定对象的速度)并且在释放按钮时,物体将移动(它的移动角度也将取决于鼠标被拉回的角度)

现在我可以告诉'数学',至少应该只是工作,不幸的是我无法测试,因为我收到以下错误。

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Snail.moveUp(Snail.java:35)
at Snail.chill(Snail.java:20)
at Level.mouseReleased(Level.java:120)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(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.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.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)

根据我的理解,这意味着我可能已经声明了一些具有空值的东西,但这让我感到困惑,因为我已经完成了所有变量的打印输出并且都给出了值。

然后我假设也许我告诉对象在声明值之前移动,但我无法弄清楚为什么会发生这种情况。

我的守则如下。

import java.awt.*;
import java.awt.event.*;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.event.MouseInputListener;

@SuppressWarnings("serial")
public class Level extends JPanel implements ActionListener, MouseInputListener{

Timer time;
private Image i;
private Graphics doubleG;
Snail s, s2;

boolean canJump;
int sx2;
int sy2;
int sx3;
int sy3;
double clickX;
double clickY;
double relX;
double relY;
double angle;
double speed;
double distance;

double scalex;
double scaley;
double velocityx = 1;
double velocityy = 1;

public Level(){

    s = new Snail();
    setFocusable(true);

    addMouseListener(this);
    addMouseMotionListener(this);
    //s2 = new Snail(250, 250);
    time = new Timer(17, this);
    time.start();   
}

@Override //double buffer
    public void update(Graphics g) {
        super.update(g);
            if(i == null){
                i = createImage(this.getSize().width, this.getSize().height);
                doubleG = i.getGraphics();
            }

            doubleG.setColor(getBackground());
            doubleG.fillRect(0, 0, this.getSize().width, this.getSize().height);

            doubleG.setColor(getForeground());
            paint(doubleG);

            g.drawImage(i, 0, 0, this);
    }

public void actionPerformed(ActionEvent e){
    s.update(this);
    //s2.update(this);
    repaint();


}

public void paintComponent (Graphics g) {   super.paintComponent(g); 
    s.paint(g);
    //s2.paint(g);

}

@Override
public void mouseClicked(MouseEvent arg0) {
    // TODO Auto-generated method stub

}

@Override
public void mouseEntered(MouseEvent arg0) {
    // TODO Auto-generated method stub

}

@Override
public void mouseExited(MouseEvent arg0) {
    // TODO Auto-generated method stub

}

@Override
public void mousePressed(MouseEvent ep) {
    clickX = ep.getX();
    clickY = ep.getY();
}

@Override
public void mouseReleased(MouseEvent er) {
    relX = er.getX();
    relY = er.getY();
    angle = Math.toRadians(Math.atan2(clickY - relY, clickX - relX));

    distance = Math.hypot(relX-clickX, relY-clickY);
    speed = distance/8;

    scalex = Math.cos(angle);
    scaley = Math.sin(angle);
    velocityx = speed * scalex;
    velocityy = speed * scaley;

    if (speed > 20){
        speed = 20;
    }

    if (canJump) {s.moveUp();

    }

}

@Override
public void mouseDragged(MouseEvent arg0) {
    // TODO Auto-generated method stub

}

@Override
public void mouseMoved(MouseEvent em) {
    sx2 = s.x - s.radius;
    sy2 = s.y + s.radius;
    sx3 = s.x + s.radius;
    sy3 = s.y - s.radius;

    if (em.getX() >= sx2 && em.getX() <= sx3 && em.getY() >= sy3 && em.getY() <= sy2){
        canJump = true;
    }else{
        canJump = false;
    }
    System.out.println("Click   X " + clickX + " Click   Y " + clickY);
    System.out.println("Release X " + relX + " Release Y " + relY);
    System.out.println("angle" + angle);
    System.out.println("distance" + distance);
    System.out.println("velx " + velocityx);
    System.out.println("vely " + velocityy);
    System.out.println(scalex);
    System.out.println(scaley);
    System.out.println(" ");

}

}

这将是错误似乎发生的第二个类,我正在从上面的类调用一个实例变量。

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


public class Snail {

Level l;
private double gravity = 10;
private double energyloss = 0.6;
private double xFriction = .95;
private double dt = .2;
int x = 0;
int y = 0;
double dx = 0;
double dy = 0;
int radius = 30;


public void chill(){
    moveUp();
}

public Snail() {
    // TODO Auto-generated constructor stub
}

public Snail(int i, int j) {
    // TODO Auto-generated constructor stub
    x = i;
    y = j;
}

public void moveUp() {
        dx = dx + l.velocityx;
        dy = dy + l.velocityy;
} 

public void update(Level l){

    if (x + dx > l.getWidth() -radius){
        x = l.getWidth() -radius;
        dx = - dx;
    }else if( x + dx < 0 +radius){
        x = 0+radius;
        dx = -dx;
    }
    else{
        x += dx;
    }

    if(y == l.getHeight()-radius)
        dx *= xFriction;
    if (Math.abs(dx) < .9){
        dx = 0;
    }

    if (y > l.getHeight() -radius){
        y = l.getHeight() -radius;
        dy *= energyloss;
        dy = - dy;
    }else{
        //velocity formula
        dy = dy + gravity *dt;
        //position formula
        y += dy*dt + .5*gravity*dt*dt;
    }

}

public void paint(Graphics g){
    g.setColor(Color.RED);
    g.fillOval(x-radius, y-radius, radius*2, radius*2);
}

}

最后是初始化关卡的类。

import javax.swing.JFrame;

public class Frame {


public static void main(String[] args){
    JFrame frame = new JFrame("Hard as Snails");
    frame.add(new Level());
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(1280,720);
    frame.setVisible(true);

}

}

任何人都可以帮助我解决我在这里做错了什么,我对这个级别的编码也很新,所以请解释好像和一个白痴交谈。非常感谢你的时间!

1 个答案:

答案 0 :(得分:7)

您的Level l对象似乎是null。因为你没有初始化它而你试图在moveUp()方法中使用它来抛出NullPointerException。

您应该按如下方式对其进行初始化。

 Level l = new Level();
  1. 您应该指定值double x = 1.0;。我可以在任何地方看到double x = 1;,这可能会给你带来意想不到的结果。