![]() |
| ||
| 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(){ |
| ||
| 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). |
| ||
| 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. |
| ||
| 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!!! |
| ||
| Re: Runtime classname of null object Quote:
public class NullTest{ |
| ||
| Re: Runtime classname of null object thanks i decided to use overloaded methods public void addParameter(Long value) { if(value == null){ |
| All times are GMT -4. The time now is 10:32 am. |
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC