Java. Refering to 'self'

Carlis

Senior member
May 19, 2006
237
0
76
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.
 

mundane

Diamond Member
Jun 7, 2002
5,603
8
81
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.
 

mcmilljb

Platinum Member
May 17, 2005
2,144
2
81
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?
 

Carlis

Senior member
May 19, 2006
237
0
76
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);
}

}

 

Carlis

Senior member
May 19, 2006
237
0
76
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.

 

bsobel

Moderator Emeritus<br>Elite Member
Dec 9, 2001
13,346
0
0
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...
 

bsobel

Moderator Emeritus<br>Elite Member
Dec 9, 2001
13,346
0
0
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
 

tfinch2

Lifer
Feb 3, 2004
22,114
1
0
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.
 

Carlis

Senior member
May 19, 2006
237
0
76
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
 
sale-70-410-exam    | Exam-200-125-pdf    | we-sale-70-410-exam    | hot-sale-70-410-exam    | Latest-exam-700-603-Dumps    | Dumps-98-363-exams-date    | Certs-200-125-date    | Dumps-300-075-exams-date    | hot-sale-book-C8010-726-book    | Hot-Sale-200-310-Exam    | Exam-Description-200-310-dumps?    | hot-sale-book-200-125-book    | Latest-Updated-300-209-Exam    | Dumps-210-260-exams-date    | Download-200-125-Exam-PDF    | Exam-Description-300-101-dumps    | Certs-300-101-date    | Hot-Sale-300-075-Exam    | Latest-exam-200-125-Dumps    | Exam-Description-200-125-dumps    | Latest-Updated-300-075-Exam    | hot-sale-book-210-260-book    | Dumps-200-901-exams-date    | Certs-200-901-date    | Latest-exam-1Z0-062-Dumps    | Hot-Sale-1Z0-062-Exam    | Certs-CSSLP-date    | 100%-Pass-70-383-Exams    | Latest-JN0-360-real-exam-questions    | 100%-Pass-4A0-100-Real-Exam-Questions    | Dumps-300-135-exams-date    | Passed-200-105-Tech-Exams    | Latest-Updated-200-310-Exam    | Download-300-070-Exam-PDF    | Hot-Sale-JN0-360-Exam    | 100%-Pass-JN0-360-Exams    | 100%-Pass-JN0-360-Real-Exam-Questions    | Dumps-JN0-360-exams-date    | Exam-Description-1Z0-876-dumps    | Latest-exam-1Z0-876-Dumps    | Dumps-HPE0-Y53-exams-date    | 2017-Latest-HPE0-Y53-Exam    | 100%-Pass-HPE0-Y53-Real-Exam-Questions    | Pass-4A0-100-Exam    | Latest-4A0-100-Questions    | Dumps-98-365-exams-date    | 2017-Latest-98-365-Exam    | 100%-Pass-VCS-254-Exams    | 2017-Latest-VCS-273-Exam    | Dumps-200-355-exams-date    | 2017-Latest-300-320-Exam    | Pass-300-101-Exam    | 100%-Pass-300-115-Exams    |
http://www.portvapes.co.uk/    | http://www.portvapes.co.uk/    |