gpt4 book ai didi

java - mouseMoved 给出空异常指针 - 完全困惑(更多 Stanford CS106 痛苦)

转载 作者:行者123 更新时间:2023-11-29 03:44:36 26 4
gpt4 key购买 nike

我已经完美地完成了这个练习:

import acm.program.*;
import acm.graphics.*;
import java.awt.event.*;
public class ProgramHierarchy extends GraphicsProgram {
public void run() {
Paddle = new GRect(0,getHeight() - 30,100,20);
add(Paddle);
addMouseListeners();
}

public void mouseMoved(MouseEvent e) {
if(e.getX() - Paddle.getWidth()>0) {
Paddle.move(e.getX()-Paddle.getX()-Paddle.getWidth(), 0);
}
}

private GRect Paddle;
}

但是,当我在一个更大的项目中使用完全相同的代码时,我的 eclipse 会抛出空指针异常,甚至会完全崩溃家人听到。我的 eclipse 副本损坏了吗?可能发生了什么?

这是我的大型硬崩溃项目。

import acm.graphics.*;
import acm.program.*;
import acm.util.*;
import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class Breakout extends GraphicsProgram {

public static final int APPLICATION_WIDTH = 400;
public static final int APPLICATION_HEIGHT = 600;
private static final int WIDTH = APPLICATION_WIDTH;
private static final int HEIGHT = APPLICATION_HEIGHT;
private static final int PADDLE_WIDTH = 60;
private static final int PADDLE_HEIGHT = 10;
private static final int PADDLE_Y_OFFSET = 30;
private static final int NBRICKS_PER_ROW = 10;
private static final int NBRICK_ROWS = 10;
private static final int BRICK_SEP = 3;
private static final int BRICK_WIDTH =
(WIDTH - (NBRICKS_PER_ROW - 1) * BRICK_SEP) / NBRICKS_PER_ROW;
private static final int BRICK_HEIGHT = 8;
private static final int BALL_RADIUS = 10;
private static final int BRICK_Y_OFFSET = 70;
private static final int NTURNS = 3;
private static final int DELAY = 30;



public void run() {
setup();
GRect Paddle = new GRect(0,HEIGHT - PADDLE_HEIGHT - PADDLE_Y_OFFSET,PADDLE_WIDTH,PADDLE_HEIGHT);
add(Paddle);
addMouseListeners();
}







private void setup() {

int xStart = (WIDTH - ((NBRICKS_PER_ROW * BRICK_WIDTH) + ((NBRICKS_PER_ROW -1) * BRICK_SEP)))/2;

int y = BRICK_Y_OFFSET;

for (int i = 0; i< NBRICK_ROWS; i++) {
int x = xStart;

for (int j = 0; j < NBRICKS_PER_ROW; j++) {
GRect block = new GRect(x, y, BRICK_WIDTH, BRICK_HEIGHT);
if (i<2) {
block.setColor(Color.RED);
block.setFilled(true);
block.setFillColor(Color.red);
}
if (i==2||i==3) {
block.setColor(Color.orange);
block.setFilled(true);
block.setFillColor(Color.orange);
}
if (i==4||i==5) {
block.setColor(Color.yellow);
block.setFilled(true);
block.setFillColor(Color.yellow);
}
if (i==6||i==7) {
block.setColor(Color.green);
block.setFilled(true);
block.setFillColor(Color.green);
}
if (i==8||i==9) {
block.setColor(Color.cyan);
block.setFilled(true);
block.setFillColor(Color.cyan);
}
add (block);
x=x+BRICK_WIDTH + BRICK_SEP;
}
y=y+BRICK_HEIGHT+BRICK_SEP;
}
}


public void mouseMoved(MouseEvent PadX) {
if(PadX.getX() - Paddle.getWidth()>0) {
Paddle.move(PadX.getX()-Paddle.getX()-Paddle.getWidth(), 0);
}
}


private GRect Paddle;
}

最佳答案

您正在隐藏 Paddle 变量:在类中声明它,但在 run 方法中重新声明它。类中保存的 Paddle 变量从未初始化,因此它保持为空。解决方法是不要在 run 方法中重新声明这个变量。

例如,在您的有效代码中,您有:

public void run() {
Paddle = new GRect(0,getHeight() - 30,100,20); // **** notice the difference?
add(Paddle);
addMouseListeners();
}

在不起作用的代码中:

public void run() {
setup();

// notice that you re-declare Paddle here!
GRect Paddle = new GRect(0,HEIGHT - PADDLE_HEIGHT - PADDLE_Y_OFFSET,PADDLE_WIDTH,PADDLE_HEIGHT);
add(Paddle);
addMouseListeners();
}

上面初始化的 Paddle 变量仅在 run 方法中可见,而在您的类中的其他任何地方都看不到。具有相同名称的类变量保持为空。

解决方案:不要重新声明 Paddle:

public void run() {
setup();

// Now we don't re-declare Paddle
Paddle = new GRect(0,HEIGHT - PADDLE_HEIGHT - PADDLE_Y_OFFSET,PADDLE_WIDTH,PADDLE_HEIGHT);
add(Paddle);
addMouseListeners();
}

关于java - mouseMoved 给出空异常指针 - 完全困惑(更多 Stanford CS106 痛苦),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11604594/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com