RSS Forums RSS

Runtime classname of null object

Please support our Java advertiser: Programming Forums
Thread Solved
Reply
Posts: 1,157
Reputation: dickersonka will become famous soon enough dickersonka will become famous soon enough 
Solved Threads: 133
dickersonka dickersonka is offline Offline
Veteran Poster

Runtime classname of null object

  #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

public void test(){
   Long nullLong = null;
   addParameter(nullLong);
}

public void addParameter(Object value) {		
     String parameterType = value.getClass().getName();
//do some stuff here with parameterType
}
Last edited by dickersonka : Sep 24th, 2008 at 12:19 pm.
Custom Application & Software Development
www.houseshark.net
AddThis Social Bookmark Button
Reply With Quote  
Posts: 1,951
Reputation: masijade is a splendid one to behold masijade is a splendid one to behold masijade is a splendid one to behold masijade is a splendid one to behold masijade is a splendid one to behold masijade is a splendid one to behold masijade is a splendid one to behold 
Solved Threads: 198
masijade's Avatar
masijade masijade is offline Offline
Posting Virtuoso

Re: Runtime classname of null object

  #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  
Posts: 4,111
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: 458
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: Runtime classname of null object

  #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 12:24 pm.
Reply With Quote  
Posts: 1,157
Reputation: dickersonka will become famous soon enough dickersonka will become famous soon enough 
Solved Threads: 133
dickersonka dickersonka is offline Offline
Veteran Poster

Re: Runtime classname of null object

  #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  
Posts: 947
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: 106
Alex Edwards's Avatar
Alex Edwards Alex Edwards is offline Offline
Posting Shark

Re: Runtime classname of null object

  #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  
Posts: 1,157
Reputation: dickersonka will become famous soon enough dickersonka will become famous soon enough 
Solved Threads: 133
dickersonka dickersonka is offline Offline
Veteran Poster

Re: Runtime classname of null object

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

public void addParameter(Long value) {	if(value == null){
//we have a null value
}
else{
//do some stuff here with value
}
Custom Application & Software Development
www.houseshark.net
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.



Other Threads in the Java Forum
Views: 812 | Replies: 5 | Currently Viewing: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 2:21 pm.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC