943,548 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 2322
  • Java RSS
Sep 24th, 2008
0

Runtime classname of null object

Expand Post »
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

Java Syntax (Toggle Plain Text)
  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.
Reputation Points: 133
Solved Threads: 141
Veteran Poster
dickersonka is offline Offline
1,162 posts
since Aug 2008
Sep 24th, 2008
0

Re: Runtime classname of null object

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).
Moderator
Reputation Points: 1471
Solved Threads: 490
Industrious Poster
masijade is offline Offline
4,043 posts
since Feb 2006
Sep 24th, 2008
0

Re: Runtime classname of null object

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.
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 838
Posting Genius
Ezzaral is offline Offline
6,756 posts
since May 2007
Sep 24th, 2008
0

Re: Runtime classname of null object

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!!!
Reputation Points: 133
Solved Threads: 141
Veteran Poster
dickersonka is offline Offline
1,162 posts
since Aug 2008
Sep 24th, 2008
0

Re: Runtime classname of null object

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--

java Syntax (Toggle Plain Text)
  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. }
Reputation Points: 392
Solved Threads: 108
Posting Shark
Alex Edwards is offline Offline
971 posts
since Jun 2008
Sep 24th, 2008
0

Re: Runtime classname of null object

thanks i decided to use overloaded methods

Java Syntax (Toggle Plain Text)
  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. }
Reputation Points: 133
Solved Threads: 141
Veteran Poster
dickersonka is offline Offline
1,162 posts
since Aug 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Simple Question
Next Thread in Java Forum Timeline: Tokenize





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC