• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Input parameter retain value

 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I am maintaining an application developed by a former colleague and a Java class converts a String input parameter named "value" in a function to a Long value with the following code;

Long retval = Long.parseLong(value);

This occurs within a try catch block and also within the try catch block is the return value of the function with the code;

return retval

However, the return statement above makes the input parameter "value" an empty string. How can I retain the value of the parameter "value" for further manipulation?

Thanks in advance

AJF

 
Ranch Hand
Posts: 157
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

However, the return statement above makes the input parameter "value" an empty string. How can I retain the value of the parameter "value" for further manipulation?



Hi Farroll, when you say Long.parseLong(str) it returns the parsed long value of the str String, it does no update the str object itself. How do you know that the "value" is an empty string ? Are you trying to manipulate the "value" in caller or within the function..

can you post the code snippet for better understanding.



 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

A Farroll wrote:However, the return statement above makes the input parameter "value" an empty string.


No, it does not. Variables are passed by value in Java; a method such as Long.parseLong(...) cannot change the value of the variable that you are passing to it, because it doesn't get a reference to the variable - it just gets the value of the variable.

Something else must be happening in your code, but we can't tell you what without seeing more of the code.
 
Ranch Hand
Posts: 789
Python C++ Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you print the value of "value" just before and just after the call you'll see it didn't change. You can use strategic prints to track down where something went haywire.
 
A Farroll
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all. Thanks for responding. I will post the code of the entire procedure below and frankly I am very confused at what the original developer is trying to do with the second try catch block. Have a look and see. When the second Long retval = Long.parseLong(value); occurs the "value" is empty and creating enormous entries in the logs - this is the problem I am trying to resolve. Right before the first Long retval = Long.parseLong(value); I have placed a system out and the "value" contains a value, but after the first return retval the "value" is empty. I hav tried swapping around the scope, i.e. having retval declared outside the try-catch but it does not work.

Thanks in advance. Also, this is actually Xpages but I have found forum help on Xpages very few and far between.

 
Rancher
Posts: 1044
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If I did not overlook it, the variable "value" does not get assigned at all and there is no other way for it to change.

Your assumptions about what is going on might be wrong.

Welll-placed debug output statements might shed light.

 
A Farroll
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ivan Jozsef Balazs thanks for your response. I have been trying to this working all day again and have found the only fix is a sort of "fudge fix" that I am not happy with. Regarding the parameter "value" it does have an actual value because I have input several debug statements are the parameter named "value" outputs a value right up to the first return retval. After that it does not have a value. I even tried creating a new Class of type String and used this to create an object that will store the value of the parameter named "value" and it does this successfully in the first try-catch block but when I tried to retrieve the value from the object in the second try-catch block it states it is null.

However if I place the code below (the fudge fix) in the second try-catch block, the problem goes away.

String firstValue = "";

if(firstValue == ""){
firstValue = "0";
}

Long retval = Long.parseLong(firstValue);
return retval;

I still cannot understand why this is occurring

Regards

AJF
 
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not convinced by this bit of the code (lines 14 - 17 in the above):


If value is empty then it will never reach the if statement as the parseLong will have thrown a NumberFormatException, so no point with the check there at all.

That aside, what is printed out by the println statement in the else block?
 
A Farroll
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dave Tolls wrote:Not convinced by this bit of the code (lines 14 - 17 in the above):


If value is empty then it will never reach the if statement as the parseLong will have thrown a NumberFormatException, so no point with the check there at all.

That aside, what is printed out by the println statement in the else block?



Thanks for response Dave Tolls. If have put in numerous debugging outputs in this class, they are just now shown in the code above as I wanted to keep it clean and readable.

I have placed dubug outputs around the lines 14 - 17 and the "value" parameter does have a value even after the Long retval = Long.parseLong(value); line of code on line 14. In fact the Xpage interface and XML has conditions to ensure it has a value.

To answer your second question of "what is printed out by the println statement in the else block" - it simple prints out in the logs "value is ". This is what is really confusing me because right before the "return retval" in the first try-catch the "value" parameter has a value as I have outputted it previously but by the time it gets to the else block the "value" parameter is empty!!! I have tried numerous things here, changing the scope of the retval variable, removing the second try-catch block, removing the else block and all these cause other issues. I have even tried creating a seperate class with a getter and setter to store the "value" parameter in an object and using this it retains the value of the "value" parameter inside the FIRST try-catch statement but NOT THE SECOND try-catch block.

That is why the only solution I have found that works is the fudge up I described in my previous comment which I do not like. A former colleague who was an Xpages person developed this and then left the company with little documentation

Regards

AJF
 
Dave Tolls
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, I could be wrong as the flow of the code there is a bit all over the place, but it looks to me like you will never enter the last try/catch block unless it has gone through the else part of your first if/else.
So in other words it will have done nothing in the if block.

Consequently if you are seeing the "value is " bit being printed with no value then it has no value entering the method.

Anything in the if block is irrelevant.
 
A Farroll
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks again Dave Tolls

I confirm the "value" parameter has a value before the If statement begins because I have output a debug statement to make sure. I then tried placing the second try-catch block inside the else statement as I can see how that would make sense but I'm afraid the outcome is still the same.

Perhaps it is the initial If condition??? Speaking as an non XPages person what exactly is that checking in the If condition? Although I can confirm the "UiValidationTools.doValidation" is another class in the application and the "doValidation" part is a function within it that returns a type Boolean, but what about the "(UIInputText)component, context)" part of the If condition???

Regards

AJF
 
Dave Tolls
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, I've pared your code down to the relevant bits around value (and the flow, so we see the exit points):

Nothing in there changes the value of "value".
Nothing.

In addition, you will never get to the second try block when you have entered the if() statement, as all routes through there result in the method exiting inside the if.

So you are passing in an empty String for "value".
 
A Farroll
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks once again Dave. I agree there doesn't seem to be anything changing the value of "value" that is why I found it so confusing. I have spent some time trying to get the contents of "value" parameter to the second try-catch block. How do I do this or amend the try-catch to allow this?

Regards

AJF
 
Dave Tolls
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

A Farroll wrote:Thanks once again Dave. I agree there doesn't seem to be anything changing the value of "value" that is why I found it so confusing. I have spent some time trying to get the contents of "value" parameter to the second try-catch block. How do I do this or amend the try-catch to allow this?

Regards

AJF



So what is it you are actually trying to achieve?
Because it really isn't clear.

"get(ting) the contents of "value" parameter to the second try-catch block" explains nothing.
You could do that by simply sticking it at the beginning of the method and removing everything else.
 
A Farroll
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dave Tolls wrote:

A Farroll wrote:Thanks once again Dave. I agree there doesn't seem to be anything changing the value of "value" that is why I found it so confusing. I have spent some time trying to get the contents of "value" parameter to the second try-catch block. How do I do this or amend the try-catch to allow this?

Regards

AJF



So what is it you are actually trying to achieve?
Because it really isn't clear.

"get(ting) the contents of "value" parameter to the second try-catch block" explains nothing.
You could do that by simply sticking it at the beginning of the method and removing everything else.



What I attempting to acheive is amend the If - else statement or\and the second try catch block to process the second "Long retval = Long.parseLong(value)" and "return retval" statements with the original value of the "value" parameter. I have already attempted placing a single "Long retval = Long.parseLong(value)" before the If statement but I kept getting errors with that too.

Thanks

Regards

AJF
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The first thing I would do is reformat the code. It's much easier to analyze code that is correctly indented and not ragged as a chain smoker's breath after a 50-meter sprint. If you haven't done this already with your code, then you should do it. It won't change anything but it will help everyone follow the flow much better. Also, you can use the Preview button to see what your message will look like when posted. If you're using code tags, you can see the line numbers in code listings. If you reference the line numbers, it's easier to find which line you're talking about vs with something like "the first retval".

The initial if condition is not the problem and that (UIInputText)component, context part is not really part of the conditional expression; these are just the parameters getting passed to the boolean method that you're calling. The if really only sees one boolean expression: whatever that method call returns. If you look at the signature of the doValidation method, it's probably something like this:

That's the only reason you would cast component to UIInputText. Otherwise, the cast probably isn't needed.

What are you trying to say in the comments on lines 60/61 and lines 85/86, where you say "parameter is in scope" and "parameter is outside scope"? A method parameter is always IN scope inside the method; it will never be out of scope until you exit the method. From what I see, there's simply no way that the value parameter could not be empty when line 5 is executed then be empty if and when line 88 is executed.

Try doing this (note the line numbers in the reformatted code above):

and this:

 
Dave Tolls
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

A Farroll wrote:

Dave Tolls wrote:

