【问题标题】:Cannot locate cause of nullpointer when using .getWidth()使用 .getWidth() 时找不到空指针的原因
【发布时间】:2015-09-12 06:11:59
【问题描述】:

我很难找到为什么我在这段代码中返回了一个空指针:

import java.awt.Graphics2D;
import java.awt.Rectangle;

public class Move {

  private static final int DIAMETER = 30;

  int x = 0;
  int y = 0;
  int x_1 = 1;
  int y_1 = 1;


  private GameStart game;

  public void Ball(GameStart game) {
    this.game = game;
  }

  void moveBall() {
    if (x + x_1 < 0) {
        x_1 = 1;
    }

    if (x + x_1 > game.getWidth() - DIAMETER) {
        x_1 = -1;
    }

    if (y + y_1 < 0) {
        y_1 = 1;
    }

    if (y + y_1 > game.getHeight() - DIAMETER) {
        game.gameOver();
    }

    if (collision()) {
        y_1 = -1;
        y = game.racquet.getTopY() - DIAMETER;
    }

    x = x + x_1;
    y = y + y_1;
  }

  private boolean collision() {
    return game.racquet.getBounds().intersects(getBounds());
  }

  public void paint(Graphics2D g) {
    g.fillOval(x, y, 30, 30);
  }

  public Rectangle getBounds() {
    return new Rectangle(x, y, DIAMETER, DIAMETER);
  }

}

GameStart 类在这里:

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.KeyEvent;

@SuppressWarnings("serial")
public class GameStart extends JPanel {

  Move move_ball = new Move();
  Racquet racquet = new Racquet(this);

  public GameStart() {

    addKeyListener(new MyKeyListener() {
        public void keyTyped(KeyEvent e) {          
        }

        public void keyReleased(KeyEvent e) {
            racquet.keyReleased(e);
        }

        public void keyPressed(KeyEvent e) {
            racquet.keyPressed(e);
        }

    });

    setFocusable(true);

  }

  private void move() {
    move_ball.moveBall();
    racquet.move();
  }

  public void paint(Graphics g) {
    super.paint(g);
    Graphics2D graphics = (Graphics2D) g;
    graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, 
                RenderingHints.VALUE_ANTIALIAS_ON);
    move_ball.paint(graphics);
    racquet.paint(graphics);
  }

  public void gameOver() {
    JOptionPane.showMessageDialog(this, "Spil slut", "Du tabte", JOptionPane.YES_NO_OPTION);
    System.exit(ABORT);
  }

  public static void main (String[] args) throws InterruptedException {

    JFrame mainWindow = new JFrame("Matematikken");
    GameStart game = new GameStart();
    mainWindow.add(game);
    mainWindow.setSize(300, 400);
    mainWindow.setVisible(true);
    mainWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    while (true) {

        game.move();
        game.repaint();
        Thread.sleep(10);

    }

  }

}

我在if (x + x_1 &gt; game.getWidth() - DIAMETER) { 线上得到了一个空指针。我在另一个班级中以同样的方式使用.getWidth(),它工作正常 - 但由于某种原因,它不会在这。我试过打印结果,但这只是另一个空指针。我知道我遗漏了一些东西,但我找不到它。

【问题讨论】:

    标签: java nullpointerexception null-pointer


    【解决方案1】:

    你没有打电话

    public void Ball(GameStart game) {
        this.game = game;
    }
    

    这就是为什么在您的 Move 类中,GameStart 仍然为空。

    【讨论】:

    • 我刚刚尝试删除 this.game = game 作为另一个帖子,建议使用另一个 NPE,但没有运气。使用 this.game.getWidth() 也会返回 NPE。还是我误会了你?
    • 你为什么有那个方法? Ball 是什么?是复制粘贴的错别字吗?我认为 Seelenvirtouse 的预感是正确的。
    • 我不是直接复制粘贴——而是跟随:edu4java.com/en/game/game6.html 我可以看到我没有调用 Ball 构造函数。这应该是问题吧?
    • 感谢您为我指明正确的方向,而不仅仅是粘贴一个工作示例!干杯人!
    • 很高兴帮助愿意学习而不仅仅是复制代码的人:)
    【解决方案2】:

    好像你想要那个方法

    public void Ball(GameStart game) { this.game = game; }
    

    实际上是一个构造函数:

    public Move(GameStart game) { this.game = game; }
    

    然后,您必须在实例化Move 时使用此构造函数(因为不再有无参数构造函数):

    Move move_ball = new Move(this);
    

    【讨论】:

      【解决方案3】:

      您尚未将Move 类型的实例move_ball 的成员变量game 设置为一个值,因此它是null,因此会引发NullPointerException。

      您需要实例化这个成员变量,例如通过调用move_ball.Ball(&lt;some Game instance&gt;)

      另外,请不要让非构造函数的方法以大写字母开头,并且请在整个过程中使用 camelCase 而不是在名称格式之间切换。它使您的代码难以阅读。

      【讨论】:

        【解决方案4】:

        您没有通过Ball 方法将GameStart 传递给Move。这是一个非常具有误导性的名字。它应该是setGame 或其他东西。

        【讨论】:

          【解决方案5】:

          您应该为 Move 类创建一个构造函数,它将游戏作为参数,并将该参数存储在类中的变量中。

          例子:

          Public Move(GameStart mygame){
              game = mygame;
          }
          

          这应该允许您使用您正在创建移动的游戏方法。它基本上会取代您拥有的“Ball”方法。

          【讨论】:

            猜你喜欢
            • 2020-01-26
            • 2023-03-21
            • 1970-01-01
            • 2011-02-03
            • 2016-07-03
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2016-09-16
            相关资源
            最近更新 更多