遇到了 java.lang.NullPointerException。

huangapple 未分类评论49阅读模式
英文:

Running into java.lang.NullPointerException Here

问题

我对编程还比较新,至少在我目前尝试的层次上是这样。我一直在为一个2D格斗游戏引擎创建一个角色类,该类将持有一个BufferedImage数组列表,其中包含角色动画的帧。程序中的所有内容都可以正常编译和运行,但在以下代码中,我一直遇到NullPointerException错误:

public void addImage(String image) throws IOException
{
  this.character.add(ImageIO.read(new File(image)));
}

我不太确定如何修复这个问题,因为我对Java的一般了解水平有限(我只上过一门高中课程,而且是由一位不怎么称职的老师教授),无法正确理解各种网站上关于如何解决这个问题的描述。是否有人可以简洁明了地描述如何解决这个问题?谢谢!

英文:

I am quite new to programming, at least on the level that I am currently attempting. I've been creating a character class for use in a 2D fighting game engine that will hold a BufferedImage arraylist of frames of character animation. Everything in the program compiles and runs fine, except that I keep running into a NullPointerException in the following code:

    public void addImage(String image) throws IOException
  {
    this.character.add(ImageIO.read(new File(image)));
  }

I am not sure exactly how to fix this, as my low level of general Java knowledge (I only have 1 2 semester high school class by a shoddy teacher under my belt) prevents me from correctly understanding the descriptions on how to fix the problem on various websites. Could somebody describe how to solve this in a clean and concise way? Thank you!

答案1

得分: 0

空指针异常是在您尝试传递一个具有空值的对象引用时抛出的。我会查看确保您传递的文件实际上是否被读取。尝试先进行分解,然后在功能正常后进行更多的 consilidating(整合)。也许将文件存储在一个变量中,然后再添加进去。如果有效果,就继续使用;如果仍然出现空值异常,就检查您的新文件。

英文:

NullPointerException is thrown when you try to pass an object ref that has a null value.. I would look to make sure the file you are passing it actually getting read.. try breaking it down then consolidating more after it is functional.. maybe store your file in a variable firs then add that.. if it works then go with it, if it doesn't and you still get the null, look to your new File

huangapple
  • 本文由 发表于 2020年5月30日 12:30:18
  • 转载请务必保留本文链接:https://java.coder-hub.com/62097849.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定