A Farroll wrote:Thanks once again Dave. I agree there doesn't seem to be anything changing the value of "value" that is why I found it so confusing. I have spent some time trying to get the contents of "value" parameter to the second try-catch block. How do I do this or amend the try-catch to allow this?

Regards

AJF



So what is it you are actually trying to achieve?
Because it really isn't clear.

"get(ting) the contents of "value" parameter to the second try-catch block" explains nothing.
You could do that by simply sticking it at the beginning of the method and removing everything else.



What I attempting to acheive is amend the If - else statement or\and the second try catch block to process the second "Long retval = Long.parseLong(value)" and "return retval" statements with the original value of the "value" parameter. I have already attempted placing a single "Long retval = Long.parseLong(value)" before the If statement but I kept getting errors with that too.

Thanks

Regards

AJF



I get the impression you're floundering a bit.

You also don't seem to be able to actually describe what you are trying to do.
You seem to be trying to fix a bug by curing the symptoms (the NumberFormatException) rather than the actual cause (the empty String).
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dave Tolls wrote:
You also don't seem to be able to actually describe what you are trying to do.
You seem to be trying to fix a bug by curing the symptoms (the NumberFormatException) rather than the actual cause (the empty String).


I agree. The method itself is quite confused. It declares the return type as Object but really will only return a Long. Why do that and why not call the method getAsLong() instead? Even if you change the signature to return a Long instead, any code that looks like this will still work:

The cast on line 7 will be unnecessary but it still works.

Another part where the code is confused in the min/max values: they are declared as Integer and yet, the value you are checking is supposed to be a Long. Then, when comparing the long value to the min/max value, it uses the doubleValue() of the Integer. None of that makes a whole lot of sense to me.

And since we're talking about refactoring at this point, why does a getAsWhatever() method have to perform both range validation and conversion? At a minimum, I would break this down into methods with separate responsibilities:

By trying to disturb the current code as little as possible and not refactoring instead, you actually make your job a lot more difficult. Clean up the code and you'll probably be done with this a lot sooner.
 
A Farroll
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are both correct Junilu Lacar and Dave Toll. I have never been trained in Xpages you see and no expert in XPages. Also about the Integer part again you are correct. I did not want to confuse this forum thread any further but the reason for the integer is because as well as the documents (they are called Contracts) being stored in Notes they are also pushed through to a SQL Server Database and so requires some handling as an integer in SQL Server as well as a Long in Notes - YES I realise this is duplication of data and overly complicated without it being necessary to be but I did not design this application. I am just stuck with maintaining it. I would frankly rebuild it in a less complicated manner.

Dave Toll you were close regarding the doValidation declaration. It is;

public static boolean doValidation(UIInputText field,FacesContext facescontext) {

...

}

I will try what you guys have suggested and let you know. It is finishing time here today where I am. I will try it tomorrow morning.

Thanks for your patience

Regards

AJF
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

A Farroll wrote:... the reason for the integer is because as well as the documents (they are called Contracts) being stored in Notes they are also pushed through to a SQL Server Database and so requires some handling as an integer in SQL Server as well as a Long in Notes - YES I realise this is duplication of data and overly complicated without it being necessary to be but I did not design this application. I am just stuck with maintaining it. I would frankly rebuild it in a less complicated manner.


Then all that translation should be in the Data Access layer. For saving the value to Notes or SQL Server, you should be able to use a reference to Number and call its intValue() or longValue() method as appropriate. Apart from this, if the value is naturally a String then most of your code should use it as a String. Only when it needs to go into Notes or SQL Server should it be converted to Long or Integer, whatever the case may be.

Frankly, being stuck maintaining the code is a poor excuse for not cleaning it up. Regardless of who originally wrote it, you are accumulating technical debt by not cleaning up the code as and when you can. This only delays the pain and suffering that you or someone else will inevitably have to deal with later on. It's best to deal with it now, while the problem is still small. With some effort, you could totally refactor this code and clean it up.

A Farroll wrote:Dave Toll you were close regarding the doValidation declaration. It is;

public static boolean doValidation(UIInputText field,FacesContext facescontext) {...


That was me, Junilu, guessing what the doValidation signature was... And it's Dave Tolls, with an "s".
 
A Farroll
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would like to thank you both Junilu Lacar and Dave Tolls for your feedback

I will work away with this and approach my boss about refactoring some of the classes and see if I can be allocated time to do this.

Regards

AJF
 
reply
    Bookmark Topic Watch Topic
  • New Topic