Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (577)
games submitted by our members
Games in WIP (498)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Can you spot the error?  (Read 1018 times)
0 Members and 1 Guest are viewing this topic.
Offline tyeeeee1
« Posted 2013-08-21 05:48:11 »

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:
1  
2  
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:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
<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!
Offline namrog84

JGO Ninja


Medals: 46
Projects: 4


Keep programming!


« Reply #1 - Posted 2013-08-21 06:40:18 »

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

"Experience is what you get when you did not get what you wanted"
Offline tyeeeee1
« Reply #2 - Posted 2013-08-21 07:50:16 »

When using the print statement it reads SWORD as it should and when using the if-statement it also runs perfectly fine.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline namrog84

JGO Ninja


Medals: 46
Projects: 4


Keep programming!


« Reply #3 - Posted 2013-08-21 07:57:03 »

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

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


Are you using java 6 or 7?

"Experience is what you get when you did not get what you wanted"
Offline tyeeeee1
« Reply #4 - Posted 2013-08-21 07:59:29 »

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

Java 7
Offline namrog84

JGO Ninja


Medals: 46
Projects: 4


Keep programming!


« Reply #5 - Posted 2013-08-21 08:06:37 »

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 Sad

"Experience is what you get when you did not get what you wanted"
Offline tyeeeee1
« Reply #6 - Posted 2013-08-21 08:25:31 »

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!
Offline Danny02
« Reply #7 - Posted 2013-08-21 10:02:25 »

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
1  
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

1  
2  
3  
Path dir = Paths.get("xml/items/armor");
for(Path i : Files.newDirectoryStream(dir, "*.xml"))
  loadArmor(i);
Offline tyeeeee1
« Reply #8 - Posted 2013-08-21 17:45:14 »

I've split up weapons and armor and implemented your for-loop but it just gives the error:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
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.
Offline lcass
« Reply #9 - Posted 2013-08-21 18:16:50 »

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.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Geemili

Senior Member


Medals: 9
Projects: 1
Exp: 2 years


No Games Finished


« Reply #10 - Posted 2013-08-21 19:49:55 »

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.
Offline tyeeeee1
« Reply #11 - Posted 2013-08-21 22:08:57 »

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.
Offline Geemili

Senior Member


Medals: 9
Projects: 1
Exp: 2 years


No Games Finished


« Reply #12 - Posted 2013-08-21 22:22:52 »

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.
Offline BurntPizza
« Reply #13 - Posted 2013-08-21 22:27:43 »

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
File.separator
Offline Cero
« Reply #14 - Posted 2013-08-22 00:06:30 »

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

Offline tyeeeee1
« Reply #15 - Posted 2013-08-22 03:25:55 »

Well, that was a bit confusing... So, the way I'm using the slashes wont cause any errors right?
Offline bilznatch

Senior Member


Medals: 7
Projects: 2
Exp: 1 year


I'm bad, I'm bad, I'm really... really bad T_T


« Reply #16 - Posted 2013-08-22 05:48:12 »

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.
Offline Several Kilo-Bytes

Senior Member


Medals: 11



« Reply #17 - Posted 2013-08-22 22:04:41 »

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.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

xsi3rr4x (24 views)
2014-04-15 18:08:23

BurntPizza (19 views)
2014-04-15 03:46:01

UprightPath (33 views)
2014-04-14 17:39:50

UprightPath (17 views)
2014-04-14 17:35:47

Porlus (33 views)
2014-04-14 15:48:38

tom_mai78101 (59 views)
2014-04-10 04:04:31

BurntPizza (117 views)
2014-04-08 23:06:04

tom_mai78101 (217 views)
2014-04-05 13:34:39

trollwarrior1 (184 views)
2014-04-04 12:06:45

CJLetsGame (191 views)
2014-04-01 02:16:10
List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:05:20
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!