gpt4 book ai didi

执行 .getImage 时出现 java.lang.NullPointerException

转载 作者:行者123 更新时间:2023-11-29 09:04:21 25 4
gpt4 key购买 nike

我已经在这个网站和谷歌上搜索了好几天了,我很想知道为什么这不起作用。我正在制作像 FFII 这样的 2D rpg 风格游戏,并且能够根据方向使用不同的图像让角色在屏幕上走动,并且代码工作正常,直到我将 Image[] 放入新的类,使其在以后的编码中更有用。这是完美运行的代码部分:

package alpharpg;

import java.awt.Image;
import javax.swing.ImageIcon;


public class AlphaCharacterSheet extends AlphaConstants{

Image[] avatar;

public AlphaCharacterSheet() {

avatar = new Image[NUMOFAVATARS];
}

public void GetCharacter (int c){

switch (c){
case BARD:

ImageIcon tempii;


tempii = new ImageIcon(this.getClass().getResource(FRANTIKMENU));
avatar[MENUAVATAR] = tempii.getImage();
tempii = new ImageIcon(this.getClass().getResource(FRANTIKBATTLE));
avatar[BATTLEAVATAR] = tempii.getImage();
tempii = new ImageIcon(this.getClass().getResource(FRANTIKFRONT));
avatar[FRONTAVATAR] = tempii.getImage();
tempii = new ImageIcon(this.getClass().getResource(FRANTIKBACK));
avatar[BACKAVATAR] = tempii.getImage();
tempii = new ImageIcon(this.getClass().getResource(FRANTIKLEFT));
avatar[LEFTAVATAR] = tempii.getImage();
tempii = new ImageIcon(this.getClass().getResource(FRANTIKWALKFRONT));
avatar[FRONTWALKAVATAR] = tempii.getImage();
tempii = new ImageIcon(this.getClass().getResource(FRANTIKWALKBACK));
avatar[BACKWALKAVATAR] = tempii.getImage();
tempii = new ImageIcon(this.getClass().getResource(FRANTIKWALKLEFT));
avatar[LEFTWALKAVATAR] = tempii.getImage();


default:
break;
}

}

GetCharacter(BARD) 的调用有效,在 paint() 中我用它绘制

drawImage(party.players[inputcounter].avatar[currentzone.avatarfacing], currentzone.avatarx, currentzone.avatary, null);

然而,尽管当我把它放入时它工作得很好

public Image[] img;

public void AlphaAvatar(){

img = new Image[NUMOFAVATARS];

}

public void SetAvatar (int avatarid){


switch (avatarid){

case BARD:

ImageIcon tempii;
tempii = new ImageIcon(this.getClass().getResource(FRANTIKBATTLE));
img[BATTLEAVATAR] = tempii.getImage();
tempii = new ImageIcon(this.getClass().getResource(FRANTIKFRONT));
img[FRONTAVATAR] = tempii.getImage();
tempii = new ImageIcon(this.getClass().getResource(FRANTIKBACK));
img[BACKAVATAR] = tempii.getImage();
tempii = new ImageIcon(this.getClass().getResource(FRANTIKLEFT));
img[LEFTAVATAR] = tempii.getImage();
tempii = new ImageIcon(this.getClass().getResource(FRANTIKWALKFRONT));
img[FRONTWALKAVATAR] = tempii.getImage();
tempii = new ImageIcon(this.getClass().getResource(FRANTIKWALKBACK));
img[BACKWALKAVATAR] = tempii.getImage();
tempii = new ImageIcon(this.getClass().getResource(FRANTIKWALKLEFT));
img[LEFTWALKAVATAR] = tempii.getImage();

现在在 CharacterSheet 中我有 AlphaAvatar 头像;

CharacterSheet() 我有 avatar = new AlphaAvatar();

然后,当调用 GetCharacter(BARD) 时,我调用了 avatar.SetAvatar(BARD),它在第一个 img[] 处给出了错误= tempii.getImage();

任何关于为什么我可以通过一个类而不是另一个类加载图像的帮助将不胜感激。

最佳答案

替换

public void AlphaAvatar(){

img = new Image[NUMOFAVATARS];

}

public AlphaAvatar(){

img = new Image[NUMOFAVATARS];

}

public void AlphaAvatar() 不是构造函数,它是一个名为 AlphaAvatar 的方法,它不返回任何内容...它解释了为什么 img 没有在其中初始化。

关于执行 .getImage 时出现 java.lang.NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15838243/

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