严重:JavaFX + Vert.x + REST的未处理异常

问题描述

情况:我更新了我的IDE(Eclipse IDE 2020-6至2020-9),并且对我的Web服务的请求因以下错误而停止工作:

sep. 19,2020 7:09:23 A. M. io.vertx.core.impl.ContextImpl
SEVERE: Unhandled exception
java.lang.NullPointerException
    at model.Concepto.<init>(Concepto.java:23)
    at consumer.ConceptoAccess.lambda$getConceptos$0(ConceptoAccess.java:31)
    at java.base/java.lang.Iterable.forEach(Iterable.java:75)
    at consumer.ConceptoAccess.lambda$getConceptos$1(ConceptoAccess.java:28)
    at io.vertx.ext.web.client.impl.HttpContext.handledispatchResponse(HttpContext.java:313)
    at io.vertx.ext.web.client.impl.HttpContext.execute(HttpContext.java:300)
    at io.vertx.ext.web.client.impl.HttpContext.next(HttpContext.java:275)
    at io.vertx.ext.web.client.impl.predicate.PredicateInterceptor.handle(PredicateInterceptor.java:69)
    at io.vertx.ext.web.client.impl.predicate.PredicateInterceptor.handle(PredicateInterceptor.java:32)

这是我在Concepto.java中的 第23行 ,位于“模型”包中:

package model;

import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;

public class Concepto {
    
    private IntegerProperty idconcepto;
    private StringProperty descripcion;
    
    public Concepto() {
//      idconcepto = new SimpleIntegerproperty();
        descripcion = new SimpleStringproperty();
    }

    public Concepto(Integer idconcepto,String descripcion) {
//      this.idconcepto.set(idconcepto);
        this.descripcion.set(descripcion);
    }
    
    public Concepto(String descripcion) {
        this.descripcion.set(descripcion);     //<----- Line 23
    }

    public StringProperty getDescripcion() {
        return descripcion;
    }

    public void setDescripcion(String descripcion) {
        this.descripcion.set(descripcion);
    }

    public IntegerProperty getIdconcepto() {
        return idconcepto;
    }
    
}

这是ConceptoAccess.java中“消费者”包中的 第31行

public class ConceptoAccess {
    
    private static final String HOST = "192.168.0.15";
    private static final int PORT = 8091;
    
    public static void getConceptos(ObservableList<Concepto> conceptoData) {
        WebClient client = WebClient.create(Vertx.vertx());
        client
        .get(PORT,HOST,"/api/conceptos")
        .send(ar -> {
            if (ar.succeeded()) {
                conceptoData.clear();
                HttpResponse<Buffer> response = ar.result();
                response.bodyAsJsonArray().forEach(concepto -> {
                    JsonObject jo = (JsonObject) concepto;
                    conceptoData.add(new Concepto(jo.getString("descripcion")));     <---Line 31
                });
                System.out.println("Received response with status code " + response.statusCode());
                System.out.println(response.bodyAsJsonArray());
            } else {
                System.out.println("Something went wrong " + ar.cause().getMessage());
            }
        });
    }

我测试了Web服务并且运行良好: Tested with Postman

怎么了?在Eclipse更新之前,一切工作正常。我使用openJDK 11 + JavaFX 14 + Vert.x 3.9.3

TIA寻求帮助!

解决方法

您得到一个 NullPointerException ,因为您尝试访问this.description.set,但是this.description null 。您仅在默认构造函数中为descripcion提供了一个值(参数为零),而在其他构造函数中将其保留为 null

private StringProperty descripcion;

public Concepto() {
    descripcion = new SimpleStringProperty();
}

public Concepto(Integer idconcepto,String descripcion) {
    descripcion = new SimpleStringProperty(); // add this
    this.descripcion.set(descripcion);
}

public Concepto(String descripcion) {
    descripcion = new SimpleStringProperty(); // add this
    this.descripcion.set(descripcion);
}
,

或者将其向上移动

private final StringProperty descripcion = new SimpleStringProperty();

并使其最终。删除该初始化程序的所有其他出现。

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...