私は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/RES/splash.pngとのソースファイルTexture.java(必要な場合)はcitynopolisproject/src/org/citynopolisproject/graphicsに保存されます。
しかし、なぜそれがクラッシュしてNPEをスローするのか分かりません。 アイデアはありますか?
挨拶
[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)も試しました。しかし、上記のコードではstacktraceを少なくしました –
あなたはすでにファイルが存在することを確認したので、 'new FileInputStream(texture)'はどうですか? – Izruo