Solved

how to validate null value

Posted on 2012-03-16
11
201 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
 

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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
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…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

762 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now