- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
当我使用 ACM 库编译和运行 Java 程序时,我不断收到此错误
Exception in thread "Thread-3" java.lang.NullPointerException
at SpaceTravel.getBlackHoleDistance(SpaceTravel.java:148)
at SpaceTravel.gameOverBlackHole(SpaceTravel.java:132)
at BlackHole.oneTimeStep(BlackHole.java:84)
at BlackHole.run(BlackHole.java:45)
at java.lang.Thread.run(Unknown Source)
这是游戏类:
import acm.graphics.*;
import acm.program.*;
import acm.util.*;
import java.awt.*;
public class SpaceTravel extends GraphicsProgram {
// specify the size of the window
public static int APPLICATION_WIDTH = 1280;
public static int APPLICATION_HEIGHT = 600;
// class constants
private static final double
PLANET_SIZE = 80,
BLACK_HOLE_SIZE = 100,
STAR_SIZE = 2;
// instance variables
private GOval ice, fire, iron;
private GPoint lastPoint;
private boolean isDragging = false;
private GLabel gameOverText, goal, win;
private BlackHole blackhole1, blackhole2;
private RandomGenerator rand = new RandomGenerator();
// init method, draw the graphics objects
public void init() {
setBackground(Color.BLACK);
// call the randomly colored stars method
drawStars();
// call 1 instance of BlackHole class
blackhole1 = new BlackHole(BLACK_HOLE_SIZE, 5, 2, this);
add(blackhole1, APPLICATION_WIDTH-400, APPLICATION_HEIGHT/2 );
new Thread(blackhole1).start();
// call 1 instance of BlackHole class, but name it differently
blackhole2 = new BlackHole(BLACK_HOLE_SIZE, 3, -4, this);
add(blackhole2, APPLICATION_WIDTH-200, APPLICATION_HEIGHT/2 );
new Thread(blackhole2).start();
// draw fire planet
fire = drawCircleCentered(100, 400, PLANET_SIZE);
add(fire);
fire.setFilled(true);
fire.setColor(Color.RED);
// draw ice planet
ice = drawCircleCentered(100, 100, PLANET_SIZE);
add(ice);
ice.setFilled(true);
ice.setColor(Color.BLUE);
// draw iron planet
iron = drawCircleCentered(100, 250, PLANET_SIZE);
add(iron);
iron.setFilled(true);
Color grey = new Color(34, 34, 34);
iron.setColor(grey);
// game over text
gameOverText = new GLabel ("GAME OVER", APPLICATION_WIDTH/2 - 250, APPLICATION_HEIGHT/2);
gameOverText.setColor(Color.RED);
gameOverText.setFont(new Font("DEFAULT_FONT", Font.BOLD, 90));
// goal text
goal = new GLabel ("GOAL", APPLICATION_WIDTH-150, APPLICATION_HEIGHT/2);
goal.setColor(Color.RED);
goal.setFont(new Font("DEFAULT_FONT", Font.BOLD, 20));
add(goal);
// win text
win = new GLabel ("WINRAR IS YOU!", APPLICATION_WIDTH/2 - 350, APPLICATION_HEIGHT/2);
win.setColor(Color.RED);
win.setFont(new Font("DEFAULT_FONT", Font.BOLD, 90));
}
// checker method if the ice and fire plantes touch, call the game over method below.
private void checkFireIce(GOval fire, GOval ice) {
if(getDistance(fire, ice) < PLANET_SIZE ) {
gameOver(fire);
}
}
// checker method for when fire planet gets to the goal text, call the game winning method below
private void checkPlanetsGoal() {
if(fire.getBounds().intersects(goal.getBounds())) {
winGame();
}
}
// start dragging if the ball is pressed
public void mousePressed(GPoint point) {
if (ice.contains(point)) {
isDragging = true;
lastPoint = point;
}
}
// move the ball when it is dragged, and call checking methods for game winning or game over conditions
public void mouseDragged(GPoint point) {
checkFireIce(fire, ice);
checkPlanetsGoal();
if (isDragging) {
ice.move(point.getX()-lastPoint.getX(),
point.getY()-lastPoint.getY());
lastPoint = point;
// bump the planets
bump(ice, iron);
bump(iron, fire);
}
}
// checking method for if any of the planets have touched an instance of black hole
public void gameOverBlackHole(BlackHole blackhole) {
double a = getBlackHoleDistance(fire, blackhole);
double b = getBlackHoleDistance(ice, blackhole);
double c = getBlackHoleDistance(iron, blackhole);
if(a < BLACK_HOLE_SIZE/2 + PLANET_SIZE/2) {
gameOver(fire);
}
if(b < BLACK_HOLE_SIZE/2 + PLANET_SIZE/2) {
gameOver(ice);
}
if(c < BLACK_HOLE_SIZE/2 + PLANET_SIZE/2) {
gameOver(iron);
}
}
// get distance between a black hole instance and a planet
private double getBlackHoleDistance(GOval planet, BlackHole blackhole) {
return GMath.distance(planet.getX()+PLANET_SIZE/2, planet.getY()+PLANET_SIZE/2,
blackhole.getX(), blackhole.getY());
}
// bump helper method, calculates how much to move a tangent planet by when it is being bumped by another
private void bump(GOval planet1, GOval planet2) {
double offset = PLANET_SIZE+1.5 - getDistance(planet1, planet2);
if (offset > 0) {
planet2.move(offset*(planet2.getX()-planet1.getX())/PLANET_SIZE,
offset*(planet2.getY()-planet1.getY())/PLANET_SIZE);
}
}
// a helper method, compute the distance between the centers of the balls
private double getDistance(GOval planet1, GOval planet2) {
return GMath.distance(planet1.getX()+PLANET_SIZE/2, planet1.getY()+PLANET_SIZE/2,
planet2.getX()+PLANET_SIZE/2, planet2.getY()+PLANET_SIZE/2);
}
// a helper method, draw a circle centered at the given location
private GOval drawCircleCentered(double centerX, double centerY, double size) {
return new GOval(centerX-size/2, centerY-size/2, size, size);
}
// a helper method, draw randomly colored stars
private void drawStars() {
for (int i = 0; i < 1000; i++) {
GOval star = drawCircleCentered(rand.nextDouble(0, APPLICATION_WIDTH), rand.nextDouble(0, APPLICATION_HEIGHT), STAR_SIZE);
add(star);
star.setFilled(true);
star.setColor(rand.nextColor());
}
}
// helper method to switch dragging off when mouse is released from window
public void mouseReleased(GPoint point) {
isDragging = false;
}
// helper method to switch dragging off, remove the planet that touched the goal, and display game over text
public void gameOver(GOval planet) {
isDragging = false;
remove(planet);
add(gameOverText);
}
// helper method to switch dragging off, remove planets, and display win text
private void winGame() {
isDragging = false;
add(win);
remove(fire);
remove(ice);
remove(iron);
remove(goal);
}
}
这个类会创建黑洞,从而结束游戏。
// import libraries
import acm.program.*;
import acm.graphics.*;
import acm.util.*;
import java.awt.*;
import java.util.*;
public class BlackHole extends GCompound implements Runnable {
// instance variables
private double size, xSpeed, ySpeed;
private SpaceTravel game;
private boolean stopHammerTime = false;
// constructor for BlackHole
public BlackHole(double size, double xSpeed, double ySpeed, SpaceTravel game) {
// save the parameters size, xSpeed, ySpeed, centerX, centerY, and game
this.size = size;
this.xSpeed = xSpeed;
this.ySpeed = ySpeed;
this.game = game;
// call method drawBlackhole
drawBlackhole(0, 0, size, 3, 40);
}
// run method, move the black hole until it hits a planet || stopHammerTime = true
public void run() {
while(!stopHammerTime) {
oneTimeStep();
}
}
// helper method, creates a black hole
private void drawBlackhole(double centerX, double centerY, double size, double gap, int layers) {
for (int i = 0; i < layers; i++) {
// this gradient color will lighten each time the for loop completes
Color gradient = new Color(0 + 5*i, 0 + 5*i, 0 + 5*i);
GOval ring = drawCircleCentered(centerX, centerY, size-gap*2*i);
add(ring);
ring.setFilled(true);
ring.setColor(gradient);
}
}
// a helper method, draw a circle centered at the given location
private GOval drawCircleCentered(double centerX, double centerY, double size) {
return new GOval(centerX-size/2, centerY-size/2, size, size);
}
// a helper method, move the blackHole in oneTimeStep
private void oneTimeStep() {
double x = getX();
double y = getY();
// if the black hole hits the left or the right wall, reverse the x-speed
if (x < size/2 || x+size/2 > game.getWidth()) xSpeed = -xSpeed;
// if the black hole hits the top or the bottom wall, reverse the y-speed
if (y < size/2 || y+size/2 > game.getHeight()) ySpeed = -ySpeed;
// move the black hole by a small interval, incorporating changes from if statements
move(xSpeed, ySpeed);
// check if a planet has touched a blackhole
game.gameOverBlackHole(this);
// delay
pause(20);
}
}
我很确定我对类的调用是正确的,但由于某种原因,被调用的 blackhole2 崩溃了。
最佳答案
Exception in thread "Thread-3" java.lang.NullPointerException
执行线程“Thread-3”
时发生NullPointerException
位于 SpaceTravel.java
文件中的第 148 行。
似乎在执行Thread-3
时,第148行试图对null
引用执行一些操作,这会导致NullPointerException
。
关于java - thread 3 NullPointerException 在我的代码中意味着什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12733714/
我在 OS X 中构建菜单栏项时找到了一些示例代码。它使用了单个 |我不确定它的实际含义。 (我想做的是在菜单项左键单击时调用一个函数,但在右键单击时显示菜单) 这是我的代码 //Get refere
为什么它在第 23 行抛出编译错误。'a' 是 Apple 类的对象,col 是包含 Apple 对象的列表,但它仍然是抛出下面提到的编译错误: 类型 Collection 中的方法 add(capt
我有一个类A,它扩展了抽象类B。 让B有一个 protected 字段值 我想在 A 中使用这个字段。如果 A 没有 value,我可以编写 this.value 从 B 获取它。 与super.va
DBLint 用于检查数据库状态。有46条规则。在 www.dblint.org 上对每条规则都有一些简单的解释,但对规则 31 的描述如下: 定义的主键不是最小键:主键是最小的 super 键。如果
var aa: (()?) = (john.residence?.address = someAddress) var bb: ()? = john.residence?.printNumberOfR
我对 jquery 的可重用插件有点陌生。我已经多次遇到这段代码,但无法弄清楚究竟发生了什么。 (function( $ ){ ... })( jQuery ); 谁能帮我解答一下吗? 最佳答案
这个问题已经有答案了: int foo (int argc, ...) vs int foo() vs int foo(void) in C (4 个回答) 已关闭 9 年前。 所以我最近在 Hack
typedef struct Element { struct Element *next; void *data; } Element; 在 pop 函数中,(!(elem = *s
数据加载两次...意味着 AsyncTask onPostExecute 加载相同的数据两次?我的 AsyncTask onPostExecute 运行两次它加载相同的数据...我正在运行异步任务以从
运行“yomeanjs”时,我无限期地挂起“这可能需要几分钟”。当我尝试运行 grunt 时,它失败了,与 npm start 相同。 我使用的是 Win 8.1,并安装了最新的 Node 和 Mon
我正在看 big nerd ranch 的“Android Programming”中的这个页面,我对下面的句子感到困惑。它声明“当 Activity 被隐藏时, Activity 对象不存在”。这让
是否 const vector意味着它的元素是const也一样? 在下面的代码中, v[0].set (1234);在 void g ( const vector & v )产生编译器错误 const
我是 xml 相关事物的新手 我无法理解: SelectNodes(@"//Form/*[. = 'on']"); 注:SelectNodes是 XmlNode 的函数.(与 XmlDocument
我想方便地控制命令行参数。因此我想使用 ShellLib。 我的代码是这样的: ... #include ... EFI_STATUS EFIAPI UefiMain ( EFI_HANDL
着眼于更正 Debian 上/etc/init.d/hostapd 中的一个问题。但是,我不知道这行代码是做什么的,也不知道它是如何工作的 [ -n "$DAEMON_CONF" ] || exit
有没有人遇到过类似我下图所示的情况? 我有一个变量 landingBools.didSlowPast40Knots(正如您从调试打印输出中看到的那样)为假,但出于某种原因,if 语句评估为真。 知道为
我设法使用 flexbox 和一些非常基本的 JavaScript 为自己构建了三个下拉菜单。 因为我不太了解,所以我使用了一个简单的函数三次,而不是使用参数、变量和其他东西。我将其称为丑陋的“蛮力”
这周刚开始学习 javascript。我有一个非常菜鸟的问题。 exports.displayName = (undefined: ?string); 在 React Native 中意味着什么? 这
我正在阅读有关 NaN here 的内容它说: A comparison with a NaN always returns an unordered result even when compari
编码格式:引入*表示“从头开始重复”。例子。输入-{a,b,a,b,c,a,b,a,b,c,d}可以写成{a,b,*,c,*,d}。输出:5;例如2:ABCABCE,输出- 5。 这里*表示从头开始重
我是一名优秀的程序员,十分优秀!