The Artima Developer Community
Sponsored Link

Java Answers Forum
do while help

3 replies on 1 page. Most recent reply: Jul 7, 2004 3:49 PM by Kishori Sharan

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 3 replies on 1 page
hyderman

Posts: 11
Nickname: salem99
Registered: Oct, 2002

do while help Posted: Jul 7, 2004 9:07 AM
Reply to this message Reply
Advertisement
hi
i hav a problem with do while in this code


public int find (int value)
{

int counter = 0;
Node tempNode = root;

do {

counter++;
if (value == tempNode.getValue() )
{
this.current = tempNode;
return counter;
}
else if (value < tempNode.getValue() )
{
tempNode = tempNode.getLeft();
//this.current = tempNode;
}
else
{
tempNode = tempNode.getRight();
//this.current = tempNode;
}
}
//current = null;
return -1 * counter;
} while (tempNode != null);

its not running and i am confused where is the problem here.

thanx


mausam

Posts: 243
Nickname: mausam
Registered: Sep, 2003

Re: do while help Posted: Jul 7, 2004 9:32 AM
Reply to this message Reply
How did ur code compiled first.

You have put while at the end of function

Put while at the end of do loop
}
//current = null;
return -1 * counter;
} while (tempNode != null);
 

should read
}while (tempNode != null);
 
//current = null;
return -1 * counter;
} 

hyderman

Posts: 11
Nickname: salem99
Registered: Oct, 2002

Re: do while help Posted: Jul 7, 2004 10:23 AM
Reply to this message Reply
thanx for replying, actually still giving me running errors

public int find (int value)
{
int counter = 0;
Node tempNode = root;
do{
counter++;
if (value == tempNode.getValue() )
{
this.current = tempNode;
return counter;
}
else if (value < tempNode.getValue() )
{
tempNode = tempNode.getLeft();
//this.current = tempNode;
}
else
{
tempNode = tempNode.getRight();
//this.current = tempNode;
}
}
//current = null;
while (tempNode != null);

return -1 * counter;
}

i am not sure where is the error here, i must use either do while or for loop, i tried several ways and still running error, it compiles but it gives running errors

message:
"exception in thread "main" java.lang.NullPointerException"

Kishori Sharan

Posts: 211
Nickname: kishori
Registered: Feb, 2002

Re: do while help Posted: Jul 7, 2004 3:49 PM
Reply to this message Reply
Unless you post all your code it is hard to tell why are you getting Nullpointer exception. It seems your root variable is null to begin with. That is what, you are getting that error. Just to debug it, print before you start do-while loop to see if root is null or not. Just before do, write as:
Node tempNode = root;
 if ( root == null ) {
   System.out.println ( "root is null");
   return -1;
 }

Flat View: This topic has 3 replies on 1 page
Topic: Query just written MS ACCESS Previous Topic   Next Topic Topic: Always on top Frame

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use