String comparison is always false problem in node getChild()

I’ve got a stupid problem. You can all flail me if you want after the resolution of this problem, but for some reason, I can’t figure out what the problem is.



I’m trying to reset the content of a node but, for some reason, using the following code doesn’t return the node.



[java]

if (getRootNode().getChild(“Selector Node”) != null)

if (((Node) getRootNode().getChild(“Selector Node”)).getQuantity() > 0) {

((Node) getRootNode().getChild(“Selector Node”)).detachAllChildren();

getRootNode().detachChild(getRootNode().getChild(“Selector Node”));

}

[/java]



Meaning the first line is always null, even if I know the spelling if right, capitalization is right, but I get nothing.



The following is taken from Node.java and unless I’m mistaken, the marked line should return with my node right? Well it’s not.

[java]

public Spatial getChild(String name) {

if (name == null)

return null;



for (int x = 0, cSize = getQuantity(); x < cSize; x++) {

Spatial child = children.get(x);

if (name.equals(child.getName())) { // <


ALWAYS FALSE
return child;
} else if(child instanceof Node) {
Spatial out = ((Node)child).getChild(name);
if(out != null) {
return out;
}
}
}
return null;
}
[/java]

For kicks I decided to make a new method, but it still doesn't work. For some reason, the comparison is also always false. :/

[java]
public Node getChildNode(String name) {
if (name == null)
return null;

for (int x = 0, cSize = getQuantity(); x < cSize; x++) {
Spatial child = children.get(x);
if (child instanceof Node)
if (name.equals(child.getName())) // <
ALWAYS FALSE
return (Node) child;
}
return null;
}
[/java]

I know I could use other ways to do this, but even so, why is this thing always false!?

*scratches head*

I need a pointer please.

Wanted to add but forgot…



Spatial.java setName uses → this.name <-:

[java]

/**

  • Sets the name of this spatial.

    *
  • @param name
  •        The spatial's new name.<br />
    

*/

public void setName(String name) {

this.name = name;

}

[/java]



But getName() didn’t, so here’s a little diff. Although it didn’t fix the problem above, it’s good that both methods use the same way to do things.



[java]# This patch file was generated by NetBeans IDE

It uses platform neutral UTF-8 encoding and n newlines.

— Base (BASE)

+++ Locally Modified (Based On LOCAL)

@@ -271,7 +271,7 @@

  • @return This spatial’s name.

    */

    public String getName() {
  •    return name;<br />
    
  •    return this.name;<br />
    

}



public LightList getLocalLightList(){

[/java]

madjack said:
But getName() didn't, so here's a little diff. Although it didn't fix the problem above, it's good that both methods use the same way to do things.

[java]
public String getName() {
- return name;
+ return this.name;
}

public LightList getLocalLightList(){
[/java]


Lol :D. "this" refers to the class's attributes instead refers to the method's param. Hense the "setName" method used "this".

madjack said:
Meaning the first line is always null, even if I know the spelling if right, capitalization is right, but I get nothing.


Are you sure "Selector Node" is not wrong? Open your scene with the scene composer and see if "Selector Node" is not wrong at scene explorer window.
1 Like
glaucomardano said:
Are you sure "Selector Node" is not wrong? Open your scene with the scene composer and see if "Selector Node" is not wrong at scene explorer window.


Will someone please beat me with a stick?

"Selector node" and "Selector Node"... I would deserve a beating.

:x: :cry:

@madjack: Can you prove you’re sure by uploading a print screen of your scene graph’s tree ( in scene explorer ) ? We’ll just know if the “getChild(String name)” is not wrong if you to do it.

madjack said:
Will someone please beat me with a stick?

"Selector node" and "Selector Node"... I would deserve a beating.

:x: :cry:


Then your problem was solved, right?

Yes. And I feel damn stupid about it too. I’d like to forget this topic ASAP. :frowning:

I’ll make this topic sticky :evil: