Mokka Chocolata
Mokka Chocolata
JCHJava Community | Help. Code. Learn.
Created by Mokka Chocolata on 3/27/2024 in #java-help
Reading resource file issue
Hi. I was making a language system for my project and it threw a NullPointerException. Every solution on the internet did not work. For some reason, the code that reads the image on the same resources folder did work. So, heres the code:
private String getLocalizedJson() throws IOException {
return new String(getClass().getClassLoader().getResourceAsStream("/resources/localizedtexts.json").readAllBytes());
}
private String getLocalizedJson() throws IOException {
return new String(getClass().getClassLoader().getResourceAsStream("/resources/localizedtexts.json").readAllBytes());
}
Stack trace:
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "java.io.InputStream.readAllBytes()" because the return value of "java.lang.ClassLoader.getResourceAsStream(String)" is null
at com.mokkachocolata.util.Localization.getLocalizedJson(Localization.java:25)
at com.mokkachocolata.util.Localization.getLocalizedText(Localization.java:50)
at com.mokkachocolata.project.adbgui.MainFrame.init(MainFrame.java:56)
at com.mokkachocolata.project.adbgui.App.main(App.java:45)
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "java.io.InputStream.readAllBytes()" because the return value of "java.lang.ClassLoader.getResourceAsStream(String)" is null
at com.mokkachocolata.util.Localization.getLocalizedJson(Localization.java:25)
at com.mokkachocolata.util.Localization.getLocalizedText(Localization.java:50)
at com.mokkachocolata.project.adbgui.MainFrame.init(MainFrame.java:56)
at com.mokkachocolata.project.adbgui.App.main(App.java:45)
38 replies