Solved

how to validate null value

Posted on 2012-03-16
11
203 Views
Last Modified: 2012-03-16
im retrieving a ABC object from database. the properties of object ccid is null and the achflag is N. ccid is integer and achflag is varchar(1)
log.debug values:
ccid null
achflag N

in my if condition it throws a null pointer exception.
if(ABC.getccid() == null && ABC.getachflag() == "N"){
//do something
}

my if condition fails and does not go in if loop
0
Comment
Question by:Newton21205
  • 8
  • 3
11 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 37731842
if you are talking about Java (not JavaScript) then

this
if(ABC.getccid() == null && ABC.getachflag() == "N")

witll throw nullpointer because  object ABC  is null
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37731846
this will not thow exception:
if(ABC != null && ABC.getccid() == null && ABC.getachflag() == "N"){
//do something
}
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37731851
this will not thow exception, and this is better because if you want
to compare Strings you need to use equals() method not ==
if(ABC != null && ABC.getccid() == null && ABC.getachflag().equals("N")){
//do something
}
0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 

Author Comment

by:Newton21205
ID: 37731872
im checking the object is not null and is not empty as shown in log.debug values.
im doing this:
if(ABC != null){
if(ABC.getccid() == null && ABC.getachflag() == "N"){
//do something
}
}

but it throws a null pointer exception
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37731875
which line exactly throws null pointer?
did you print stack trace and determine exact line?
0
 
LVL 47

Accepted Solution

by:
for_yan earned 100 total points
ID: 37731880
null pointer is thrown when you invoke method on the object which is null

this code
if(ABC != null){
if(ABC.getccid() == null && ABC.getachflag() == "N"){
//do something
}
}
should not thrwo null pointer, because you are checking upfront
check exact line using stack trace
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37731882
>and achflag is varchar(1)

varchar is not Java type it is database type

what Java type is retruened by ABC.getachflag() ?

post the contents of the class ABC
0
 

Author Comment

by:Newton21205
ID: 37731903
yes i did a try catch around the if it points to the if condition.
0
 

Author Comment

by:Newton21205
ID: 37731904
the Java datatype for achflag is String varchar is the database datatype
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37731905
post the code for class to which variable ABC belongs
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37731906
what code is in the part //do something ?
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
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 learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

776 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