0

Estoy tratando te convertir un Json,que tiene un atributo como array, a un objeto usando Gson, pero siempre me indica que el atributo del array no se creo ( es null) en el objeto. Mi codigo de test es

def "Crear un objeto Ghi desde un JSONElement manual" () {
    given:
    Gson gson = new Gson()

    when:
    String json ='{ "forecasts" : [ {"ghi":786,"ghi90":930,"ghi10":561,"period_end":"2021-01-08T17:30:00.0000000Z", "period":"PT30M"}, {"ghi":638,"ghi90":1061,"ghi10":52,"period_end":"2021-01-15T16:00:00.0000000Z", "period":"PT30M"}, {"ghi":623,"ghi90":1062,"ghi10":52,"period_end":"2021-01-15T16:30:00.0000000Z", "period":"PT30M"}, {"ghi":600,"ghi90":1047,"ghi10":52,"period_end":"2021-01-15T17:00:00.0000000Z", "period":"PT30M"}]}'

     ForecastGhi forecastGhi = gson.fromJson(json, ForecastGhi.class);
        
then:

       
        forecastGhi.getForecast().get(1).getGhi() == 638
    }

Pero me arroja el siguiente error:

Condition failed with Exception:

System.out.println(forecastGhi.getForecast().get(1).getGhi())
       |           |           |             |
       |           |           null          java.lang.NullPointerException: Cannot invoke method get() on null object
       |           webServices.solcastWorldApi.ForecastGhi@44251d30
       org.gradle.internal.io.LinePerThreadBufferingOutputStream@64177e23

Condition failed with Exception:

System.out.println(forecastGhi.getForecast().get(1).getGhi())
       |           |           |             |
       |           |           null          java.lang.NullPointerException: Cannot invoke method get() on null object
       |           webServices.solcastWorldApi.ForecastGhi@44251d30
       org.gradle.internal.io.LinePerThreadBufferingOutputStream@64177e23

    at ikebanaerp.v4.Convertions.JsonToGhiHorizontalClassTest.Crear un objeto Ghi desde un JSONElement manual(JsonToGhiHorizontalClassTest.groovy:116)
Caused by: java.lang.NullPointerException: Cannot invoke method get() on null object
    ... 1 more

Mi clase contenedora que contiene el array es

class ForecastGhi {

    static constraints = {
    }

    private ArrayList<IrradianciaGlobalHorizontal> forecast


    

    ArrayList<IrradianciaGlobalHorizontal> getForecast() {
        return forecast
    }

    void setForecast(ArrayList<IrradianciaGlobalHorizontal> forecast) {
        this.forecast = forecast
    }
}

y la clase de modelo de dominio es :

class IrradianciaGlobalHorizontal {

    static constraints = {
    }
    @JsonProperty("all")  // Todas son propiedades Json

    private def period_end
    private def period
    private def ghi90
    private def ghi
    private def ghi10

    def getGhi() {
         this.ghi
    }

    void setGhi(int ghi) {
        this.ghi = ghi
    }

    def getGhi90() {
        this.ghi90
    }

    void setGhi90(int ghi90) {
        this.ghi90 = ghi90
    }

    def getGhi10() {
        this.ghi10
    }

    void setGhi10(int ghi10) {
        this.ghi10 = ghi10
    }

    def getPeriod_end() {
         this.period_end
    }

    void setPeriod_end(Date period_end) {
        this.period_end = period_end
    }

    def getPeriod() {
        this.period
    }

    void setPeriod(String period) {
        this.period = period
    }
}

Una ayuda por favor , gracias

2
  • Hola, que librería de test estas usando? el 12 ene. 2021 a las 19:10
  • Spock de grails , y el gson es el 2.8.0 el 12 ene. 2021 a las 23:41

0

Tu Respuesta

Al pulsar en “Publica tu respuesta”, muestras tu consentimiento a nuestros términos de servicio, política de privacidad y política de cookies

Examina otras preguntas con la etiqueta o formula tu propia pregunta.