2

I wrote a code to decode base64 image and represent that image in javafx. In my url base64 code continuously change.So that's why I used task in my javafx code. But I get an error : java.lang.NullPointerException: Children: child node is null: parent = StackPane@583b7e6b

this is my code:

Java File

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 file:

@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;

}

}

7
  • which line throws the NullPointerException? Commented Aug 24, 2015 at 9:43
  • sp.getChildren().add(imgView); that line Commented Aug 24, 2015 at 9:47
  • so where do you initialize your imgView? It looks like, that imgView is null at this moment... Commented Aug 24, 2015 at 9:49
  • no I initialized that as private ImageView imgView; Commented Aug 24, 2015 at 9:51
  • When you just made ImageView imgView, than your imgView = null. You need to create a new one Commented Aug 24, 2015 at 9:52

1 Answer 1

2

Okay according to the comments change the line:

 sp.getChildren().add(imgView);

to:

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

sp.getChildren().add(imgView);

You also need to change

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

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

to:

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);
     }
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.