1

So here is a part of my code thats throwing NullPointerException:

public class PuzzleGame extends GraphicsProgram implements KeyListener{
        private ArrayList <PuzzleImage> list = new ArrayList <PuzzleImage>();
        private PuzzleImage _11=null;

    public static void main(String[] args) {
            PuzzleGame game= new PuzzleGame();
            game.setup();  //NullPointerException here
            game.addKeyListener(game);
        }

     private void setup(){
            BufferedImage img11 = null;
            try {
                img11 = ImageIO.read(new File("C://part11.png"));
            } catch (IOException e) {
            }
            PuzzleImage _11=new PuzzleImage(img11,2,2,2,2);  //NullPointerException here
            list.add(_11);
    }
}

And here is class PuzzleImage

public class PuzzleImage extends GImage {
    public PuzzleImage(Image img, double x1, double y1, double realX, double realY) {
        super(img, x1, y1);  //NullPointerException here
        x=x1;
        y=y1;
    }
    private double x;
    private double y;
    private double realX;
    private double realY;
}

So I made sure there is file named part11.png on C, so I am guessing path should be right. Now I honestly have no idea what is wrong with this code, however I am very new to java so it's likely there is just something I don't know or haven't seen. Maybe some of you guys could take a look and see if you can find anything? Thanks.

SOLVED: Turns out that out of like 12 images I am adding this one was only .jpg, and not .png. I guess it's getting late, I'm sorry to bother you guys.

Robin Green
  • 32,079
  • 16
  • 104
  • 187

2 Answers2

1

it is quite likely that img11 in setup() is null, because you have a try catch block surrounding its assignment. If your code gets a IOException, your code will not break but neither will img11 get assigned any value and continue to remain null.

Try printing out a message on an error to verify if you are getting an IO Exception.

So change your code to something like this ...

private void setup(){
            BufferedImage img11 = null;
            try {
                img11 = ImageIO.read(new File("C://part11.png"));
            } catch (IOException e) {
                e.printStackTrace() //ADD THIS LINE
            }
            PuzzleImage _11=new PuzzleImage(img11,2,2,2,2);  //NullPointerException here
            list.add(_11);
    }

If you see the error messages being printed you will know that the issue is very likely with img11.

Plus, i am guessing that you are using the ImageIO.read(FIle) method from this following JDK API http://docs.oracle.com/javase/6/docs/api/javax/imageio/ImageIO.html#read(java.io.File), which clearly states that the error will be thrown if the input is null...

read

public static BufferedImage read(File input) throws IOException

Parameters: input - a File to read from. Returns: a BufferedImage containing the decoded contents of the input, or null.

Throws: IllegalArgumentException - if input is null. IOException - if an error occurs during reading.

vijay
  • 2,646
  • 2
  • 23
  • 37
  • yea that's what i am thinking as well, I mean it can't be anything else right? also am I adding path right? I just wasn't sure about those things. EDIT:yes I have confirmed that is indeed the issue, it is throwing exception, but can u figure out the reason? –  Mar 03 '13 at 08:01
  • no i don't think it is anything else. it would also help if you could post the API spec for `ImageIO.read(File)`. that can help confirm things. – vijay Mar 03 '13 at 08:02
  • 1
    @AndrewThompson +1 for the suggestion. i updated my solution to reflect the same. – vijay Mar 03 '13 at 08:04
  • would u please sort of guide me on how to post api spec, as I said im farily new to java, so I am very noobish –  Mar 03 '13 at 08:08
  • @ViktorMalab no issues ... i added the API reference from JDK 6. i am guessing that it the library you are using. – vijay Mar 03 '13 at 08:14
0

PuzzleImage _11=new PuzzleImage(img11,2,2,2,2);

Just try like this:

_11=new PuzzleImage(img11,2,2,2,2);

Generally, NullPointerException is caused at this type only.

  • Thanks for your suggestion, however it still doesn't work, the issue seems to be that it doesn't set img11 to anything, since the try throws an exception. –  Mar 03 '13 at 08:12