Can you spot the error?

Hey,

I’m just about done a few-hour-long rewrite of a big part of my program/game and I can’t seem to figure out the cause of a, probably obvious, error that’s showing up. It’s probably just because I’m up pretty late but I can’t seem to figure out what’s causing the error I’m getting.

The error is:

DBLoader(Inventory Class: The specified item type doesn't exist. There is no error handleing for this.
/XML/Items/Weapons/Sword.xml

The error seems pretty straightforward since I wrote it up myself for myself but it’s making no sense from what my sleepy-eyes can see. The ItemType Sword does exist and the xml file lists the proper ItemType in all caps just as it should; so I’m at a loss for what the problem is unless my eyes have been messing with me and there’s a typo in one of the SWORD strings…

The inside of the Sword.xml file is:

<body>
	<item>
		<id>0</id>
		<name>Test Sword</name>
		<rarity>COMMON</rarity>
		<reqLevel>1</reqLevel>
		<bonusStrength>1</bonusStrength>
		<bonusDexterity>0</bonusDexterity>
		<bonusConstitution>0</bonusConstitution>
		<bonusIntelligence>0</bonusIntelligence>
		<bonusWisdom>0</bonusWisdom>
		<bonusCharisma>0</bonusCharisma>
		<itemType>SWORD</itemType>
		<damageMin>1</damageMin>
		<damageMax>10</damageMax>
		<bonusDamage>0</bonusDamage>
	</item>
	<item>
		<id>1</id>
		<name>Wooden Stick</name>
		<rarity>COMMON</rarity>
		<reqLevel>1</reqLevel>
		<bonusStrength>1</bonusStrength>
		<bonusDexterity>0</bonusDexterity>
		<bonusConstitution>0</bonusConstitution>
		<bonusIntelligence>0</bonusIntelligence>
		<bonusWisdom>0</bonusWisdom>
		<bonusCharisma>0</bonusCharisma>
		<itemType>SWORD</itemType>
		<damageMin>1</damageMin>
		<damageMax>7</damageMax>
		<bonusDamage>0</bonusDamage>
	</item>
</body>

Here is the DBLoader class:
https://github.com/Valkryst/Project_02/blob/master/src/main/java/valkryst/item/DBLoader.java

Here is the ItemType class:
https://github.com/Valkryst/Project_02/blob/master/src/main/java/valkryst/type/ItemType.java

What is supposed to happen is that the DBLoader class is called when the program starts. It then begins to loop through every single xml file to load all of the items into the game. That’s pretty much it.

Thanks for any replies!

Tell me if that is null or something else?

System.out.println(XMLParse.getStringValue(“itemType”, e));
do this before the switch statement on line 94?

edit:

You are returning a string
but you are using a switch statement

This might not work, because remember that == shouldn’t be used with strings, when .equals( otherString) should be used

so in this case, it might be “SWORD” == “SWORD” isn’t always true with strings…

Tahts why I wanted you to to test the printout of getStringValue, and then try it with if(getStringValue(“itemType”,).equals(“SWORD”) { //beep try this? at least for 1 test case

When using the print statement it reads SWORD as it should and when using the if-statement it also runs perfectly fine.

So if it works with that if statement, you should restructure your conditional to be something like the following?

String tempString = getStringValue("itemType",e);
if(tempString.equals("SWORD"){ itemType = ItemType.BACK; break; }
else if(tempString.equals......
....

Are you using java 6 or 7?

That’s exactly what the switch statement does but without having to go through the whole if-else chain.

Java 7

I thought you said before that the error exists in the original code

“when using the if-statement it also runs perfectly fine.”
Or did you mean by that, the if-statement throws the same error?

edit: Then I am out of ideas :frowning:

As I was writing up some code to show you what I did, I somehow fixed the problem without even knowing how I did it…

~Off to fix the other errors! After some sleep…

Thanks for the help!

when you are using enums for the types and rarity(as it seems) you don’t need switch case statements.

I don’t know the exact methodname atm, but there should be a method like

ItemType.named("Maze")

which gives you the enum object or throws an exception I think.

Also get rid of this distinction between armor and weapons after the 11th path. Have one collection of paths for armor and one for weapons and load them differently(two methods).

And as a third trip, because you already have armor and weapons folders why not just read every xml file in these folders instead of having to specify everyfile in a static array.

when using Java7 you can do this like this


Path dir = Paths.get("xml/items/armor");
for(Path i : Files.newDirectoryStream(dir, "*.xml"))
  loadArmor(i);

I’ve split up weapons and armor and implemented your for-loop but it just gives the error:

java.nio.file.NoSuchFileException: \XML\Items\Armor
	at sun.nio.fs.WindowsException.translateToIOException(WindowsException.java:79)
	at sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:97)
	at sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:102)
	at sun.nio.fs.WindowsDirectoryStream.<init>(WindowsDirectoryStream.java:86)
	at sun.nio.fs.WindowsFileSystemProvider.newDirectoryStream(WindowsFileSystemProvider.java:526)
	at java.nio.file.Files.newDirectoryStream(Files.java:479)
	at valkryst.item.DBLoader.loadArmor(DBLoader.java:29)
	at valkryst.item.DBLoader.load(DBLoader.java:22)
	at valkryst.core.Game.run(Game.java:43)
	at java.lang.Thread.run(Thread.java:724)
java.nio.file.NoSuchFileException: \XML\Items\Weapons
	at sun.nio.fs.WindowsException.translateToIOException(WindowsException.java:79)
	at sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:97)
	at sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:102)
	at sun.nio.fs.WindowsDirectoryStream.<init>(WindowsDirectoryStream.java:86)
	at sun.nio.fs.WindowsFileSystemProvider.newDirectoryStream(WindowsFileSystemProvider.java:526)
	at java.nio.file.Files.newDirectoryStream(Files.java:479)
	at valkryst.item.DBLoader.loadWeapons(DBLoader.java:100)
	at valkryst.item.DBLoader.load(DBLoader.java:23)
	at valkryst.core.Game.run(Game.java:43)
	at java.lang.Thread.run(Thread.java:724)

So I guess it can’t work the way it is. Reverting to original code.

I’ll take a look at the enum method.

I would say debug see whats going on with the items and whats different ive personally found the switch statement horrific to use mainly because of its un-predictability, it never works for me.

I’m going to guess that the problem is because you are using backslashes, for one, instead of forward slashes.
Also, unless the files are located under C:/XML/Items/Armor, you should use “XML/Items/Armor”.
Of course, this is just a guess.

From what I can remember, Java requires you to use back-slashes instead of forward-slashes. The files are compiled and placed with in the jar so the file paths should be correct.

Actually, from looking it up, it looks like it is system dependent? Tell me if I’m wrong. So you are right, the file separator isn’t the problem.

File separators in Java are forward slashes. ( / ) They are automatically changed to the appropriate character at runtime, depending on the system. You can query this with [icode]File.separator[/icode]

Forward slashes work on windows too. Backslashes ONLY work on windows

Well, that was a bit confusing… So, the way I’m using the slashes wont cause any errors right?

backslashes are used for escape sequence, so if you use them somewhere that expects a string (as in a file path) it’ll likely think you’re trying to use an escape character such as a new line \n or quotes ".

I’d swap them to forward slashes, and remove the first, just in case.

Paths starting with / are relative to the root ancestor of your file system. Paths starting with ./ are relative to the working directory. …/ Designates the parent of a directory. Paths that do not start with / ./ or …/ can refer to multiple locations: the operating system searches the working directory then other globally defined directories, typically containing references to program files. (This is why you can type “java” in a terminal instead of the full path to the java binary.) If you want to be explicit that a file is in the working directory, then paths should start with ./ The above is true for Unix style filesystems, Windows filesystems, and network protocols like FTP and HTTP. Only Windows uses back slashes and Windows also treats forward slashes the same way.

If you assume the working directory contains all your data files, you should start file paths with ./
Alternatively you should use the same relative path notation and apply that relative path to the path to your program’s data.