Solved

how to validate null value

Posted on 2012-03-16
11
206 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
[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
  • 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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 

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

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

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
In this post we will learn different types of Android Layout and some basics of an Android App.
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

691 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