[SOLVED] HashMap doesn't work :(

Discussion in 'Plugin Development' started by McDjuady, Oct 22, 2011.

Thread Status:
Not open for further replies.
  1. Offline

    McDjuady

    Code:
    public class JDOMDatabaseGroup implements DatabaseGroup {
    [...]
        private HashMap<String, JDOMDBNode> nodes = new HashMap<String, JDOMDBNode>();
    [...]
        public JDOMDatabaseGroup(Document d, JDOMDBNode node) {
    [...]
                    nodes.put(e.getName(), node);
    [...]
        public int getInt(String name) {
            if (name == null || name.isEmpty()) {
                return 0;
            }
            JDOMDBNode node = nodes.get(name);
            if (node == null || !(node.getType() == JDOMDBNode.Type.NODE_INT)) {
                return 0;
            }
            return (Integer) node.getValue();
        }
    The Problem is, my code always ends up in the
    Code:
    if (node == null || !(node.getType() == JDOMDBNode.Type.NODE_INT))
    
    clause because of the HashMap always returning the same node. (I checked that via node.toString())

    So what am i doing wrong?
     
  2. Offline

    nisovin

    Obviously the nodes you're putting in aren't NODE_INT. The HashMap isn't failing, it's doing exactly what you tell it to do, so you must be doing something wrong. It's impossible to say what's wrong with the code snippet you've provided.
     
  3. Offline

    McDjuady

    Code:
    public class JDOMDatabaseGroup implements DatabaseGroup {
    
        private Document doc;
        private JDOMDBNode groupNode;
        private String groupName;
        private HashMap<String, JDOMDBNode> nodes = new HashMap<String, JDOMDBNode>();
        private HashMap<String, DatabaseGroup> groups = new HashMap<String, DatabaseGroup>();
    
        public JDOMDatabaseGroup(Document d, JDOMDBNode node) {
            if (d == null || node == null || !node.isValid() || !(node.getType() == JDOMDBNode.Type.NODE_GROUP || node.getType() == JDOMDBNode.Type.NODE_ROOT)) {
                return;
            }
            doc = d;
            groupNode = node;
            Element elem = node.getElement();
            groupName = node.getElement().getName();
            @SuppressWarnings("unchecked")
            List<Element> childs = elem.getChildren();
            for (Element e : childs) {
                JDOMDBNode childNode = new JDOMDBNode(doc, e);
                if (!childNode.isValid()) {
                    continue;
                } else if (childNode.getType() == JDOMDBNode.Type.NODE_ROOT) {
                    continue;
                } else if (childNode.getType() == JDOMDBNode.Type.NODE_GROUP) {
                    SpoutRPG.getLogger().info(e.getName() + " added as a Subgroup");
                    groups.put(e.getName(), new JDOMDatabaseGroup(d, childNode));
                    nodes.put(e.getName(), node);
                } else {
                    SpoutRPG.getLogger().info(e.getName() + " added as a Node");
                    nodes.put(e.getName(), node);
                }
            }
        }
    
        public JDOMDatabaseGroup(Document d, JDOMDBNode parent, String s) {
            if (d == null || parent == null || !parent.isValid() || s == null || s.isEmpty()) {
                return;
            }
            doc = d;
            groupName = s;
            groupNode = new JDOMDBNode(doc, groupName, parent.getElement(), JDOMDBNode.Type.NODE_GROUP);
        }
    
        public int getInt(String name) {
            if (name == null || name.isEmpty()) {
                return 0;
            }
            JDOMDBNode node = nodes.get(name);
            if (node == null || !(node.getType() == JDOMDBNode.Type.NODE_INT)) {
                SpoutRPG.getLogger().info("Node was " + (node == null ? "null" : node.toString() + " with type " + (node.getType() == null ? "null" : node.getType())));
                return 0;
            }
            return (Integer) node.getValue();
        }
    }
    So that are the only parts, that are important for this.
    The problem is, all nodes from the XMLFile (from which im loading the data) are loaded correctly.
    I double checked that via logging.

    But when i try getInt(), i always get in the same node (which i first checked via the Object.toString(), to get te class@memlocation format and later with an own toString() method)

    So the main proble is, nodes.get(String) always returning the same node :/
     
  4. Offline

    nisovin

    That's because you're putting the same node in every time:

    nodes.put(e.getName(), node);

    Your "node" variable never changes.
     
  5. Offline

    McDjuady

    oh... Silly me >.> Thanks for the help
     
Thread Status:
Not open for further replies.

Share This Page