File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Setting position

 
Paul Jones
Greenhorn
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2399
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look at the two conditions in your while loop. Could they be true at the same time?
 
Paul Jones
Greenhorn
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1780
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look at your stack trace. Do you see the problem?
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ...
 
Paul Jones
Greenhorn
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ernest,

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

Paul
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.)
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic