Java에서 LWJGL 3 용 텍스처 클래스를 빌드 해 봅니다.getSystemResource (..)를 사용하는 NullPointerException
내 loadTexture 기능은 다음과 같습니다 :
public static Texture loadTexture(String filename) {
int id = -1;
try {
File texture = new File(filename);
if (!texture.exists()) {
System.err.println("File '" + filename + "' does not exist.");
return null;
}
// crash in following line
InputStream stream = ClassLoader.getSystemResource(filename).openStream();
PNGDecoder decoder = new PNGDecoder(stream);
// Some code between here
return new Texture(id, new Vector2i(decoder.getWidth(), decoder.getHeight()));
} catch (IOException e) {
e.printStackTrace();
return new Texture(id, new Vector2i());
}
}
을 스택 트레이스는 다음입니다 :
Exception in thread "main" java.lang.NullPointerException
at org.citynopolisproject.graphics.Texture.loadTexture(Texture.java:49)
at org.citynopolisproject.Game.<init>(Game.java:30)
at org.citynopolisproject.Game.<init>(Game.java:33)
at org.citynopolisproject.Game.main(Game.java:188)
파일의 위치는 다음과 같습니다 citynopolisproject/고해상도/splash.png과의 소스 파일 Texture.java (필요한 경우)는 citynopolisproject/src/org/citynopolisproject/graphics에 저장됩니다.
하지만 왜 충돌이 발생하는지 알 수 없으며 NPE를 throw합니다. 아이디어가 있으십니까?
인사말
[NullPointerException이란 무엇이며 어떻게 수정합니까?] (https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix -it) – jrook
분명히 아닙니다. 나는 그들을 어떻게 다룰 지 안다. 그러나 ClassLoader.getSystemResource ("여기 기존 파일")에 오류가 있어야하며, Texture.class.getResourceAsStream() 및 Texture.class.getClassLoader(). getResourceAsStream (filename)도 시도했습니다. 그러나 위의 코드는 less stacktrace를주었습니다. –
'FileInputStream (texture)'을 이미 파일이 존재했는지 확인 했으니 까? – Izruo