简体   繁体   中英

Getting NullPointerException when making GImage

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM