Buffered image?

Hi all, I’ve resumed developing my java gaming skills and have used the book “Killer Game Programming in Java”

To teach myself, I’m using the examples in the book and modifying them to create games, but I’ve hit a snag.

When running a build I get the following errors and I wondered is the book out of date and using incorrect syntax, does java even use buffered image in that form anymore? :

Exception in thread "main" java.lang.NullPointerException
	at java.io.Reader.<init>(Reader.java:78)
	at java.io.InputStreamReader.<init>(InputStreamReader.java:72)
	at ImagesLoader.loadImagesFile(ImagesLoader.java:97)
	at ImagesLoader.<init>(ImagesLoader.java:66)
	at JackPanel.<init>(JackPanel.java:124)
	at JumpingJack.<init>(JumpingJack.java:57)
	at JumpingJack.main(JumpingJack.java:98)

When investigated further, this is one of the apparent errors in the coding:

      BufferedReader br = new BufferedReader( new InputStreamReader(in));

I know this is a vague description of the problem, but as I don’t know what the problem is I don’t really know what to provide you :stuck_out_tongue: any help would be appreciated!

Probably the image file is not at the place it is supposed to be.

Image in the build path?

You’d perform io operations with images via ImageIO class:


    BufferedImage img = null;
    try {
    img = ImageIO.read(new File("strawberry.jpg"));
    } catch (IOException e) {
    }

see http://docs.oracle.com/javase/tutorial/2d/images/loadimage.html

Depends on whether the image/stream is accessed/opened as resource to be found in the classpath or as file or as URL.
Take a look into ImagesLoader.

I’m running my game from a JAR file. Here I’m loading images like this:


Image zombieImg = ImageIO.read(getClass().getResource("/img/zombie.gif"));

I then have to place “zombie.gif” in the folder “img” at the root of the JAR file.

ps. I’m using the same book :-).

I could use that method and individually load each image via hard coding, but that seems a little wasteful and could quickly get unmanageable with a game with many assets, this method reads imsInfo.txt, which tells it how big the image is, how many separate images to break it into etc. I think this method is superior and is in line with OOPs and allows easy extension of assets.

I’ve added the asset files into my IDE in the same project and built it, when run it still outputs the same errors.

The images are obtained through the following code:

String imsFNm = IMAGE_DIR + fnm;
    System.out.println("Reading file: " + imsFNm);
    try {
      InputStream in = this.getClass().getResourceAsStream(imsFNm);
      BufferedReader br = new BufferedReader( new InputStreamReader(in));
      // BufferedReader br = new BufferedReader( new FileReader(imsFNm));
      String line;
      char ch;
      while((line = br.readLine()) != null) {
        if (line.length() == 0)  // blank line
          continue;
        if (line.startsWith("//"))   // comment
          continue;
        ch = Character.toLowerCase( line.charAt(0) );
        if (ch == 'o')  // a single image
          getFileNameImage(line);
        else if (ch == 'n')  // a numbered sequence of images
          getNumberedImages(line);
        else if (ch == 's')  // an images strip
          getStripImages(line);
        else if (ch == 'g')  // a group of images
          getGroupImages(line);
        else
          System.out.println("Do not recognize line: " + line);
      }
      br.close();
    } 
    catch (IOException e) 
    { System.out.println("Error reading file: " + imsFNm);
      System.exit(1);
    }
  }  // end of loadImagesFile()

I’ve made sure that both the assets and imsInfo.txt (text file which tells the reader what kind of image file is present and how many separate sprites to break the image into etc.) are within the project and have rebuilt the project before running it, any ideas?

[EDIT]: The line that netbeans seems to be having a problem with is line 5 if that’s any help?

any ideas?

What kind of problem / error do you have? The line [5] should compile just fine, I think.