[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

how to validate null value

Posted on 2012-03-16
11
Medium Priority
?
209 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
Suggested Courses

649 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