gpt4 book ai didi

javafx任务错误: NullPointerException

转载 作者:行者123 更新时间:2023-11-30 07:59:21 26 4
gpt4 key购买 nike

我编写了一段代码来解码 Base64 图像并在 javafx 中表示该图像。在我的 url base64 代码中不断变化。这就是我在 javafx 代码中使用任务的原因。但我收到错误:java.lang.NullPointerException:Children:子节点为空:parent = StackPane@583b7e6b

这是我的代码:

Java 文件

public String restService(){

try{
ObjectMapper mapper = new ObjectMapper();
ObjectMapper mapper1 = new ObjectMapper();

//@SuppressWarnings({ "resource", "deprecation" })
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet("");
HttpResponse response = client.execute(request);

BufferedReader reader1 = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
String line = "";
try{
while ((line = reader1.readLine()) != null) {

Status status = mapper.readValue(line, Status.class);
String sessionID = status.getSessionId();

String url = "";
HttpGet request1 = new HttpGet(url);
HttpResponse response1 = client.execute(request1);
BufferedReader reader2 = new BufferedReader(new InputStreamReader(response1
.getEntity().getContent()));

String line1 = "";
while ((line1 = reader2.readLine()) != null) {
Screen status1 = mapper1.readValue(line1, Screen.class);
String value = status1.getValue();
image = value;
}

}
}
catch (IOException e){
System.out.println(e);
}

}

catch (ClientProtocolException e){
System.out.println("Client doesn't response");
}
catch (IOException e){
System.out.println("Can't connect to server2");
}
return image;
}
public static void main(String[] args) throws ClientProtocolException,IOException, FileNotFoundException {
RestCall test = new RestCall();
String imageString = test.restService();
System.out.println(imageString);




}

javafx 文件:

@Override
public void start(Stage primaryStage) {
try {
final Task<Void> task;
task = new Task<Void>(){
@Override
protected Void call() throws Exception{
while(true){
try{
RestCall rest = new RestCall();
String base_64 = rest.restService();
ByteArrayInputStream imageStream = decodeImage(base_64);
final Image screenImg = new Image(imageStream);

Platform.runLater(new Runnable(){
public void run() {

imgView.setImage(screenImg);
imgView.setFitWidth(468);
imgView.setFitHeight(620);
StackPane sp = new StackPane();
sp.getChildren().add(imgView);

}
});

Thread.sleep(2);
}
catch (Exception e){
System.out.println("Image Not Found");
}

}


}
};


StackPane sp = new StackPane();
sp.getChildren().add(imgView);

Scene scene = new Scene(sp);
primaryStage.setScene(scene);
primaryStage.show();

new Thread(task).start();




} catch(Exception e) {
System.out.println(e);
}
}

/*public void show(){
launch();
}*/

public static void main(String[] args) {
/*Main main = new Main();
main.show();*/
launch(args);
}



public static ByteArrayInputStream decodeImage(String str){

Base64 base64 = new Base64();
ByteArrayInputStream screenInputStream = new ByteArrayInputStream(base64.decode(str));

return screenInputStream;

}

}

最佳答案

好的,根据评论更改行:

 sp.getChildren().add(imgView);

至:

if(imgView == null){
imgView = new imgView();
}

sp.getChildren().add(imgView);

你还需要改变

 Platform.runLater(new Runnable(){
public void run() {

imgView.setImage(screenImg);
imgView.setFitWidth(468);
imgView.setFitHeight(620);
StackPane sp = new StackPane();
sp.getChildren().add(imgView);
}
});

至:

Platform.runLater(new Runnable(){
public void run() {

if(imgView == null)
imgView = new imgView();
imgView.setImage(screenImg);
imgView.setFitWidth(468);
imgView.setFitHeight(620);
StackPane sp = new StackPane();
sp.getChildren().add(imgView);
}
});

关于javafx任务错误: NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32179108/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com