So I am trying to write to a PipedOutputStream using the WritetoStream() method below. However, when I attempt to do this I "hey3" never prints out due to the following line of code never runs:
pseudoUserInput.write(ClusterInformation.getBytes());
Any idea as to why the thread is unable to write to pseudoUserInput?
package sample;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
import java.io.*;
import java.util.Scanner;
public class Main extends Application {
private static final PipedOutputStream pseudoUserInput = SystemInputChange();
@Override
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
PRun("q");
primaryStage.setTitle("Hello World");
primaryStage.setScene(new Scene(root, 300, 275));
primaryStage.show();
}
public static void PRun(String clusterId){
String CI;
CI = clusterId;
System.out.println("hey2");
try{WritetoSystemInput(CI);}
catch (IOException ex) {
System.err.println("Error running NetworkAnaylsis: " + ex.getMessage());
ex.printStackTrace();
System.exit(1);}
System.out.println("hey3");
System.out.println("Note that all windows must be closed before the program competely terminates.");
}
private static PipedOutputStream SystemInputChange(){
try {
PipedOutputStream pseudoUserInput = new PipedOutputStream();
System.setIn(new PipedInputStream(pseudoUserInput));
}
catch (IOException e){
throw new RuntimeException(e);
}
return pseudoUserInput;
}
private static void WritetoSystemInput(String ClusterInformation) throws IOException{
pseudoUserInput.write(ClusterInformation.getBytes());
System.out.println(ClusterInformation.getBytes());
}
public static void main(String[] args) {
launch(args);
}
}
Where sample.fxml
<?import javafx.geometry.Insets?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<GridPane fx:controller="sample.Controller"
xmlns:fx="http://javafx.com/fxml" alignment="center" hgap="10" vgap="10">
</GridPane>
The Stack Trace when running Main.java is:
> hey2
> [B@1dd5b802
> Exception in Application start method
> java.lang.reflect.InvocationTargetException at
> java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native
> Method) at
> java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
> at
> java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
> at java.base/java.lang.reflect.Method.invoke(Method.java:564) at
> javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:464)
> at
> javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:363)
> at
> java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native
> Method) at
> java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
> at
> java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
> at java.base/java.lang.reflect.Method.invoke(Method.java:564) at
> java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1051)
> Caused by: java.lang.RuntimeException: Exception in Application start
> method at
> javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:900)
> at
> javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:195)
> at java.base/java.lang.Thread.run(Thread.java:832) Caused by:
> java.lang.NullPointerException at
> sample.Main.WritetoSystemInput(Main.java:52) at
> sample.Main.PRun(Main.java:30) at sample.Main.start(Main.java:17) at
> javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:846)
> at
> javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:455)
> at
> javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428)
> at
> java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
> at
> javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:427)
> at
> javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
> at
> javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native
> Method) at
> javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174)
> ... 1 more Exception running application sample.Main