Solved

if statement doesn´t recognize null nor ""

Posted on 2003-11-09
4
247 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
  • 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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

756 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