Solved

if statement doesn´t recognize null nor ""

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

Suggested Solutions

Title # Comments Views Activity
login jsp example 24 105
Custom Java Application connects to database: Which one of the 20? 4 58
jsp login check 12 66
add projects t working set in maven 2 70
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
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…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
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

738 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