mercredi 30 janvier 2019

NullPointerException while using Optional classs [duplicate]

This question already has an answer here:

I am retrieving the record from the database and storing it using Java 8 Optional and I am facing NullPointException Error.

Written code is -

public class PermissionHandler {
    @Autowired
    static PermissionServiceImpl service;
    public static void loopOverFields() {
        PermissionContants permissionContstants = new PermissionContants();
        ReflectionUtils.doWithFields(permissionContstants.getClass(), new FieldCallback() {
            public void doWith(Field field) throws IllegalArgumentException, IllegalAccessException {
                String permisssion = field.get(permissionContstants).toString();
                Optional<TblPermission> permissionObject = service.findByName(permisssion);
            }
        });
    }
}

and the produced output is -

Exception in thread "restartedMain" java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49)
Caused by: java.lang.NullPointerException
    at com.sbs.common.permission.PermissionHandler$1.doWith(PermissionHandler.java:21)
    at org.springframework.util.ReflectionUtils.doWithFields(ReflectionUtils.java:756)
    at org.springframework.util.ReflectionUtils.doWithFields(ReflectionUtils.java:735)
    at com.sbs.common.permission.PermissionHandler.loopOverFields(PermissionHandler.java:18)
    at com.sbs.restServer.SbsApplication.main(SbsApplication.java:26)
    ... 5 more

Please help me to find out what I am doing wrong.





Aucun commentaire:

Enregistrer un commentaire