Runtime classname of null object

Thread Solved
Reply

Join Date: Aug 2008
Posts: 1,158
Reputation: dickersonka will become famous soon enough dickersonka will become famous soon enough 
Solved Threads: 136
dickersonka dickersonka is offline Offline
Veteran Poster

Runtime classname of null object

 
0
  #1
Sep 24th, 2008
I am determining what method to call by using the runtime class. My problem is whenever I pass in a null object, obviously it will throw a null reference exception. How are you able to determine at runtime the class of a null object. Although its null i need to do further processing, and woud rather not pass in type as a parameter to the method.

Here's an example

  1. public void test(){
  2. Long nullLong = null;
  3. addParameter(nullLong);
  4. }
  5.  
  6. public void addParameter(Object value) {
  7. String parameterType = value.getClass().getName();
  8. //do some stuff here with parameterType
  9. }
Last edited by dickersonka; Sep 24th, 2008 at 1:19 pm.
Custom Application & Software Development
www.houseshark.net
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,281
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 243
masijade's Avatar
masijade masijade is online now Online
Nearly a Posting Maven

Re: Runtime classname of null object

 
0
  #2
Sep 24th, 2008
null has no type. It would have a vague type determined by whatever type the argument it was passed in as was suppossed to have. And I say vague, because it would be determinable, because the method already knows it's suppossed to be of that type.

Also, how would propose to get the type (if it had one)? The Class is the type, and since there is no way to dereference null (since it is a non-object) you can neither inspect the "class" field nor call the getClass() method (at least one of which instanceof also uses).
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,346
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 498
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: Runtime classname of null object

 
0
  #3
Sep 24th, 2008
You can't - it's null. It has no value and the only context your method has for a relationship to a class is the parameter type, which you have denoted as Object.

You don't mention the larger context of this usage, but it raises suspicion of a design problem that would be better addressed with an interface for the parameter or behavioral design pattern.

Edit: Posted at the same time as masijade. Answer pertains to the original question.
Last edited by Ezzaral; Sep 24th, 2008 at 1:24 pm.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 1,158
Reputation: dickersonka will become famous soon enough dickersonka will become famous soon enough 
Solved Threads: 136
dickersonka dickersonka is offline Offline
Veteran Poster

Re: Runtime classname of null object

 
0
  #4
Sep 24th, 2008
I am working on a logger showing showing types and values for classes and sql statements (ie parameters in sql statements)

From the example above I will need to show output as something like
Long: null and BIGINT: null

What is a possible recommendation to be able to do this?

Should I create overloaded methods for each type or pass in the class at runtime? Any other suggestions are welcome and thanks for your help!!!
Custom Application & Software Development
www.houseshark.net
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 973
Reputation: Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough 
Solved Threads: 107
Alex Edwards's Avatar
Alex Edwards Alex Edwards is offline Offline
Posting Shark

Re: Runtime classname of null object

 
0
  #5
Sep 24th, 2008
Originally Posted by dickersonka View Post
I am working on a logger showing showing types and values for classes and sql statements (ie parameters in sql statements)

From the example above I will need to show output as something like
Long: null and BIGINT: null

What is a possible recommendation to be able to do this?

Should I create overloaded methods for each type or pass in the class at runtime? Any other suggestions are welcome and thanks for your help!!!
This might help--

  1. public class NullTest{
  2.  
  3. public static void main(String... args){
  4.  
  5. Object obj = null;
  6. System.out.println(determineNull(obj));
  7. obj = new Object();
  8. System.out.println(determineNull(obj));
  9. }
  10.  
  11. private static String determineNull(Object o){
  12. return (o == null) ? "Object is null": "Object is not null";
  13. }
  14. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 1,158
Reputation: dickersonka will become famous soon enough dickersonka will become famous soon enough 
Solved Threads: 136
dickersonka dickersonka is offline Offline
Veteran Poster

Re: Runtime classname of null object

 
0
  #6
Sep 24th, 2008
thanks i decided to use overloaded methods

  1. public void addParameter(Long value) { if(value == null){
  2. //we have a null value
  3. }
  4. else{
  5. //do some stuff here with value
  6. }
Custom Application & Software Development
www.houseshark.net
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC