Java program won't open as a .jar file, yet works just fine in IntelliJ before being exported as a jar file. Running the jar in the cmd gave me the error
exception in thread main java.lang.nullpointerexception:
Cannot invoke "java.net.URL.openstream() because the return value of
"java.lang.Class.getResource(String)" is null
, which as a noob at programming doesn't make a ton of sense to me.
Opening the jar file reveals that the resources the program is trying to access are indeed present within the jar file, and as said previously it runs fine in the IDE. I'm not entirely sure if people will need to see my actual code in order to fix the issue, so let me know if you want to see anything and I'll post it.
link to error message
This is the section of the code that the cmd pointed out in the image above. "FontMainText" is line 103.
public GUI(Game game) throws IOException, UnsupportedAudioFileException {
this.game = game;
//Fonts
try{
fontMainText = Font.createFont(Font.TRUETYPE_FONT, getClass().getResource("KnightsQuest.TTF").openStream()).deriveFont(26f);
fontTitle = Font.createFont(Font.TRUETYPE_FONT, getClass().getResource("KnightsQuest.TTF").openStream()).deriveFont(120f);
fontStats = Font.createFont(Font.TRUETYPE_FONT, getClass().getResource("KnightsQuest.TTF").openStream()).deriveFont(26f);
fontHealth = Font.createFont(Font.TRUETYPE_FONT, getClass().getResource("KnightsQuest.TTF").openStream()).deriveFont(42f);
fontOptions = Font.createFont(Font.TRUETYPE_FONT, getClass().getResource("KnightsQuest.TTF").openStream()).deriveFont(30f);
fontStart = Font.createFont(Font.TRUETYPE_FONT, getClass().getResource("KnightsQuest.TTF").openStream()).deriveFont(40f);
GraphicsEnvironment graphicsEnvironment = GraphicsEnvironment.getLocalGraphicsEnvironment();
graphicsEnvironment.registerFont(fontMainText);
}
catch(IOException | FontFormatException e){
}
}
Any help would be appreciated, thank you!
This is the project structure in the IDE: project structure
This is what the .jar file looks like when opened up in 7-zip:
So the answer ended up being pretty simple. In my IDE, I listed the fonts' file type as ".TTF" and the IDE was able to read this. However, for the .jar file, it needs to be lowercase ".ttf" otherwise it won't work.