This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12个答案)
5年前关闭。
好的,我在这里要做的是创建一个游戏,在该游戏中,通过用鼠标单击对象,向后拉鼠标(向后拉鼠标的距离将确定对象的速度)来控制对象/球释放对象将移动的按钮(它的移动角度也将取决于鼠标向后拉的角度)
现在,据我所知,“算术”至少应该用于工作,但不幸的是,由于出现以下错误,我无法对其进行测试。
从我的理解来看,这意味着我可能已经声明了一个具有空值的东西,但这使我感到困惑,因为我已经完成了所有变量的打印输出,并且都给出了值。
然后,我假设也许我是在声明值之前告诉对象移动,但是我不知道为什么会发生这种情况。
我的代码如下。
这将是似乎正在发生错误的第二个类,我在上面的类中调用一个实例变量。
}
最后,初始化级别的类。
}
任何人都可以帮助我解决我在这里做错的事情,这种级别的编码对于我来说还是很新的,因此请像在白痴说话一样进行解释。非常感谢您的宝贵时间! 您应该分配值
(12个答案)
5年前关闭。
好的,我在这里要做的是创建一个游戏,在该游戏中,通过用鼠标单击对象,向后拉鼠标(向后拉鼠标的距离将确定对象的速度)来控制对象/球释放对象将移动的按钮(它的移动角度也将取决于鼠标向后拉的角度)
现在,据我所知,“算术”至少应该用于工作,但不幸的是,由于出现以下错误,我无法对其进行测试。
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);
}
}
任何人都可以帮助我解决我在这里做错的事情,这种级别的编码对于我来说还是很新的,因此请像在白痴说话一样进行解释。非常感谢您的宝贵时间!
最佳答案
您的Level l
对象似乎是null
。由于尚未初始化它,因此您尝试在抛出NullPointerException的moveUp()
方法中使用它。
您应该按如下所示对其进行初始化。
Level l = new Level();
double x = 1.0;
。我到处都能看到double x = 1;
,这可能会给您带来意想不到的结果。 关于线程 "AWT-EventQueue-0"中的Java异常java.lang.NullPointerException,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15578604/
10-13 09:41