• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 214
  • Last Modified:

how to validate null value

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
Newton21205
Asked:
Newton21205
  • 8
  • 3
1 Solution
 
for_yanCommented:
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
 
for_yanCommented:
this will not thow exception:
if(ABC != null && ABC.getccid() == null && ABC.getachflag() == "N"){
//do something
}
0
 
for_yanCommented:
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
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

 
Newton21205Author Commented:
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
 
for_yanCommented:
which line exactly throws null pointer?
did you print stack trace and determine exact line?
0
 
for_yanCommented:
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
 
for_yanCommented:
>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
 
Newton21205Author Commented:
yes i did a try catch around the if it points to the if condition.
0
 
Newton21205Author Commented:
the Java datatype for achflag is String varchar is the database datatype
0
 
for_yanCommented:
post the code for class to which variable ABC belongs
0
 
for_yanCommented:
what code is in the part //do something ?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

  • 8
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now