Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

if statement doesn´t recognize null nor ""

Posted on 2003-11-09
4
Medium Priority
?
250 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 100 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

Independent Software Vendors: 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

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
The viewer will learn how to implement Singleton Design Pattern in Java.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

661 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