• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Java. Refering to 'self'

Carlis

Senior member
Hi Im building a program with a hierarchy of nodes. Every node produces a set of new nodes and keeps track of them. These create new ones in turn etc.

Now, it is necessary that every node knows who its parent node is. Obviously, every node should have a field 'parent' which should be given a value of type node when initiated. I tried something like:

myNode=new Node(arguments);
myNode.parent=this;

Now, when I refer to parent I get nullpointerexception....

I tried to Google for it, but one gets tons of rubbish hits searching for 'java+this', as you might imagine. Does anyone know a method to do this?

Best regards,
Carlis.
 
Assuming your code is the only place where nodes are created, the provided code is only being run from within another instance of node, and the parent field is only being set there, then the only occurrence where parent == null should be for the root node.
 
Originally posted by: Carlis
Hi Im building a program with a hierarchy of nodes. Every node produces a set of new nodes and keeps track of them. These create new ones in turn etc.

Now, it is necessary that every node knows who its parent node is. Obviously, every node should have a field 'parent' which should be given a value of type node when initiated. I tried something like:

myNode=new Node(arguments);
myNode.parent=this;

Now, when I refer to parent I get nullpointerexception....

I tried to Google for it, but one gets tons of rubbish hits searching for 'java+this', as you might imagine. Does anyone know a method to do this?

Best regards,
Carlis.

First off, myNode.parent=this; is bad style. You should use a set method to set variables. It's just nice, clean and you only have to modify the method if you decide you need parsing(or some other modification to the input) later. I think it should work the way it is. This returns the current object(it's a reflection, like looking in the mirror). Without the code, I cannot test it to see where it is breaking. Why are you even setting the parent outside of the constructor? It seems like it would be easier to just add a parameter to the constructor for accepting an object of type Node.

*edit*
Also where are you calling this portion of code?
 
Hi again.
It would be a little messy to submit the entire code, since it consist of 9 classes and is dependent on a text file of 700+ lines (word list). The purpose of the program is to find the shortest way from the word 'fan' (satan) to the word 'gud' (god) by changing only a single letter at a time and demanding that any words in between do exist (ie, are present in a word list). After some testing I have concluded that most of the program works. It does find the word 'gud' (stored as 'wanted'). But the program fails to return the proper address (a Queue with all nodes above and it self). There is a try-statement in the makeAdress method that constantly fails since the vaule of parent is always null. Next to it, is a simple print-statement that I added to see whether the wanted word can see its parent. It cant, and causes NullPointer...

The value of parent is set in the method 'construct' and is marked ***.

I actually tried to ad a constructor that set parent directly, but it did not help me...







-----------------------------------------------------------------------------------------




public class MultiNode {


boolean isParent=false; // There are levels further down in the tree
boolean hasSon=false; // node has a nonzero number of sons.
boolean isTarget=false;
MultiNode parent;
String value;
MultiNode[] sons;
Queue adress=new Queue();
Bintree wordlist;
Bintree oldwords;
String wanted;
String[] SweAlpha={"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "å", "ä", "ö"};
String namesOfSons;


public MultiNode(String s, Bintree words, Bintree old, String target){
value=s;
wordlist=words;
oldwords=old;
wanted=target;
makeAdress(adress);

}

public void newSons(){
if(isParent==false){
makeSons();
isParent=true;
}
else{
if(hasSon==true){
for(int i=0; i<sons.length; i++){
sons.newSons();
}
}
}
}



public void makeSons(){
String[] c=value.split("");
String[] v=value.split("");
String test;
for(int i=0; i<c.length-1; i++){
for(int j=0; j<SweAlpha.length; j++){
String[] s={v[1], v[2], v[3]};
s=SweAlpha[j];
test=s[0]+s[1]+s[2];
makeWords(test);
}
}
if(hasSon){
construct(namesOfSons);
}


}

void makeWords(String s){
if(wordlist.hasElement(s)){
if(oldwords.hasElement(s)==false){
if(hasSon==false){
namesOfSons=s;
}
else{
namesOfSons=namesOfSons+" "+s;
}
hasSon=true;
}
}
}

public void construct(String s){
String[] k=s.split(" ");
sons=new MultiNode[k.length];
for(int i=0; i<k.length; i++){
sons=new MultiNode(k, wordlist, oldwords, wanted);
sons.parent=this; ***
oldwords.put(k);
if(k.equals(wanted)){
sons.isTarget=true;
}
}


}

public void makeAdress(Queue q){

if(value.equals(wanted)){
System.out.print(parent.value);
}

try{
parent.makeAdress(q);
}
catch(NullPointerException e){

}
Node n=new Node();
n.value=this;
q.Enque(n);
}

public boolean pathFound(){
boolean b=false;
if(value.equals(wanted)){
b=true;
}
if(hasSon==true){
for(int i=0; i<sons.length; i++){
if(sons.pathFound()==true){
b=true;
}
}
}
return(b);
}
public Queue getPath(){
Queue A=new Queue();
if(isTarget==true){
A=adress;
}
else{
for(int i=0; i<sons.length; i++){
if(sons.pathFound()==true){
A=sons.getPath();
}
}
}
return(A);
}

}

 
Update

I solved the problem with some minor changes in the program. Instead of setting a parent, I tried using a Queue of nodes with a clone method so that the constructor takes the address of its parent as an argument and ads itself, then clones its address-Queue and pass it on to its sons. Anyway, I had no problems using 'this' now. I never found out what the actual problem was though.

 
Originally posted by: Carlis
Update

I solved the problem with some minor changes in the program. Instead of setting a parent, I tried using a Queue of nodes with a clone method so that the constructor takes the address of its parent as an argument and ads itself, then clones its address-Queue and pass it on to its sons. Anyway, I had no problems using 'this' now. I never found out what the actual problem was though.

From the code posted your not even trying to set the parent field until after the MultiNode constructor finishes in 'construct()'. However its in the MultiNode constructor your trying to use that unset parent...
 
Originally posted by: Carlis
Of course... what the h*ll am I doing...

The one hard truth I've learned about development (and I've been doing it for some time) is that no matter how good you are there is ALWAYS that bug you just stare at and miss, sometimes for days. Its that bug that your coworkers looks over your shoulder and says 'there is the problem', and that diagnosis usually takes THEM 5 seconds.... 🙂

Cheers,
Bill
 
Originally posted by: bsobel
Originally posted by: Carlis
Of course... what the h*ll am I doing...

The one hard truth I've learned about development (and I've been doing it for some time) is that no matter how good you are there is ALWAYS that bug you just stare at and miss, sometimes for days. Its that bug that your coworkers looks over your shoulder and says 'there is the problem', and that diagnosis usually takes THEM 5 seconds.... 🙂

Cheers,
Bill

You're telling me...

I have plenty of bone headed stories like that. 😛
 
Haha, a lesson learned I guess. I am fairly new to programing...and, I thought... after a few small programs I would stop doing those things 😉
 
Back
Top