-2

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!

Nessbound
  • 3
  • 2

1 Answers1

0

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

bjastski
  • 54
  • 3
  • 1
    Please use the following code : I'm not able gave answer - question is closed public void addImage(String image) throws IOException { File f = new File(image); if(f.exists() && !f.isDirectory()) { // check file directory is exist or not this.character.add(ImageIO.read(new File(image))); } } – Sridhar Karuppusamy May 30 '20 at 04:42