public String function(String person, String area){
System.out.println("area"+area); //verified "null" is obtained here
if(area==null){
System.out.println("case1:"+area);
}
return area;
}
I am not getting the print specified inside the if condition why is it happening? it is a database connecting page consists of 2100 lines of codes. can any one tell me possible reason for this? am really fed up with this. it was working fine before. :(
could it be that area is "null" and not null?
if that is the case and area is a database value of type varchar try this:
if(area==null || area.toLowerCase().equals("null")){
btw. not sure if toLowerCase is needed.
and by the way :-) this is much better.
if(area==null || "null".equals(area.toLowerCase())){
anyway. null safetyness is not necessary because of the area==null.
if area is null the whole if condition will be true.
Related
I am attempting to eliminate a large amount of IF/ELSE statements from my code with the use of the ternary operator.
//Checks to see if person exists //if not then add //else print
println doesPersonExist(personName) ? addPerson(personName) : 'No such person'
The problem is that if the addPerson(personName) method is executed then NULL is printed due to the println. Is there anyway of NULL from printng to the console? I understand i could remove the println but then 'No such person' would not print.
UPDATE: I finally managed to get this working as required - i simply removed the println and also 'No such person' and replaced them with this:
doesCarrierExist(carrierName) ? exists() : addCarrier(carrierName)
Where the exists method simply called a println statement within the base class. I hope this helps anyone that has a similar problem in the future.
In your code you mix 2 different actions.
The print method.
The addPerson(personName).
And you wish to print that into the screen.
The best way is handling it as 2 separate handles one for each action (for the addition and for the printing).
If the addPerson(personName) returns a String result you can print to the console the result by using the following Groovy code section:
def textResult = doesPersonExist(personName) ? addPerson(personName) : ""
println(textResult)
Note: If you are using Java language replace the 'def' with 'String'.
In Jaspersoft Studio I have tried the following expression. I am getting null but I don't understand why. This should be as simple as 3.00/2 and display 1.50 however it is not working it still shows null. I have confirmed that the fields contain values for all fields.
The expression I am using is as follows:
new Double($V{UnitPrice}.doubleValue() == 0 ? 0 : ($F{Price Qty}.doubleValue()/$F{Price}.doubleValue()))
Since you are using Double for your arithmetic, why not use the compareTo(Double anotherDouble)?
Not sure if this is the source of your trouble, but it could be the == behaving in a way that you did not intend it to and returning false hence, the zero...
Never thought I will be asking such a trifle query!
Please help with this stack:
if(request.getParameter("cl")=="star"){
txtFilePath = session.getServletContext().getRealPath("/cl.json");
}
else
{ txtFilePath = session.getServletContext().getRealPath("/kl.json");
}
Irrespective of passing cl=star in the url parameters, its not bringing any difference, and showing only kl.json
This code is in jsp, and as of now, no other framework is being used. It's being tested on Tom cat container.
NEVER compare strings using ==.
Always compare using the equals method.
request.getParameter("cl").equals("star")
Note that when using == to compare string objects, you are not comparing it's values but it's references.
I am getting the occasional odd result from String.equals(String).
here is the code:
boolean equals(OutputHolder other){
boolean result=true;
if( ! this.speciesEng.equals(other.speciesEng))
result=false;
else if( ! this.date.equals(other.date))
result=false;
else if( ! this.gridRef.equals(other.gridRef))
result=false;
else if( ! this.recorder.equals(other.recorder))
result=false;
return result;
}
All pretty straight forward but on some objects .equals() returns false on what appear to be 2 identical strings.
This is a screenshot of the Expressions watchlist when the error occurs. As you can see this.speciesEng.equals(other.speciesEng) is returning false despite the fact that both strings appear the same.
The strings are initially from the same string resource but both have passed over an XMPP connection and back.
EDIT: To pass them over the XMPP connection, they have been concatenated with other strings to represent the whole OutputHolder. They are then separated on return using .substring(start,end). It occurred to me that it might make a difference if I made a new string from the substring but that didn't have any effect. Neither did trimming them.
I am at a loss as to how to proceed with debugging the problem. Any help or suggestions welcome.
There might be some whitespaces in there. Use String#trim method prior to calling equals.
Make sure there are no trailing spaces. So better use trim method on the strings before comparing them using equals method.
I think you should first trim both the strings and get rid of additional spaces.
That way you will be able to equate both the Strings properly.
Example Code:
String yourString = "Your String ";
//Trim the String and get rid of extra spaces before doing any comparisons.
yourString.trim();
//After trimming it, do the comparisons.
if(yourString.equalsIgnoreCase("other trimmed string"))
{
.....
}
I hope this helps.
After god knows how many hours copying and pasting from the debugger to a hex editor I have found the problem and a solution that works.
As suggested the problem was whitespaces but not in the way I or (I think) others suspected.
For some reason that I have failed to get to the bottom of, I am getting non-breaking whitespaces (0x00A0) in my strings instead of normal whitespaces (Ox0020). This appears to be happening more or less at random and I haven't found the section of code responsible yet.
The work around at the moment is to start my equals() method with:
speciesEng=speciesEng.replace((char)0x00a0,(char)0x0020);
other.speciesEng=other.speciesEng.replace((char)0x00a0,(char)0x0020);
speciesEng=speciesEng.trim();
other.speciesEng=other.speciesEng.trim();
Far from elegant but it works for the moment. I'll leave the question open for a couple of days in case anyone has more to add.
Thanks to all for the answers.
boolean equals(OutputHolder other){
boolean t1 = speciesEng.equals(other.speciesEng);
boolean t2 = date.equals(other.date);
boolean t3 = gridRef.equals(other.gridRef);
boolean t4 = recorder.equals(other.recorder);
return t1 && t2 && t3 && t4;
}
I want to check that text is now not present on a webpage without my test failing
I have achieved this by catching the exception but is there a better way of doing this?
try {
selenium.isTextPresent(selenium.getText("27"));
} catch (Exception e) {
System.out.println("Element does not exhist");
}
There's nothing wrong with try-catch.
Anyway, what you have should be more of a isElementPresent("27") since you discard the return value of isTextPresent(). Moreover, if the element exists, then isTextPresent() will always return true, because ... well, you took the text out of existing element, it has to be there. In this case, it is enough just to assure whether the element exists or not.
But if you do actually need it in real code somehow, then what about if (selenium.isElementPresent("27") && selenium.isTextPresent(selenium.getText("27"))) ?
Also, getXpathCount(//*[#id='27' and text()]) == 0 expression does the trick, too. It count the number of returned elements that have id=27 and contain some text. If that's a zero, there are none.
you can use assertTextNotPresent OR verifyTextNotPresent
# Slanec Yes, its my mistake
You can use
verifyFalse(selenium.isTextPresent("text"));
assertFalse(selenium.isTextPresent("text"));
Based on the docs you should be able to say the following:
selenium.isTextPresent("27"));
This returns a boolean so if you want to make sure it is not there... check it is false.