| | |
Runtime classname of null object
Thread Solved |
•
•
Join Date: Aug 2008
Posts: 1,160
Reputation:
Solved Threads: 137
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
Here's an example
Java Syntax (Toggle Plain Text)
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 1:19 pm.
Custom Application & Software Development
www.houseshark.net
www.houseshark.net
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).
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
----------------------------------------------
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
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.
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.
•
•
Join Date: Aug 2008
Posts: 1,160
Reputation:
Solved Threads: 137
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!!!
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
www.houseshark.net
•
•
•
•
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!!!
java Syntax (Toggle Plain Text)
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"; } }
•
•
Join Date: Aug 2008
Posts: 1,160
Reputation:
Solved Threads: 137
thanks i decided to use overloaded methods
Java Syntax (Toggle Plain Text)
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
www.houseshark.net
![]() |
Other Threads in the Java Forum
- Previous Thread: Simple Question
- Next Thread: Tokenize
| Thread Tools | Search this Thread |
android api applet application apps array arrays automation awt bidirectional binary birt bluetooth businessintelligence busy_handler(null) card class classes client code collision columns component constructor crashcourse database designadrawingapplicationusingjavajslider draw eclipse error errors eventlistener exception expand fractal game givemetehcodez graphics gui guidancer html ide image inetaddress integer intellij j2me java javadoc javafx javamicroeditionuseofmotionsensor javaprojects jme jni jpanel jtree julia linux list loop machine map method methods mobile mobiledevelopmentcreatejar myaggfun netbeans newbie oracle physics plazmic print problem program project radio recursion scanner server set sharepoint smart sms smsspam sort sortedmaps sql string subclass support swing textfield threads tree trolltech unlimited utility webservices windows






