이 게임의 배경 그림을로드하려고하는데 파일이 클래스 파일과 동일한 폴더에 있고 폴더가 하나뿐입니다. 파일을 참조하는 여러 가지 방법을 모색했지만 모두 실패했습니다. 이 던진 오류입니다 :NullPointerException 파일을로드하는 중 오류가 발생했습니다.
Exception in thread "main" java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(Unknown Source)
at ThrustR.<init>(ThrustR.java:28)
at ThrustR.main(ThrustR.java:35)
그리고 여기에 코드입니다 :
public class ThrustR extends JFrame
{
public String path;
public File file;
public BufferedImage image;
public void setValues() throws IOException
{
path = "CityRed.jpg";
file = new File(path);
image = ImageIO.read(file);
}
public ThrustR(String title)
{
super(title);
JLabel back = new JLabel(new ImageIcon(image));
}
public static void main(String[] args)
{
// Main Window
ThrustR frame = new ThrustR("ThrustR");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(720,480);
frame.setVisible(true);
}
}
경로 = "CityRed.jpg"; HDD, 자바 패키지, BufferedImage에 유효한 경로를 리턴하지 못한다면 NullPointerException이 발생한다. 자바 패키지에 대한 오라클 튜토리얼 – mKorbel