SEVERE: JavaFX + Vert.x + REST 发生未处理的异常

huangapple go评论66阅读模式
英文:

SEVERE: Unhandled exception with 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: 未处理的异常
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)

这是在"model"包中的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;
	}
	
}

这是在"consumer"包中的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服务,并且工作正常:
使用Postman进行测试

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

感谢您的帮助!

英文:

Situation: I updated my IDE (Eclipse IDE 2020-6 to 2020-9) and the request to my web service stopped working with the following error:

sep. 19, 2020 7:09:23 A. M. io.vertx.core.impl.ContextImpl
SEVERE: Unhandled exception
java.lang.NullPointerException
	at model.Concepto.&lt;init&gt;(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)

This is my line 23 in Concepto.java in package "model":

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);     //&lt;----- Line 23
	}

	public StringProperty getDescripcion() {
		return descripcion;
	}

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

	public IntegerProperty getIdconcepto() {
		return idconcepto;
	}
	
}

and this is line 31 in ConceptoAccess.java in "consumer" package:

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

I tested the web service and works fine:
Tested with Postman

Whats' wrong? everything was working fine before the Eclipse update. I use openJDK 11 + JavaFX 14 + Vert.x 3.9.3

TIA for any help!

答案1

得分: 2

你会收到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(); // 添加这行
    this.descripcion.set(descripcion);
}

public Concepto(String descripcion) {
    descripcion = new SimpleStringProperty(); // 添加这行
    this.descripcion.set(descripcion);
}
英文:

You get a NullPointerException because you are trying to access this.description.set but this.description is null. You are only giving descripcion a value in the default constructor (with zero parameters) and leaving it as null in the others.

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);
}

答案2

得分: 1

或者只需将其上移至

private final StringProperty descripcion = new SimpleStringProperty();

并将其设置为final。删除所有其他此初始化程序的出现。

英文:

Or just move it up to

private final StringProperty descripcion = new SimpleStringProperty();

and make it final. Remove all the other occurences of this initializer.

huangapple
  • 本文由 发表于 2020年9月19日 18:35:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/63967792.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定