*
The moose likes Beginning Java and the fly likes Call to static method does not return Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Call to static method does not return" Watch "Call to static method does not return" New topic
Author

Call to static method does not return

John Norris
Greenhorn

Joined: Jul 22, 2008
Posts: 14
Not sure if this would start here but will give it a shot.

I have the following code snippet:



The static method in class Test is


Trying to figure out why when load completes, it does not return to callTest. I see "returnCode: 1" instead. The actual code is more complex than this but still can't figure why it doesn't return.

Thanks in advance.

[edit]Add code tags. CR[/edit]
[ August 13, 2008: Message edited by: Campbell Ritchie ]
Ronald Schild
Ranch Hand

Joined: Jun 09, 2008
Posts: 117
Is this load the function that is called? It seems to take a reference to an array of String objects, not a reference to a String object.

Are you sure there is no overloaded

public static int load(String stringArgument) { ...

method that is called instead?


Java hobbyist.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38075
    
  22
Apart from the compiler errors in the parameters for both the methods you quoted, I can't see anything wrong with your code; it ran nicely for me.
Bill Shirley
Ranch Hand

Joined: Nov 08, 2007
Posts: 457
perhaps if you post the more complicated code, we could help find the issue


Bill Shirley - bshirley - frazerbilt.com
if (Posts < 30) you.read( JavaRanchFAQ);
John Norris
Greenhorn

Joined: Jul 22, 2008
Posts: 14
Yes, sorry about the compile errors.
It is my experience that this works also. This occurs in a front end method that references a public class with a static method. This method makes a call to a mainframe application that updates files on the server. Once complete it should return back to the calling method to finish some cleanup. What appears to be happening is that the static method returns control to the parent of the calling method. No errors appear to be thrown.

Can you think of any instance where a completed return does not actually go back to the caller?

Thanks in advance.
John Norris
Greenhorn

Joined: Jul 22, 2008
Posts: 14
Here's a bit of the complicated code



and


The code never reaches the returnCode logic in refreshData(). The last entries will be "TestDataLoader successful." followed by "returnCode: 0"

Thanks in advance,
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38075
    
  22
Can't see why it isn't returning, and I am assuming you don't get any Exceptions in loadData() and that you don't really have an empty catch. Are you sure refreshData is where loadData is called from? Is it being called from elsewhere? If the return value is returned elsewhere, then you won't see anything in refreshData().

But why are you using error codes in the first place? The Java convention for error handling doesn't use error codes at all. If anything goes wrong it ought not to be a case of "return," but something more assertive . . .


throw new XYZException();

Difficult to know what the best thing would be, but I would be tempted to remove the try-catch from the loadData() method and declare whichever Exception you need.
Then catch the Exception in the refreshData() method.
If you are actually calling loadData() from elsewhere, you might find that out when you get a compiler error about unhandled Exceptions.

Anybody else got any ideas? There are bound to be different ways to handle this problem.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38075
    
  22
Have you really got an empty catch{} in the act() method? Disastrous! All sorts of things could be going wrong and you don't find out. At least put e.printStackTrace(); in that catch. An Exception going through refreshData() could cause such problems.
John Norris
Greenhorn

Joined: Jul 22, 2008
Posts: 14
The basis of this code is vendor specific and was written in '02. There are many things which I would like to change but my goal is to stay as close to baseline as possible. My call to loadData() is shoe horned into the current framework and any other calls are generated from an outside http call, which is why I chose to write this method.
This is my attempt at refreshing the data daily without user input. If it fails, I still have the option of refreshing it manually.
Yes, I removed considerable vendor specific code from the example given. There are vendor specific debug statements in each of the catches, which is why I'm sure none of them are being triggered.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38075
    
  22
Thank you. It looks like old code, and you have been dropped in it. Can't think of anything else, I am afraid.
Bill Shirley
Ranch Hand

Joined: Nov 08, 2007
Posts: 457
there is no way for code execution to "jump" someone on the call stack without throwing an exception,

have you tracked this in the debugger?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Can you think of any instance where a completed return does not actually go back to the caller?
That could happen if an unchecked exception is thrown. And (just speculating) if you didn't do something that sets up the environment for that static method, then it could throw a NullPointerException because you didn't assign a value to some variable.
John Norris
Greenhorn

Joined: Jul 22, 2008
Posts: 14
Debugging mode...That's something I can only dream about. This is the only shop I've been in where I can neither get to nor control a dev region. I could go on and on...
Anyway, the last comment, along with the one about no jumping, peaked my interest as refreshData() was only catching a Runtime exception. I've modified this to catch an exception and given the ear to the was admins to deploy and will let you know the results.

Thanks
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38075
    
  22
Odd about RuntimeException; if it were likely to throw another Exception that would be a checked Exception and the compiler would complain about it.
Disclaimer:

Don't try this at home.
If you get really desperate do something I usually advise against . . . go into the act() method and change your catch(Exception e){} toThat will get round the remote possibility of your having an Error occurring instead of an Exception.

Another wildly lunatic suggestion, instead:
Back to the loadData() method and replace its return statement with
throw new Throwable();
Again, something you don't usually do, but it ought to crash through all your Exception catching and bring down your thread, and give you a stack trace so you can see where your method calls are.
Bill Shirley
Ranch Hand

Joined: Nov 08, 2007
Posts: 457
readdessing "returnCode: 1"

also,
if anywhere System.exit() is called, the control will not return to the caller,
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38075
    
  22
Originally posted by Bill Shirley:
if anywhere System.exit() is called, the control will not return to the caller,
. . . but it would be obvious that the JVM has stopped.
John Norris
Greenhorn

Joined: Jul 22, 2008
Posts: 14
I want to thank all of you for your help on this topic. I finally located the problem and developed a work around. When I have time I'll try to go back and understand exactly what happened. For some reason the logging logic broke between going to the static method and returning. It was completing all along but stopped logging progress when it came back with the returnCode. Found it by using logic to give me the calling method along with using System.out as opposed to the vendor logger.

Thanks again for your help.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38075
    
  22
Well done. It just goes to show what sort of problems one can get with maintaining code.
 
wood burning stoves
 
subject: Call to static method does not return
 
Similar Threads
Getting a return code from main
Overriding Clarification
TO STRING.
returning a String containing the code from a class
rules engine decision table book or web page