java.lang.NullPointerException

Spring JIRA | Ben Madore | 1 year ago
  1. 0

    I have a project like: {code:java} @Entity public class Store { @Id public Long id; @OneToMany @JsonIgnore public Set<Hours> hours; @OneToMany @JsonIgnore public Set<HourOverrides> overrides; public TodaysHours getTodaysHoursComputedValue() { TodayHours todaysHours = //computation based on hours and overrides return todaysHours; } public setTodaysHoursComputedValue() { //Do nothing, just needed so that jackson has a corresponding setter to call, but value is discarded } {code} I have a separate manual controller that handles {code}/stores/{id}/hours{code} and lets your CRUD against a DTO containing both hours and overrides - so I *do not* want to expose those values directly on the Store, but i do want to expose a read-only version of the hours. In DomainObjectReader there is the call to: {code:java}final MappedProperties properties = getJacksonProperties(entity, mapper);{code} which returns all the properties jackson knows about. Then for each of those it calls: {code:java}PersistentProperty<?> persistentProperty = entity.getPersistentProperty(property.getInternalName());{code} which in the case of this, returns null as this is a COMPUTED value and doesn't map to a persisted entity. The problem is that even though there IS no persistent property it adds the null to a couple of {code:java}propertyToFieldName.put(persistentProperty, property.getName()); fieldNameToProperty.put(property.getName(), persistentProperty);{code} which are later dereferenced and leads to an NPE. {code}Caused by: java.lang.NullPointerException at org.springframework.data.mapping.model.BeanWrapper.getProperty(BeanWrapper.java:96) ~[spring-data-commons-1.11.0.M1.jar:?] at org.springframework.data.rest.webmvc.json.DomainObjectReader.doMerge(DomainObjectReader.java:185) ~[spring-data-rest-webmvc-2.4.0.M1.jar:?] at org.springframework.data.rest.webmvc.json.DomainObjectReader.merge(DomainObjectReader.java:136) ~[spring-data-rest-webmvc-2.4.0.M1.jar:?] at org.springframework.data.rest.webmvc.json.DomainObjectReader.readPut(DomainObjectReader.java:130) ~[spring-data-rest-webmvc-2.4.0.M1.jar:?] at org.springframework.data.rest.webmvc.config.JsonPatchHandler.applyPut(JsonPatchHandler.java:134) ~[spring-data-rest-webmvc-2.4.0.M1.jar:?]{code} I believe the fix would be on {{DomainObjectReader.java:237}} to add a null check before adding the nulls to those maps. It seems like you should be able to have computed values in your response and to simply ignore them on the deserialization if the client happens to pass them. I know it's a bit of a hack in jackson to have a getter with an empty setter for computed properties - but this is getting full support in jackson 2.6 (https://github.com/FasterXML/jackson-databind/issues/95) - and i believe it's a valid use case. Please let me know if you disagree, or if you know of any work arounds.

    Spring JIRA | 1 year ago | Ben Madore
    java.lang.NullPointerException
  2. 0

    I have a project like: {code:java} @Entity public class Store { @Id public Long id; @OneToMany @JsonIgnore public Set<Hours> hours; @OneToMany @JsonIgnore public Set<HourOverrides> overrides; public TodaysHours getTodaysHoursComputedValue() { TodayHours todaysHours = //computation based on hours and overrides return todaysHours; } public setTodaysHoursComputedValue() { //Do nothing, just needed so that jackson has a corresponding setter to call, but value is discarded } {code} I have a separate manual controller that handles {code}/stores/{id}/hours{code} and lets your CRUD against a DTO containing both hours and overrides - so I *do not* want to expose those values directly on the Store, but i do want to expose a read-only version of the hours. In DomainObjectReader there is the call to: {code:java}final MappedProperties properties = getJacksonProperties(entity, mapper);{code} which returns all the properties jackson knows about. Then for each of those it calls: {code:java}PersistentProperty<?> persistentProperty = entity.getPersistentProperty(property.getInternalName());{code} which in the case of this, returns null as this is a COMPUTED value and doesn't map to a persisted entity. The problem is that even though there IS no persistent property it adds the null to a couple of {code:java}propertyToFieldName.put(persistentProperty, property.getName()); fieldNameToProperty.put(property.getName(), persistentProperty);{code} which are later dereferenced and leads to an NPE. {code}Caused by: java.lang.NullPointerException at org.springframework.data.mapping.model.BeanWrapper.getProperty(BeanWrapper.java:96) ~[spring-data-commons-1.11.0.M1.jar:?] at org.springframework.data.rest.webmvc.json.DomainObjectReader.doMerge(DomainObjectReader.java:185) ~[spring-data-rest-webmvc-2.4.0.M1.jar:?] at org.springframework.data.rest.webmvc.json.DomainObjectReader.merge(DomainObjectReader.java:136) ~[spring-data-rest-webmvc-2.4.0.M1.jar:?] at org.springframework.data.rest.webmvc.json.DomainObjectReader.readPut(DomainObjectReader.java:130) ~[spring-data-rest-webmvc-2.4.0.M1.jar:?] at org.springframework.data.rest.webmvc.config.JsonPatchHandler.applyPut(JsonPatchHandler.java:134) ~[spring-data-rest-webmvc-2.4.0.M1.jar:?]{code} I believe the fix would be on {{DomainObjectReader.java:237}} to add a null check before adding the nulls to those maps. It seems like you should be able to have computed values in your response and to simply ignore them on the deserialization if the client happens to pass them. I know it's a bit of a hack in jackson to have a getter with an empty setter for computed properties - but this is getting full support in jackson 2.6 (https://github.com/FasterXML/jackson-databind/issues/95) - and i believe it's a valid use case. Please let me know if you disagree, or if you know of any work arounds.

    Spring JIRA | 1 year ago | Ben Madore
    java.lang.NullPointerException
  3. 0

    spring-boot, spring security, spring web with maven, jboss and eclipse

    codegur.net | 8 months ago
    org.springframework.http.converter.HttpMessageNotReadableException: Could not read an object of type class example.Item from the request!; nested exception is org.springframework.http.converter.HttpMessageNotReadableException: Could not read payload!; nested exception is java.lang.NullPointerException
  4. Speed up your debug routine!

    Automated exception search integrated into your IDE

  5. 0

    NPE when using PATCH/PUT with @Transient Map

    Stack Overflow | 7 months ago | Ickbinet
    java.lang.NullPointerException: null
  6. 0

    spring-boot, spring security, spring web with maven, jboss and eclipse

    sc0713.com | 3 months ago
    org.springframework.http.converter.HttpMessageNotReadableException: Could not read an object of type class example.Item from the request!; nested exception is org.springframework.http.converter.HttpMessageNotReadableException: Could not read payload!; nested exception is java.lang.NullPointerException

    1 unregistered visitors
    Not finding the right solution?
    Take a tour to get the most out of Samebug.

    Tired of useless tips?

    Automated exception search integrated into your IDE

    Root Cause Analysis

    1. java.lang.NullPointerException

      No message provided

      at org.springframework.data.mapping.model.BeanWrapper.getProperty()
    2. Spring Data Core
      BeanWrapper.getProperty
      1. org.springframework.data.mapping.model.BeanWrapper.getProperty(BeanWrapper.java:96)[spring-data-commons-1.11.0.M1.jar:?]
      1 frame
    3. Spring Data REST - WebMVC
      DomainObjectReader.readPut
      1. org.springframework.data.rest.webmvc.json.DomainObjectReader.doMerge(DomainObjectReader.java:185)[spring-data-rest-webmvc-2.4.0.M1.jar:?]
      2. org.springframework.data.rest.webmvc.json.DomainObjectReader.merge(DomainObjectReader.java:136)[spring-data-rest-webmvc-2.4.0.M1.jar:?]
      3. org.springframework.data.rest.webmvc.json.DomainObjectReader.readPut(DomainObjectReader.java:130)[spring-data-rest-webmvc-2.4.0.M1.jar:?]
      3 frames