Fixing An Exceptional Bug

No matter how much you test a mod you will always miss something. This time it was a crash that I couldn’t reproduce, nor could I find the cause for. I had to sit on it for a while, but thankfully someone gave me the vital clue I needed to find and fix the bug once and for all.

Cannot Reproduce


After releasing the latest version of the mod I got a comment on my CurseForge page.

This LOOKS incredible but for some reason it makes the game crash upon loading a world? i can send the crash report but idk where. thank you though this mod looks insane

RagdollWraith

So there is a crash bug in the latest version that somehow managed to slip through. I went back to the project and started testing. I created a single player world. No issues. I started a server and connected a couple of clients. No issues. I tried downloading the mod through CurseForge and running it on a fresh mod. No issues.

This was the worst kind of bug. It’s a crash bug that I cannot reproduce. And with no more information on how it happens I was at a loss. All I could do was wait and hope that crash report came through.

More Info Needed


A while later I got a crash report submitted to my GitHub project by IMS212. I don’t know if this was the same user from CurseForge, but it gave me the information I needed to fix it. Within it was the essential clue I needed to fix the bug:

com.sun.jdi.InvalidTypeException, used in ButterflyData, does not exist on a JRE, only a JDK. If a butterfly attempts to be loaded while using a JDK…

So what is actually the problem here? Let’s back up.

Some time in November I implemented some improved error checking for the Butterfly Data. The idea behind it was so that you could see in the logs if there was any invalid data, and point you to the incorrect data, allowing for easier fixes.

To handle this I decided to use exception handling and I had to pick the right exception to use. I went with InvalidTypeException

        throw new InvalidTypeException(search);

What I didn’t notice is the library that it comes from:

import com.sun.jdi.InvalidTypeException;

JDI in this instance stands for Java Debug Interface, which is “a high level Java API providing information useful for debuggers and similar systems needing access to the running state of a (usually remote) virtual machine.” We don’t need to get into details, but the important thing here is that this library only exists in the JDK (Java Development Kit), and not the JRE (Java Runtime Environment).

What does this mean? Well, the JDK is what Java developers will have installed on their machines. It contains a whole bunch of extra libraries and features that are only useful to people who are programming with Java. When you run the game through an IDE like IntelliJ, you are using the JDK to run Minecraft.

But the average user doesn’t do any Java programming, they just want to run applications like Minecraft. So instead of filling their hard drives with all the debug tools and libraries, they use the JRE instead which only contains everything they need to run an application, and none of the tools and libraries used only for development.

So when an inexperienced Java developer (i.e. yours truly) decides to use code from a library only in the JDK, they might think everything is fine when they test the game. But when someone else tries to run the mod on their machine with the JRE it crashes, and confuses that inexperienced developer who can’t reproduce the crash.

Fixed


So the fix for this bug was very simple once I knew what the problem was. I still wanted to throw an exception, but I needed to use either a) an exception that is included in the JRE, or b) a custom exception of my own. I took the path of least resistance and use IllegalArgumentException instead. This exception is in the java.lang library, which are the fundamental classes for Java and included in the JRE.

The lesson here is that you need to pay attention to what libraries you are including in your project as you develop. This isn’t actually the first time I’ve had this kind of bug, so it’s something I need to be more vigilant about in the future. It’s easy to automatically include libraries as you add code thanks to modern day coding tools, but this also makes it more likely a bug like this can slip through.

Another lesson is that more information always helps. Thanks to the crash log that IMS212 submitted I was finally able to find the cause of the crash and push out a fix. As always, I genuinely appreciate people who report bugs and make suggestions, and I’ll always do what I can to fix and improve the mod based on these submissions.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.