Jump to content
Sign in to follow this  
Divine-X

Java error with xml

Recommended Posts

Okay, I know what the problem is, I just don't know how to fix it.

The problem is when I create a new file my code would get everything from the player class but not the hp. And when I try to load the game I would get an error since [code]<hp>AMOUNT</hp>[/code] isn't in the xml file.

I feel like and it probably is because the variable is static but if it's not static I wouldn't be able to change the value.

When I create a new game/file, what the game makes:
[code]<player>
<username>Storm</username>
<password>k</password>
<attack>5</attack>
<defence>1</defence>
<magic>1</magic>
<warrior>false</warrior>
<archer>false</archer>
<mage>false</mage>
<rights>1</rights>
</player>[/code]

What it's missing:
[code]<player>
<username>Storm</username>
<password>k</password>
[COLOR="#FF0000"] <hp>100</hp>[/COLOR]
<attack>5</attack>
<defence>1</defence>
<magic>1</magic>
<warrior>false</warrior>
<archer>false</archer>
<mage>false</mage>
<rights>1</rights>
</player>[/code]

Load file code: [code] public static void load(Player p) {
String name = JOptionPane.showInputDialog("Please enter your username");
if(name.length() == 0) {
JOptionPane.showMessageDialog(null, "Enter a name longer than 0 characters!");
load(p);
}
String pass = JOptionPane.showInputDialog("Please enter your password");
if(pass.length() == 0) {
JOptionPane.showMessageDialog(null, "Enter a password longer than 0 characters!");
load(p);
} else {
try {
XML playerFile =
new XML("data/games/"+name+".xml");
playerFile.setByElement("player");
for(int i = 0; i < playerFile.getElements().size(); i++) {
Map<String, String> element = playerFile.getElements().get(i);
String username =
playerFile.getStringByTag(element, "username");
p.setUsername(username);
String password =
playerFile.getStringByTag(element, "password");
p.setPassword(password);
int hp =
playerFile.getIntByTag(element, "hp");
p.setHp(hp);
int attack =
playerFile.getIntByTag(element, "attack");
p.setAttack(attack);
int defence =
playerFile.getIntByTag(element, "defence");
p.setDefence(defence);
int magic =
playerFile.getIntByTag(element, "magic");
p.setMagic(magic);
boolean warrior =
Boolean.parseBoolean(playerFile.getStringByTag(element, "warrior"));
p.setWarrior(warrior);
boolean archer =
Boolean.parseBoolean(playerFile.getStringByTag(element, "archer"));
p.setArcher(archer);
boolean mage =
Boolean.parseBoolean(playerFile.getStringByTag(element, "mage"));
p.setMage(mage);
int rights =
playerFile.getIntByTag(element, "rights");
p.setRights(rights);
}
} catch(Exception e) {
JOptionPane.showMessageDialog(null, "Error with loading!", "Alert", JOptionPane.INFORMATION_MESSAGE);
e.printStackTrace();
}
}
}[/code]

XML stuff:
[code]public class XML {

private List<Map<String, String>> elements = new ArrayList<Map<String, String>>();
private BufferedReader reader;

public XML(final String file) throws IOException {
reader = new BufferedReader(new FileReader(file));
}

public void setByElement(String name) throws IOException {
String line;
String endName = "</" + name +">";
name = "<" + name + ">";
while ((line = reader.readLine()) != null) {
if (line.contains(name)) {
Map<String, String> element = new HashMap<String, String>();
while (!(line = reader.readLine()).contains(endName)) {
element.put(line.substring(line.indexOf("<") + 1, line.indexOf(">")), line.substring(line.indexOf(">") + 1, line.lastIndexOf("<")));
}
elements.add(element);
}
}
reader.close();
}

public List<Map<String, String>> getElements() {
return elements;
}

public String getStringByTag(Map<String, String> element, String name) {
return element.get(name);
}

public long getLongByTag(Map<String, String> element, String name) {
return Long.parseLong(element.get(name));
}

public int getIntByTag(Map<String, String> element, String name) {
return Integer.parseInt(element.get(name));
}

public short getShortByTag(Map<String, String> element, String name) {
return Short.parseShort(element.get(name));
}

public byte getByteByTag(Map<String, String> element, String name) {
return Byte.parseByte(element.get(name));
}

}[/code]

