gpt4 book ai didi

Java "Space Invaders"原因 "NULL exception"

转载 作者:行者123 更新时间:2023-12-01 18:38:04 24 4
gpt4 key购买 nike

我无法解决将外星人图像加载到我的主程序中的问题。

这是Monster的代码:

 package classes;

public class Monster extends Actor {

protected int enemySpeed;

public Monster(Stage stage)
{
super(stage);
setSpriteName("0.png");
}

public void act()
{
x+=enemySpeed;
if (x < 0 || x > Stage.windowWidth)
enemySpeed = -enemySpeed;
}
public int getVx()
{
return enemySpeed;
}
public void setVx(int i)
{
enemySpeed = i;
}
}
<小时/>

这是 Actor 的代码(它是创建图像的主类):

    package classes;

import java.awt.Graphics2D;
import java.awt.image.BufferedImage;

public class Actor {
protected int x,y;
protected int width, height;
protected String spriteName;
protected Stage stage;
protected SpriteCache spriteCache;
private BufferedImage IMG;

public Actor(Stage stage)
{
this.stage = stage;
spriteCache = stage.getSpriteCache();
}

public void paint(Graphics2D g)
{
g.drawImage( spriteCache.getSprite(spriteName), x,y, stage );
}

public int getX()
{
return x;
}
public void setX(int i)
{
x = i;
}
public int getY()
{
return y;
}
public void setY(int i)
{
y = i;
}
public String getSpriteName()
{
return spriteName;
}
public void setSpriteName(String string)
{
spriteName = string;
IMG = spriteCache.getSprite(spriteName);
/*
BufferedImage image = spriteCache.getSprite(spriteName);
height = image.getHeight();
width = image.getWidth();
*/
}
public int getHeight()
{
return height;
}
public int getWidth()
{
return width;
}
public void setHeight(int i)
{
height = i;
}
public void setWidth(int i)
{
width = i;
}
public void act()
{

}
}
<小时/>

最后但并非最不重要的代码是我加载图像的 SpriteCache 代码。

package classes;

import java.awt.image.BufferedImage;
import java.net.URL;
import java.util.HashMap;

import javax.imageio.ImageIO;

public class SpriteCache
{
public HashMap<String, BufferedImage> sprites;

public SpriteCache()
{
sprites = new HashMap<String, BufferedImage>();
}


private BufferedImage loadImage(String sciezka)
{
URL url=null;
try
{
url = getClass().getClassLoader().getResource("0.png");
return ImageIO.read(url);
}
catch (Exception e)
{
System.out.println("Przy otwieraniu " + sciezka +" jako " + url);
System.out.println("Wystapil blad : "+e.getClass().getName()+" "+e.getMessage());
System.exit(0);
return null;
}
}
public BufferedImage getSprite(String sciezka) {
BufferedImage img = (BufferedImage)sprites.get("0.png");
if (img == null)
{
img = loadImage("0.png");
sprites.put(sciezka,img);
}
return img;
}
}
<小时/>

毕竟我得到了错误:

java.lang.NullPointerException
at classes.Actor.setSpriteName(Actor.java:48)
at classes.Monster.<init>(Monster.java:10)
at Main.initWorld(Main.java:116)
at Main.game(Main.java:99)
at Main$1.run(Main.java:50)

我尝试使用一切,但我不知道发生了什么。我在“class”文件夹中获取了所有类,在“img”文件夹中获取了所有图像。

这是主类:

import java.awt.Canvas;


public class Main extends Canvas implements Stage {

private JFrame frame;
//wysokosc szerokosc
private int windowWidth = 800;
private int windowHeight = 600;

//Buffor stretegi - odświeżanie ekranu ( ucieczka od migania )
public BufferStrategy strategy;

private SpriteCache spriteCache;
private ArrayList actors;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Main window = new Main();
window.frame.setVisible(true);
window.game();
} catch (Exception e) {
e.printStackTrace();
}
}
});
}

/**
* Create the application.
*/
public Main() {
initialize();
}

/**
* Initialize the contents of the frame.
*/
private void initialize()
{
//główne okno
frame = new JFrame("Wojna z Alienami by Dawid Raźny!");
frame.setBounds(100, 100, windowWidth, windowHeight);
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e){
System.exit(0);
}
});

//panel w którym będą rysowane obiekty i grafiki
JPanel panel = (JPanel)frame.getContentPane();
setBounds(0,0,windowWidth,windowHeight);
panel.setPreferredSize(new Dimension(windowWidth,windowHeight));
panel.setLayout(null);
panel.add(this);


//potrzebne elementy BufforStrategy
createBufferStrategy(1);
strategy = getBufferStrategy();
requestFocus();

}//konstruktor


public void game() {
initWorld();
while (isVisible())
{
updateWorld();
paintWorld();
try
{
Thread.sleep(Stage.Speed);
}
catch (InterruptedException e) {}
}
}

public void initWorld() {
actors = new ArrayList();
for (int i = 0; i < 10; i++)
{
Monster m = new Monster(this);
m.setX( (int)(Math.random()*Stage.windowWidth) );
m.setY( i*20 );
m.setVx( (int)(Math.random()*3)+1 );
actors.add(m);
}
}
public void paintWorld() {
Graphics2D g = (Graphics2D)strategy.getDrawGraphics();
g.setColor(Color.black);
g.fillRect(0,0,getWidth(),getHeight());
for (int i = 0; i < actors.size(); i++)
{
Actor m = (Actor)actors.get(i);
m.paint(g);
}
g.setColor(Color.white);
strategy.show();
}

public void updateWorld() {
for (int i = 0; i < actors.size(); i++)
{
Actor m = (Actor)actors.get(i);
m.act();
}
}

@Override
public SpriteCache getSpriteCache() {
// TODO Auto-generated method stub
return null;
}

}

舞台课:

    package classes;

import java.awt.image.ImageObserver;

public interface Stage extends ImageObserver
{
public static final int windowWidth = 800;
public static final int windowHeight = 600;
public static final int Speed = 10;
public SpriteCache getSpriteCache();
}

最佳答案

该错误指出,在 Actor.setSpriteName 中的某个位置,您试图取消引用空指针。唯一可疑的行是 IMG = spriteCache.getSprite(spriteName);,这表明 spriteCache 在该行执行时为 null。由于这是使用 spriteCache = stage.getSpriteCache() 行(在 Actor 中)初始化的,因此 getSpriteCache 似乎返回了一个 null 对象。我希望这很容易理解...

关于Java "Space Invaders"原因 "NULL exception",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20981889/

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