javafx任务错误:NullPointerException

时间:2015-08-24 09:43:21

标签: java javafx-8

我编写了一个代码来解码base64图像并在javafx中表示该图像。在我的url base64代码不断更改。所以这就是我在javafx代码中使用任务的原因。但是我收到一个错误:java.lang.NullPointerException:Children:子节点为null: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;

}

}

1 个答案:

答案 0 :(得分:1)

好的,根据评论更改行:

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