Player:
[code]
package text.game.player;

public class Player {

String username, password;
[COLOR="#FF0000"] private static int hp = 100;[/COLOR]
private int attack = 5;
private int defence = 1;
private int magic = 1;
private boolean warrior = false;
private boolean archer = false;
private boolean mage = false;
private int rights = 0;

private Inventory inventory;

/**
* @return the username
*/
public String getUsername() {
return username;
}

/**
* @param username the username to set
*/
public void setUsername(String username) {
this.username = username;
}

/**
* @return the password
*/
public String getPassword() {
return password;
}

/**
* @param password the password to set
*/
public void setPassword(String password) {
this.password = password;
}

[COLOR="#FF0000"] /**
* @return the hp
*/
public int getHp() {
return hp;
}[/COLOR]

[COLOR="#FF0000"] /**
* @param newHp the hp to set
*/
public void setHp(int newHp) {
Player.hp = newHp;
}[/COLOR]

/**
* @return the rights
*/
public int getRights() {
return rights;
}

/**
* @param rights the rights to set
*/
public void setRights(int rights) {
this.rights = rights;
}

/**
* @return the warrior
*/
public boolean isWarrior() {
return warrior;
}

/**
* @param warrior the warrior to set
*/
public void setWarrior(boolean warrior) {
this.warrior = warrior;
}

/**
* @return the archer
*/
public boolean isArcher() {
return archer;
}

/**
* @param archer the archer to set
*/
public void setArcher(boolean archer) {
this.archer = archer;
}

/**
* @return the mage
*/
public boolean isMage() {
return mage;
}

/**
* @param mage the mage to set
*/
public void setMage(boolean mage) {
this.mage = mage;
}

/**
* @return the attack
*/
public int getAttack() {
return attack;
}

/**
* @param attack the attack to set
*/
public void setAttack(int attack) {
this.attack = attack;
}

/**
* @return the defence
*/
public int getDefence() {
return defence;
}

/**
* @param defence the defence to set
*/
public void setDefence(int defence) {
this.defence = defence;
}

/**
* @return the magic
*/
public int getMagic() {
return magic;
}

/**
* @param magic the magic to set
*/
public void setMagic(int magic) {
this.magic = magic;
}

public Inventory getInventory() {
return inventory;
}

public void setInventory(Inventory inventory) {
this.inventory = inventory;
}

}
[/code]
Error:
[code]Item info loaded: 2
java.lang.NumberFormatException: null
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at text.game.managers.XML.getIntByTag(XML.java:49)
at text.game.managers.FileManager.load(FileManager.java:92)
at text.gui.GUI$4.actionPerformed(GUI.java:87)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.AbstractButton.doClick(Unknown Source)
at javax.swing.plaf.basic.BasicMenuItemUI.doClick(Unknown Source)
at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$400(Unknown Source)
at java.awt.EventQueue$2.run(Unknown Source)
at java.awt.EventQueue$2.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)[/code]

Share this post


Link to post
Share on other sites
Well where do you create the file? From a quick look over I don't see where you create the file.

Also your set hp you use Player.hp while everywhere else you use this.hp any reason why?

Sent from my XT1034 using Tapatalk

Share this post


Link to post
Share on other sites
[quote name='Friss']Well where do you create the file? From a quick look over I don't see where you create the file.

Also your set hp you use Player.hp while everywhere else you use this.hp any reason why?

Sent from my XT1034 using Tapatalk[/QUOTE]

[code] public static void create(Player p) {
try {
String username = JOptionPane.showInputDialog("Please enter a username");
String password = JOptionPane.showInputDialog("Please enter a password");
if(password.length() == 0) {
JOptionPane.showMessageDialog(null, "Enter a password greater than 0!");
create(p);
} else {
File f = new File("data/games/"+username+".xml");
if(f.exists()) {
JOptionPane.showMessageDialog(null, "This file already exists!", "Alert", JOptionPane.ERROR_MESSAGE);
create(p);
} else {
p.setUsername(username);
p.setPassword(password);
save(p);
}
}
} catch(Exception e) {
JOptionPane.showMessageDialog(null, "There is no such directory.", "Alert", JOptionPane.INFORMATION_MESSAGE);
}
}[/code]

