So I broke Java

Hey Everyone! I’ve been gone for quite a while, reason being I’ve been caught up in classes, got a promotion at work, and in the middle of a move. But mainly because I’ve been designing several game consoles, using the Z80, an STM32, and even a couple of PowerPC 750s. However, that’s beside the point. In a 2D game I’m working on, the map loader for it reads an image file and generates a level out of it. The thing is, when it reads a tile data and generates a sprite sheet, the game crashes with an Index Out of Bounds Exception.

The broken part is that when I used debug mode and step through the code, the game loads the tile sheet with no issues. The with print line statements, in the code where the sprite sheet is loaded in, the array size says it’s 400 or so, but when the game jumps out of that code, it says that same array is 0. This does not happen when I step through the code manually. It also does not happen if I have the game load in the whole sprite sheet for every sprite created, but obviously this is super slow. I fixed the glitch by just having every sprite share the same sheet, which gets created when the game starts. So in short:

Create a sprite sheet using a pre-loaded image: Crash
Create a sprite sheet by loading in the images every single time: No Crash
Create one sprite object, and have new sprites copy it’s sheet data: Crash
Have every sprite share the same sheet: No Crash
Manually step through the code: None of the above methods crash.

From debugging:

-Read tile data
-Jump to sprite generator
-Create array
-Array Size: 400
-Return from sprite generator
-Array size: 0 (stays at 400 if manually stepping)
-Use the sprite data: crashes

What I think is happening is somewhere along the code the Array for the sprite sheet is stored in cache somewhere, but is not visible to the rest of the code. I don’t know, I just found it very weird.

This is telling you exactly where the bug is happening… and even telling you the index most likely.

100% chance you are stepping through the wrong conditions.

Look at the stack trace for the exception (the 99% of useful information an exception). Put printlns in that code that verify your assumptions. Repeat.

I totally guarantee without a doubt that you will learn something about where your bug actually is if you follow this path.

Although this is interesting, too. If you are accessing the data from multiple threads then it 100000% must be protected with thread safe primitives.

If not then there is no such thing as a ‘cache’.

I’ve done that already and fixed the issue. I just find it weird that the exception happens when I let the program run on its own, but it doesn’t happen when I manually step through the code in debug mode.

It’s not the same code context or you have some other thread messing things up.

It can’t really be anything else.

You might be interested in checking my library GitHub - Pesegato/MonkeySheet: Spritesheet library for jMonkeyEngine

1 Like

Looks nice, I might try that one of these days. But I’m using my own game engine I made specifically for 2D games

I read that as ‘spreadsheet library’ and was thoroughly confused for a moment :smile:

I don’t see why… balancing your books is soooo much better in 3D!

1 Like

I broke Java again. So I’m working on a 3D game also, using the JME SDK, and I have to use a library I made for a post processing filter. I imported the jar file like I normally do, but the IDE refused to let me use the classes in that JAR file. Like the SDK didn’t acknowledge the class’s existence. So I had to copy the code from the library into the game’s source code itself. Has anyone else come across this problem?

Sometimes netbeans wont link properly. Backspace off and retype a single character in the line with the error and it will almost always relink properly.

Edit: Assuming you have compile on save set and Code completion.

Forgot to mention, can also try clean and build and Save All. For me, one of these three will get things working again.

I’ve done all three of those, and also closing and reopening the project, restarting netbeans, clearing the cache, was tempted to reinstall it bu I got lazy and just copied the code. I swear I’m the king of breaking things

If the jar is in the dependencies and you can’t find the classes… then the .class files aren’t in the jar file or they aren’t in the proper directories in the .jar file.

…but none of that is anything we can see.

The thing is the JAR i’m trying to use I’ve successfully used in previous projects, it’s just this one in particular where it acts like it ins’t there. But it would probably be helpful if I gave a screenshot

Even if I manually place in the import statement, Netbeans says the package doesn’t exist when it’s CLEARLY RIGHT THERE!!!

Wait a minute… Is it because the class I’m trying to access is a .java and not a .class?

1 Like

Yeah, .java is a source file. .class is a class file.

Like I said:

Edit: also note that the package is “Filters” in that directory structure… which is a super unusual package name so may also be a mistake. (By convention, package names should be lower case.)

I don’t know how I didn’t notice that, I’ve been using the wrong jar file this whole time! And about the package name, I made that “library” a looooong time ago, before I started doing programming classes and learning proper naming conventions. Before then I had a strange habit or always capitalizing my package names. I still do it actually

Well It Makes Things Harder To Read. But I Guess It’s Only Your Own Time That You Are Wasting.

Just Know That The Conventions Aren’t Arbitrary And Generally Exist For Very Good Reasons.

1 Like