aspose file tools*
The moose likes Beginning Java and the fly likes Setting position Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Setting position" Watch "Setting position" New topic
Author

Setting position

Paul Jones
Greenhorn

Joined: May 03, 2006
Posts: 11
Hi,

I'm trying to set the position of something using the following statement;

public void setPosition(int newPosition)
{
if ((newPosition > aNumber) && (newPosition < aNumber))
{
this.setPosition(newPosition);
}
else
{
this.setPosition(aNumber);
}

I'm running it passed a simple filter to make sure its between a certain number, can anyone help?
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
Look at the two conditions in your while loop. Could they be true at the same time?
Paul Jones
Greenhorn

Joined: May 03, 2006
Posts: 11
Hi,

I've looked at my conditions, and changed them but now I get the return;

java.lang.StackOverflowError


Any ideas?

Cheers
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
Look at your stack trace. Do you see the problem?


There is no emoticon for what I am feeling!
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

StackOverflowExceptions often don't, perversely, show a stack trace, so let's give him a stronger hint: imagine that "newPosition" passes the test and takes the first branch in your if/else. What method is called? And during that method call, what happens, exactly? And what method is called? And during that method call, what happens, exactly? And ...


[Jess in Action][AskingGoodQuestions]
Paul Jones
Greenhorn

Joined: May 03, 2006
Posts: 11
Hi,

Thanks - so what you're saying is it starts recursively calling itself forever more?

How do I get out of that, assign a variable to the result, then pass that variable to setPosition() method?
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

setPosition() must be intended to set the value of a member variable, right? Instead of calling the routine recursively, this would be to place to make the actual assignment.
Paul Jones
Greenhorn

Joined: May 03, 2006
Posts: 11
Ernest,

Thanks v.much for your help, but how would I set the position if not using this method?

Paul
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
Could this setPosition method be overriding a version in the superclass? If so, when you call invoke setPosition in the method, you may be trying to call the super's version. Hmmm....

(BTW, my IDE treats my to a long, long stack trace when recursion diverges.)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Setting position