0

I intend to make a common dynamic null check function on any object before doing some work on it.

For example:

Class A{ B b; // with getters and setters of b } class B{ C c; //with getters and setters of c } class C{ BigInteger d; //with getters and setters of d } 

now, I want to check whether objA.getB().getC().getD() returns some value or throws NullPointerException?

By common I mean I can pass any kind of object to it, something like below function

CheckNull.checkingDynamicNull( "objA.getB().getC().getD()" ) 

will return me true or false depending on the case.

Any ideas?

6
  • objA.getB().getC().getD() == null Commented Feb 15, 2013 at 9:43
  • 1
    @GrijeshChauhan.. And what if getB() returned a null? Commented Feb 15, 2013 at 9:43
  • that is what i want to handle by returning false through that function.... Commented Feb 15, 2013 at 9:45
  • Any kind of dynamic null-check using (getter) methods may fail any time the methods have side-effects. So you probably come best off when you check each method individually. Using reflection and public fields this check may be possible. Commented Feb 15, 2013 at 9:50
  • It doesn't sound like a very good approach. Surely you care which one is null? Won't that affect how you handle it being null? ie. if flight.family.person.baggage is null, that's probably fine - this person just doesn't have any baggage. But if flight.family.person is null, that means a family hasn't got any people - which is a serious error. Commented Feb 15, 2013 at 9:50

6 Answers 6

1

Unfortunately it is not possible to pass a funktion into a method in Java (yet, Java 8 will). Also, if you pass the variable name as String this wont work, since the recieving method has no way of knowing what Object is mapped to that variable (if you ignore reflection but even with that you are screwed if its a local variable).

Another way would be to do something like this

boolean nullCheck(Object obj, String Methods) 

and then parse the String to Methods and invoke them on the object. But that wont work if you have Methods with arguments.

In short: It's more trouble then it's worth

In the most cases you want to have something like this

if(object == null){ //error behavior } 

This assumes that you want to throw a certain Exception, have means to get a value for the null object elswhere or want to do something before you throw an exception. If you simply want to throw an Exception (that is not a NullPointerException)

assert object != null 
Sign up to request clarification or add additional context in comments.

2 Comments

+1 for "In short: It's more trouble then it's worth"
this will probably solve my problem as I don't intend to pass any arguments in the methods as mostly they are normal getters.
0

What about this:

public boolean checkingDynamicNull(A objA) { try { objA.getB().getC().getD().toString(); } catch(NullPointerException npe) { return true; } return false; } 

3 Comments

before calling getB() cast objA to A.
I thought of that but that wont be a common function.... isn't it...
true, but I dont see much sense in using this method for a common Util . You should handle null in the special case. This Method doesn't handle null gracefully.
0

To know if a statement would return null, you would have to execute it first. Even if you somehow manage to execute the statement given in String to your method, it would be no different than just running it and checking whether the result is null, or catching the NullPointerException.

1 Comment

AFAIK we can attack on the problem through reflection methods....but I have no hands on it....
0

You are looking for something that will be part of Java 8 (or 9 or 10?) soon. Cf. http://viralpatel.net/blogs/null-safe-type-java-7-nullpointerexception/

Comments

0

With reflection it could be something like that:

nullCheck(objA, "getB", "getC", "getD" ); 

public static boolean nullCheck(Object obj, String... methods) { Object o = obj; Class<?> clazz = obj.getClass(); Method method = null; try { for( String name : methods ) { method = clazz.getMethod( name, null ); o = method.invoke( o, null ); clazz = method.getReturnType(); } } catch( NullPointerException e ) { System.err.println(clazz.getSimpleName()+"(null)."+method.getName()); return false; } catch( NoSuchMethodException e ) { e.printStackTrace(); } catch( SecurityException e ) { e.printStackTrace(); } catch( IllegalAccessException e ) { e.printStackTrace(); } catch( IllegalArgumentException e ) { e.printStackTrace(); } catch( InvocationTargetException e ) { e.printStackTrace(); } return true; } 

System.out.println(nullCheck(GraphicsEnvironment.getLocalGraphicsEnvironment(), "getDefaultScreenDevice", "getDisplayMode", "toString")); System.out.println(nullCheck(GraphicsEnvironment.getLocalGraphicsEnvironment(), "getDefaultScreenDevice", "getFullScreenWindow", "doLayout")); 

brings

true false 'Window(null).doLayout' 

Comments

0

you can achieve this by using reflection, here is your method:

public boolean dynamicNullCheck(Object o, String path) throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException { Object object= o; for (String element : path.split("\\.")) { Field field = object.getClass().getDeclaredField(element); field.setAccessible(true); Object fieldVal = field.get(object); if (fieldVal!=null) { field.setAccessible(true); object = fieldVal; } else { return true; } } return false; } 

use it by giving your root element and path to each object, ie dynamicNullCheck(objA,"b.c.d")

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.