DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   Java (http://www.daniweb.com/forums/forum9.html)
-   -   Runtime classname of null object (http://www.daniweb.com/forums/thread147507.html)

dickersonka Sep 24th, 2008 1:15 pm
Runtime classname of null object
 
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
}

masijade Sep 24th, 2008 1:21 pm
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).

Ezzaral Sep 24th, 2008 1:22 pm
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.

dickersonka Sep 24th, 2008 1:30 pm
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!!!

Alex Edwards Sep 24th, 2008 2:48 pm
Re: Runtime classname of null object
 
Quote:

Originally Posted by dickersonka (Post 698124)
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--

public class NullTest{
       
        public static void main(String... args){
               
                Object obj = null;
                System.out.println(determineNull(obj));
                obj = new Object();
                System.out.println(determineNull(obj));
        }

        private static String determineNull(Object o){
                return (o == null) ? "Object is null": "Object is not null";
        }
}

dickersonka Sep 24th, 2008 3:01 pm
Re: Runtime classname of null object
 
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
}


All times are GMT -4. The time now is 8:39 pm.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC