Reputation: 23
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;
}
}
Upvotes: 2
Views: 8353
Reputation: 1638
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);
}
});
Upvotes: 2