Solved

if statement doesn´t recognize null nor ""

Posted on 2003-11-09
4
249 Views
Last Modified: 2010-03-31
Hello,
I am trying to use an if statement to select whether or not to execute a code. But I don´t know why it isn´t recognizing the null or blank .
I have the folowing code
if (aeroportoPartida.getCodigoAeroporto() == null || aeroportoPartida.getCodigoAeroporto() == "")
{
....
}
But it is not getting in it!!! this tests if a textField is blank.
I testeted with System.out.println(aeroportoPartida.getCodigoAeroporto()); to see if it was blank , and it prints nothing when it is blank , and when I write something in the text field it prints the thing I wrote!!
Why the if statement isn´t recognizing it??!! Is there another word or way to know what it would be if the text field is blank!!!

Thanks
         
             
0
Comment
Question by:idur
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 15

Accepted Solution

by:
JakobA earned 25 total points
ID: 9712060
what is the return type of your method getCodigoAeroporto() ? and what does it return?

     == null    should work
     == ""       will never work the == operator compares the object reference for the returned string with the object-refernece of the constant "", and they will never match.
to compare the content of to thrings use the string method  equals
    aeroportoPartida.getCodigoAeroporto().equals( "" );

adduming that getCodigoAeroporto() returns a String.

regards JakobA
0
 
LVL 15

Expert Comment

by:JakobA
ID: 9717447

if ( aeroportoPartida.getCodigoAeroporto().equals("") )
{
    ....
}
0
 
LVL 7

Expert Comment

by:grim_toaster
ID: 9720698
Erm, by text field do you mean a Swing text field, or something along those sorts of lines?  If it is a Swing one, then you will need to do:

aeroportoPartida.getCodigoAeroporto().getText().length() == 0

Or you could do the .equals(""), but this is less efficient than .length() == 0, as you are creating a new instance of String, doing some casting, and then comparing lengths, etc.  I've seen worse though: .compareToIgnoreCase("").  Although to be honest, you probably wouldn't notice the difference unless you were doing it millions of times.


If it's not this, please provide a bit more of your code, stating what getCodigoAeroporto() returns.
0
 

Author Comment

by:idur
ID: 9723502
Thanks
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Suggested Courses

615 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question