Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have an image file in my project. The hierarchy looks like this:

Project hierarchy

I'm trying to read Manling.png into Manling.java using this code:

public BufferedImage sprite;

public Manling()
{
    try
    {
    File file = new File("resources/Manling.png");
    sprite = ImageIO.read(file);
    } catch (IOException e) {}

    System.out.println(sprite.toString()); //This line is to test if it works
}

I always get a NullPointerException on the println statement, so I assume the path is wrong. I've tried moving the image to different places in the project and I've tried changing the file path (e.g. 'mine/resources/Manling.png' and '/resources/Manling.png'). Any ideas?

If you want a full compilable example, try this one:

package minesscce;

import javax.swing.*;
import java.awt.*;
import java.awt.image.*;
import java.io.*;
import javax.imageio.*;
import java.net.URL;

public class Mine extends JFrame
{
private BufferedImage sprite;

public static void main(String args[])
{
    Mine mine = new Mine();
}

public Mine()
{
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setVisible(true);
    setSize(800, 600);
    setExtendedState(Frame.MAXIMIZED_BOTH);
    setBackground(Color.WHITE);

    try
    {
        File file = new File("resources/Manling.png");
        sprite = ImageIO.read(file);
    } catch (IOException e) {}

    System.out.println(sprite.toString());
}

public void paint(Graphics g)
{
    g.translate(getInsets().left, getInsets().top);
    Graphics2D g2d = (Graphics2D)g;

    g2d.drawImage(sprite, 0, 0, this);
    Toolkit.getDefaultToolkit().sync();
    g2d.dispose();
}

}

Just set up the project like this, using any image you want:

SSCCE

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
318 views
Welcome To Ask or Share your Answers For Others

1 Answer

Try

ImageIO.read(Mine.class.getResource("../minesscce.resources/Manling.png"));

Here's an example:

  • Hierarchy

enter image description here

  • Result

enter image description here

And here's the code...

public final class ImageResourceDemo {
    private static BufferedImage bi;

    public static void main(String[] args){
        try {
            loadImage();

            SwingUtilities.invokeLater(new Runnable(){
                @Override
                public void run() {
                    createAndShowGUI();             
                }
            });
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void loadImage() throws IOException{
        bi = ImageIO.read(
                ImageResourceDemo.class.getResource("../resource/avatar6.jpeg"));
    }

    private static void createAndShowGUI(){
        final JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setBackground(Color.WHITE);
        frame.add(new JLabel(new ImageIcon(bi)));
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...