?
Solved

how to validate null value

Posted on 2012-03-16
11
Medium Priority
?
210 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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 400 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
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:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses
Course of the Month13 days, 18 hours left to enroll

807 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