java - 使用代码获取黄色错误/警告

标签 java serialization error-handling compiler-errors

在我们开始之前,我需要一个解决方案,其中不涉及向显示的类添加任何额外的代码,它必须保持原样。不知道这是否有可能,但如果不是id,我也想找到那个,以便我能解决真正的问题。

嘿,iv给了我一半的代码来完成我的作业,并被告知使用他们给我的2个类创建缺少的类并修复所有错误,而永不更改给定类上的任何内容。到目前为止,iv用自动生成的方法和东西制作了所有类,我设法修复了所有红色错误,但是代码在编译时仍会给我错误:/所以我认为也许与这个黄色错误有关,并且过了几天我不知道如何解决它:(所以我希望你们会知道这个黄色错误的意思。

错误1:GameManager

public class GameManager extends JFrame implements KeyListener {

它说游戏管理器“serializable类未声明静态的最终serialVersionUID字段。”但我不能添加任何代码到此类:/

编译错误的屏幕快照:http://prntscr.com/je580m

完整代码:
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;
import java.util.Random;
import javax.swing.JFrame;

public class GameManager extends JFrame implements KeyListener {

private int canvasWidth;
private int canvasHeight;
private int borderLeft;
private int borderTop;
private BufferedImage canvas;
private Stage stage;
private Enemy[] enemies;
private Player player;
private Goal goal;
private Graphics gameGraphics;
private Graphics canvasGraphics;
private int numEnemies;
private boolean continueGame;

public static void main(String[] args) {
    // During development, you can adjust the values provided in the brackets below
    // as needed. However, your code must work with different/valid combinations
    // of values.
    GameManager managerObj = new GameManager(1920, 1280, 30);
}

public GameManager(int preferredWidth, int preferredHeight, int maxEnemies) {
    this.borderLeft = getInsets().left;
    this.borderTop = getInsets().top;
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    if (screenSize.width < preferredWidth)
        this.canvasWidth = screenSize.width - getInsets().left - getInsets().right;
    else
        this.canvasWidth = preferredWidth - getInsets().left - getInsets().right;
    if (screenSize.height < preferredHeight)
        this.canvasHeight = screenSize.height - getInsets().top - getInsets().bottom;
    else
        this.canvasHeight = preferredHeight - getInsets().top - getInsets().bottom;
    setSize(this.canvasWidth, this.canvasHeight);
    setResizable(false);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setVisible(true);
    addKeyListener(this);
    Random rng = new Random(2);
    this.canvas = new BufferedImage(this.canvasWidth, this.canvasHeight, BufferedImage.TYPE_INT_RGB);
    // Create a Stage object to hold the background images
    this.stage = new Stage();
    // Create a Goal object with its initial x and y coordinates
    this.goal = new Goal(this.canvasWidth / 2, Math.abs(rng.nextInt()) % this.canvasHeight);
    // Create a Player object with its initial x and y coordinates
    this.player = new Player(this.canvasWidth - (Math.abs(rng.nextInt()) % (this.canvasWidth / 2)),
            (Math.abs(rng.nextInt()) % this.canvasHeight));
    // Create the Enemy objects, each with a reference to this (GameManager) object
    // and their initial x and y coordinates.
    this.numEnemies = maxEnemies;
    this.enemies = new Enemy[this.numEnemies];
    for (int i = 0; i < this.numEnemies; i++) {
        this.enemies[i] = new Enemy(this, Math.abs(rng.nextInt()) % (this.canvasWidth / 4),
                Math.abs(rng.nextInt()) % this.canvasHeight);
    }
    this.gameGraphics = getGraphics();
    this.canvasGraphics = this.canvas.getGraphics();
    this.continueGame = true;
    while (this.continueGame) {
        updateCanvas();
    }
    this.stage.setGameOverBackground();
    updateCanvas();
}

public void updateCanvas() {
    long start = System.nanoTime();
    // If the player is alive, this should move the player in the direction of the
    // key that has been pressed
    // Note: See keyPressed and keyReleased methods in the GameManager class.
    this.player.performAction();
    // If the enemy is alive, the enemy must move towards the goal. The goal object
    // is obtained
    // via the GameManager object that is given at the time of creating an Enemy
    // object.
    // Note: The amount that the enemy moves by must be much smaller than that of
    // the player above
    // or else the game becomes too hard to play.
    for (int i = 0; i < this.numEnemies; i++) {
        this.enemies[i].performAction();
    }
    if ((Math.abs(this.goal.getX() - this.player.getX()) < (this.goal.getCurrentImage().getWidth() / 2))
            && (Math.abs(this.goal.getY() - this.player.getY()) < (this.goal.getCurrentImage().getWidth() / 2))) {
        for (int i = 0; i < this.numEnemies; i++) {
            // Sets the image of the enemy to the "dead" image and sets its status to
            // indicate dead
            this.enemies[i].die();
        }
        // Sets the image of the enemy to the "dead" image and sets its status to
        // indicate dead
        this.goal.die();
        // Sets the background of the stage to the finished game background.
        this.stage.setGameOverBackground();
        this.continueGame = false;
    }
    // If an enemy is close to the goal, the player and goal die
    int j = 0;
    while (j < this.numEnemies) {
        if ((Math.abs(this.goal.getX() - this.enemies[j].getX()) < (this.goal.getCurrentImage().getWidth() / 2))
                && (Math.abs(this.goal.getY() - this.enemies[j].getY()) < (this.goal.getCurrentImage().getWidth()
                        / 2))) {
            this.player.die();
            this.goal.die();
            this.stage.setGameOverBackground();
            j = this.numEnemies;
            this.continueGame = false;
        }
        j++;
    }
    try {
        // Draw stage
        this.canvasGraphics.drawImage(stage.getCurrentImage(), 0, 0, null);
        // Draw player
        this.canvasGraphics.drawImage(player.getCurrentImage(),
                this.player.getX() - (this.player.getCurrentImage().getWidth() / 2),
                this.player.getY() - (this.player.getCurrentImage().getHeight() / 2), null);
        // Draw enemies
        for (int i = 0; i < this.numEnemies; i++) {
            this.canvasGraphics.drawImage(this.enemies[i].getCurrentImage(),
                    this.enemies[i].getX() - (this.enemies[i].getCurrentImage().getWidth() / 2),
                    this.enemies[i].getY() - (this.enemies[i].getCurrentImage().getHeight() / 2), null);
        }
        // Draw goal
        this.canvasGraphics.drawImage(this.goal.getCurrentImage(),
                this.goal.getX() - (this.goal.getCurrentImage().getWidth() / 2),
                this.goal.getY() - (this.goal.getCurrentImage().getHeight() / 2), null);
    } catch (Exception e) {
        System.err.println(e.getMessage());
    }
    // Draw everything.
    this.gameGraphics.drawImage(this.canvas, this.borderLeft, this.borderTop, this);
    long end = System.nanoTime();
    this.gameGraphics.drawString("FPS: " + String.format("%2d", (int) (1000000000.0 / (end - start))),
            this.borderLeft + 50, this.borderTop + 50);
}

public Goal getGoal() {
    return this.goal;
}

public void keyPressed(KeyEvent ke) {
    // Below, the setKey method is used to tell the Player object which key is
    // currently pressed.
    // The Player object must keep track of the pressed key and use it for
    // determining the direction
    // to move.
    if (ke.getKeyCode() == KeyEvent.VK_LEFT)
        this.player.setKey('L', true);
    if (ke.getKeyCode() == KeyEvent.VK_RIGHT)
        this.player.setKey('R', true);
    if (ke.getKeyCode() == KeyEvent.VK_UP)
        this.player.setKey('U', true);
    if (ke.getKeyCode() == KeyEvent.VK_DOWN)
        this.player.setKey('D', true);
    if (ke.getKeyCode() == KeyEvent.VK_ESCAPE)
        this.continueGame = false;
}

@Override
public void keyReleased(KeyEvent ke) {
    // Below, the setKey method is used to tell the Player object which key is
    // currently released.
    // The Player object must keep track of the pressed key and use it for
    // determining the direction
    // to move.
    if (ke.getKeyCode() == KeyEvent.VK_LEFT)
        this.player.setKey('L', false);
    if (ke.getKeyCode() == KeyEvent.VK_RIGHT)
        this.player.setKey('R', false);
    if (ke.getKeyCode() == KeyEvent.VK_UP)
        this.player.setKey('U', false);
    if (ke.getKeyCode() == KeyEvent.VK_DOWN)
        this.player.setKey('D', false);
}

@Override
public void keyTyped(KeyEvent ke) {
}

}

我的目标类别:
import java.awt.Dimension;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

public class Goal {

    private BufferedImage imageRunning;
    private BufferedImage imageOver;
    private BufferedImage imageCurrent;

    public Goal() {
        try {
            this.imageRunning = ImageIO.read(new File("C:\\Users\\HUS\\Desktop\\image-file\\stage-normal.png"));
            this.imageOver = ImageIO.read(new File("C:\\Users\\HUS\\Desktop\\image-file\\stage-gameover.png"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        this.imageCurrent = this.imageRunning;
    }

    public BufferedImage getCurrentImage() {
        return this.imageCurrent;
    }

    public void setGameOverBackground() {
        this.imageCurrent = this.imageOver;
    }



public Goal(int i, int j) {
    // TODO Auto-generated constructor stub
}

public Dimension getCurrentImage1() {
    // TODO Auto-generated method stub
    return null;
}

public int getY() {
    // TODO Auto-generated method stub
    return 0;
}

public int getX() {
    // TODO Auto-generated method stub
    return 0;
}

public void die() {
    // TODO Auto-generated method stub


}

}

最佳答案

检查第93行:

this.goal.getCurrentImage()

您没有初始化currentImage,因此得到了NullPointerException

关于java - 使用代码获取黄色错误/警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50192509/

相关文章:

c++ - 使用返回对象的函数处理错误

amazon-web-services - 在哪里可以找到等效的AWS上详细的Heroku日志?

java - 如何使 IntelliJ 自动完成不在 Javadoc 中插入完整路径

java - Junit @Rule 和 Maven Checkstyle 插件

java - 使用特定语法将Java对象转换为JSON

java - 写在文件中好还是用Java序列化好?

java - 如何用 Java 制作 soap 客户端?

java - 在循环中创建对象的新实例以添加到列表中

c# - WCF CultureInfo 类型化 DataMember 序列化 CommunicationException

swift - 如何在 Swift 中提供带有错误类型的本地化描述?