• Post Reply Bookmark Topic Watch Topic
  • New Topic

Why a method is not returning a value?  RSS feed

 
Alex Orozco
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,

I'm new to Java and I have a problem with a method, I can't see the code of the method, I just have a jar, but it should return a boolean, something like this:



The problem is that the method seems that is returning nothing (band remain false), and if I initialize band to true:



band remain true, in other words, the value of band is never modified, the question is, how is this possible? because it should return the same value on both calls, true or false, no matter the initial value of the variable that is receiving the returning value of the method.

Any ideas why this could happen?
Thanks!
 
Paweł Baczyński
Bartender
Posts: 2077
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If your code compiles then this method is returning a boolean.
I really can't help without more details.
Are you sure that this method gets executed? Maybe it is inside a branch of code that is not being executed.
 
Rico Felix
Ranch Hand
Posts: 411
5
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In order for such code to compile in the first place the method call has to return a boolean value....

To test the method more appropriately you should use and output statement after calling the method and assigning the value return to the variable such as:

 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We need more detail to be sure. How are you checking the value afterwards?

One thing: your description of what you are doing doesn't match the code. You show code that initialises the variable to false, and say it was initialised to true. Make sure you're giving us the correct details.
 
Alex Orozco
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

To check the value I'm debugging the app, there I can see that the value doesn't change on both cases. sorry if I can't provide more information about the method, it is on a jar, so I can't see what exactly is happening.

Matthew, sorry if I can't explain it well, English is not my first language, what I'm trying to say is if the variable is initialized to false, after calling the method, the variable remains false, and if the variable is initialized to true, after calling the method, the variable remains true, this behavior is weird to me, because on both calls it should return the same value.

Thanks
 
Alex Orozco
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your answers,

I finally figured out what is happening, the problem is that I have a try/catch block surrounding the method calling, the method is throwing an exception, and in the catch block I was not printing the stack trace, that's why I though that the method was working fine, and since the method is failing the value is never updated.

Thanks again!
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cesar Ivan Orozco Cervantes wrote: and in the catch block I was not printing the stack trace


Ah! A useful lesson, that. Never do that - it's a great way of making bugs hard to find.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!