title: Reading an image in Netbeans
body: <p>I have an image file in my project. The hierarchy looks like this:</p>
  
  <p><img src="https://i744.photobucket.com/albums/xx81/Tharwen/Pathfailure.png" alt="Project hierarchy"></p>
  
  <p>I'm trying to read Manling.png into Manling.java using this code:</p>
  
  <pre><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
  }
  </code></pre>
  
  <p>I always get a <code>NullPointerException</code> on the <code>println</code> 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?</p>
  
  <p>If you want a full compilable example, try this one:</p>
  
  <pre><code>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();
  }
  </code></pre>
  
  <p>}</p>
  
  <p>Just set up the project like this, using any image you want:</p>
  
  <p><img src="https://i744.photobucket.com/albums/xx81/Tharwen/SSCCEHierarchy.png" alt="SSCCE"></p>