首页 > 解决方案 > PipedOutputStream.write() InvocationTargetException

问题描述

所以我正在尝试使用下面的 WritetoStream() 方法写入 PipedOutputStream。但是,当我尝试这样做时,我“hey3”永远不会打印出来,因为以下代码行永远不会运行:

pseudoUserInput.write(ClusterInformation.getBytes());

关于为什么线程无法写入 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);
    }
}

在哪里 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>

运行 Main.java 时的堆栈跟踪是:

> 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

标签: javajavafxinputstreamoutputstream

解决方案


推荐阅读