| | |
Runtime classname of null object
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Aug 2008
Posts: 1,162
Reputation:
Solved Threads: 138
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,162
Reputation:
Solved Threads: 138
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,162
Reputation:
Solved Threads: 138
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 |
Tag cloud for Java
addressbook android api apple applet application arguments array arrays automation binary bluetooth button calculator chat class classes client code columns component converter database draw eclipse error errors event exception file fractal ftp game givemetehcodez graphics gridlayout gui helpwithhomework html ide image inetaddress input integer j2me japplet java javaprojects jme jmf jni jpanel julia link linux list loop map method methods midlethttpconnection mobile netbeans newbie number objects openjavafx oracle php print problem program programming project projects recursion rim scanner screen server set signing size smart sms socket sort sql storm string support swing test threads time tree unlimited variablebinding webservices windows






