以下程序很簡單,應該畫出一個落在重力影響下的球,然後彈回。該程序似乎編譯,但我不斷收到異常錯誤。重力移動球
在類球中,函數運動包含計算速度和位移的物理公式。
包重力;
import java.awt.Color;
import java.awt.Graphics;
public class Ball extends StartingPoint{
/**
*
*/
private static final long serialVersionUID = 1L;
public int x;
public int y;
private static final double dt = .2;
private static final double g = 9.8;
public double vy = 0;
Ball(int x, int y){
this.x = x;
this.y = y;
}
public void move(){
vy += g*dt;
y += (1/2)*g * dt*dt + vy * dt;
if (y >= (this.getHeight() - 20 -1)){
y = this.getHeight() - 20 -1;
vy = -vy;}
}
public void paintBALL(Graphics g){
g.setColor(Color.LIGHT_GRAY);
g.fillOval(x, y, 20, 20);
}
}
package gravity;
import java.applet.Applet;
import java.awt.Graphics;
public class StartingPoint extends Applet {
/**
*
*/
private static final long serialVersionUID = 1L;
public int x = 400, y = 25;
Ball ball;
//private static final int nbBalls = 1;
@Override
public void init() {
setSize(800, 600);
}
@Override
public void start() {
Ball ball = new Ball(x,y);
Thread thread = new Thread(new RunBall(ball));
thread.start();
}
public class RunBall implements Runnable{
private Ball ball;
RunBall(Ball ball){
this.ball = ball;
}
@Override
public void run() {
while(true){
ball.move();
try {
Thread.sleep(17);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
repaint();
}
}
}
@Override
public void stop() {
}
@Override
public void paint(Graphics g) {
ball.paintBALL(g);
}
}
編譯器不斷添加以下exeption:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at gravity.StartingPoint.paint(StartingPoint.java:61)
at java.awt.Container.update(Container.java:1835)
at sun.awt.RepaintArea.updateComponent(RepaintArea.java:267)
at sun.awt.RepaintArea.paint(RepaintArea.java:233)
at apple.awt.ComponentModel.handleEvent(ComponentModel.java:263)
at java.awt.Component.dispatchEventImpl(Component.java:4852)
at java.awt.Container.dispatchEventImpl(Container.java:2142)
at java.awt.Component.dispatchEvent(Component.java:4604)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:717)
at java.awt.EventQueue.access$400(EventQueue.java:82)
at java.awt.EventQueue$2.run(EventQueue.java:676)
at java.awt.EventQueue$2.run(EventQueue.java:674)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:86)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:690)
at java.awt.EventQueue$3.run(EventQueue.java:688)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:86)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:687)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:296)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:211)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:196)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:188)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
可能重複[?什麼是空指針異常,以及如何修復它(http://stackoverflow.com/questions/218384 /什麼,是一個無效指針異常和如何-DO-I-FIX-IT) – azurefrog 2014-10-11 20:05:41