[code] public static void save(Player p) {
try {
XStream xst = FileManager.getXStream();
xst.toXML(p, new FileOutputStream(
"data/games/"+p.getUsername()+".xml"));
} catch(Exception e) {
System.out.println("Error with saving player!");
}
}[/code]
And I use "Player.hp" because eclipse said it should be accessed in a static way. Edited by Divine-X

Share this post


Link to post
Share on other sites
Why is 'hp' static? You're using setters, so I'm assuming you have getters as well.

This line:
[CODE]return Integer.parseInt(element.get(name));[/CODE]

parseInt is meant for strings only, which the value could be an object in this case.

Should be:
[CODE]
return Integer.parseInt(element.get(name).toString());
[/CODE]

Only thing I can see on a quick glance. Edited by Emily

Share this post


Link to post
Share on other sites
[quote name='Emily']Why is 'hp' static? You're using setters, so I'm assuming you have getters as well.

This line:
[IMG]return Integer.parseInt(element.get(name));[/IMG]

parseInt is meant for strings only, which the value could be an object in this case.

Should be:
[CODE]
return Integer.parseInt(element.get(name).toString());
[/CODE]

Only thing I can see on a quick glance.[/QUOTE]

If I didn't make it static I wouldn't be able to change the players HP, it'll only change the classes HP value temporarily.

Share this post


Link to post
Share on other sites
[quote name='Divine-X']If I didn't make it static I wouldn't be able to change the players HP, it'll only change the classes HP value temporarily.[/QUOTE]

Is this single player or multiplayer? And the reason you can only use static, is you're probably not calling the loading with the login. The references need to carry. Also, regardless if it's single/multi, you either need to add the player's index/reference to a map or list (multi) or create a global reference that you're always pulling from.

Share this post


Link to post
Share on other sites
You're abusing/misunderstanding the point of the static keyword. When a variable is static, it is the same value for all instances of that class, rather than each instance having its own separate value such as for non-static variables. This mean every instance of Player will have the same hp, which is almost assuredly not intended.

You say that you make it static so that you can change it, and I don't understand why this is. You have setters/getters for all your other instance variables, so why is hp any different?

Are you using serialization (Serializable interface, read/writeObject)? The fact that it is static is also probably why it isn't saved in the XML. It doesn't make sense to save static variables, so they aren't. Edited by Trey

Share this post


Link to post
Share on other sites
[quote name='Trey']You're abusing/misunderstanding the point of the static keyword. When a variable is static, it is the same value for all instances of that class, rather than each instance having its own separate value such as for non-static variables. This mean every instance of Player will have the same hp, which is almost assuredly not intended.

You say that you make it static so that you can change it, and I don't understand why this is. You have setters/getters for all your other instance variables, so why is hp any different?

Are you using serialization (Serializable interface, read/writeObject)? The fact that it is static is also probably why it isn't saved in the XML. It doesn't make sense to save static variables, so they aren't.[/QUOTE]

If I have the regular getter/setter setup and I do some calculations for the damage done to the player, and I try to save the game, the player file will stay at[code]<hp>100</hp>[/code] when in the game itself the player is at for example 70hp.

Share this post


Link to post
Share on other sites
[quote name='Divine-X']If I have the regular getter/setter setup and I do some calculations for the damage done to the player, and I try to save the game, the player file will stay at[code]<hp>100</hp>[/code] when in the game itself the player is at for example 70hp.[/QUOTE]

Is it renewing the save (other information is updating correctly?). It really sounds like you aren't pulling the instance of the player class correctly.

Share this post


Link to post
Share on other sites
[quote name='Divine-X']If I have the regular getter/setter setup and I do some calculations for the damage done to the player, and I try to save the game, the player file will stay at[code]<hp>100</hp>[/code] when in the game itself the player is at for example 70hp.[/QUOTE]

Sounds like you either aren't saving successfully or are somehow not loading correctly. Keep in mind that the changes to the instance aren't magically felt by the saved copy, though this goes without saying. Make sure you really are saving the player; check file timestamp and such, and also check the value of hp at the time that you save.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×