Solved

if statement doesn´t recognize null nor ""

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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

760 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now