1

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 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: enter image description here

  • it seems that in your IDE the resource was found, in the jar, it isn't. Also, try not to hide exceptions as you do in the code you show, at least log them. the way you coded it now, not only will you not know what went wrong, you won't even know something went wrong – Stultuske Jan 06 '22 at 08:01
  • When running in the IDE, the classpath has probably be set up to point to the resource files. Also, the IDE will not run the JAR it will run the classes. – jr593 Jan 06 '22 at 08:10
  • Yea in intelliji I set the res folder as a designated resource folder in the project structure. I am just stuck on getting the jar to find it as well. I added the project structure to my question – Benjamin Keninger Jan 06 '22 at 08:45
  • I posted what it looks like in 7-zip though I'm not entirely sure what I'm looking for. All the resource files are there, but they are not in their own folder together like how I have it in the program on the IDE. – Benjamin Keninger Jan 06 '22 at 09:52
  • Try using `getResourceAsStream` instead `getResource`. See also https://www.jetbrains.com/help/idea/resource-files.html . – Egor Klepikov Jan 06 '22 at 12:47
  • I used getResourceAsStream as you said and some progress was made! The .jar file will run and handle the images and sound correctly but is still messing up the fonts. It will just simply not load the custom font and will default to the standard font. – Benjamin Keninger Jan 06 '22 at 19:19

1 Answers1

0

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.