java - 在 Java 中,尝试写入命名管道时出现空指针异常

标签 java linux nullpointerexception

我是 Java 新手,我不明白如何写入命名管道。我想让我的 Java 从一个管道读取数据,进行一些转换,然后写入另一个管道。

这是在 Linux 上。我毫无问题地设置了管道,并且没有出现有关管道的错误。

很长一段时间我都在这一行遇到空指针异常:

writePipe.write(m);

我使用 FileOutputStream 作为“writePipe”。但现在我已经改用PrintWriter了。

我正在使用 2 个终端窗口来测试我的代码。我在一个终端中运行我的应用程序,然后在另一个终端中运行此消息到管道:

echo "Ankanth, expect messages like this!" > api-sends-messages-to-nlp

直到 15 分钟前,这都会被正确读取,但是当我的应用程序尝试写回消息时,我收到了空指针异常。现在,直到我从管道中读取数据时,才出现空指针异常:

cat nlp-sends-messages-to-api

此时我看到:

We just received this message: 
Ank, expect messages like this!
We got a message in the main loop()!
Now we will write to the pipe.
error in writeMessage java.lang.NullPointerException

我的“Api”类处理管道的读取和写入:

public class Api {

// Connect to the named pipe                                                                                                                             
public BufferedReader readPipe = null;
public PrintWriter writePipe = null;


Api() {
    try {
        this.readPipe = new BufferedReader(new FileReader("/home/rollio/api-sends-messages-to-nlp"));
        this.writePipe = new PrintWriter((new BufferedWriter(new FileWriter("/home/rollio/nlp-sends-messages-to-api"))));
    }
    catch (Exception e) {
        System.out.println("error in Api constructor " + e);
    }
}


public String readMessage() throws Exception {
    try {
        // Read response from pipe                                                                                                                       
        String message = readPipe.readLine();
        System.out.println("We just received this message: ");
        System.out.println(message);
        return message;
    }
    catch (Exception e) {
        System.out.println("error in readMessage " + e);
        throw new Exception("error in readMessage " + e);
    }
}

    public void writeMessage(String message) throws Exception {
    try {
        // Write request to the pipe                                                                                                                     
        String m = message.concat(" ...said the awesome NLP app!!!! Hell yeah!!!!!");
        System.out.println("Now we will write to the pipe.");
        if (m==null)
            m="";
        writePipe.println("This next line is the message: ");
        writePipe.println(m);
    }
    catch (Exception e) {
        System.out.println("error in writeMessage " + e);
        throw new Exception("error in writeMessage " + e);
    }
}

上面的代码在 main 中开始的 while 循环()中永远运行:

   public static void main(String...args){
        try {
            Api rw = new Api();
            while (true) {
                String message = rw.readMessage();
                // something magic happens and Ankanth transforms the message                                                                           
                System.out.println("We got a message in the main loop()!");
                rw.writeMessage(message);
    }

                }

    catch (Exception e) {
            System.out.println(e);
        }
    }
    }

看来这是抛出空指针异常的行:

writePipe.println(m);

管道的路径是正确的,那么问题是什么?

更新:

好吧,如果我像这样改变读取方法:

public String readMessage() throws Exception {
    try {
        // Read response from pipe                                                                                                                       
        String message = readPipe.readLine();
        System.out.println("We just received this message: ");
        System.out.println(message);
        return message;
    }

这 2 个 println 给了我:

We just received this message: 
null

但是为什么消息会为空呢?我从终端写入管道,如下所示:

echo "expect messages like this!" > api-sends-messages-to-nlp

应用程序“听到”一些东西,因为这一行返回一些东西:

readPipe.readLine();

但是为什么它返回“null”?为什么它不返回写入管道的内容?

再次更新:

[编辑]

因此,从终端,我使用“echo”对应用程序进行两次写入,然后使用“cat”从中读取:

echo“Ankanth,期待这样的消息!” > API 发送消息至 NLP

echo“Ankanth,期待这样的消息!” > API 发送消息至 NLP

cat nlp-sends-messages-to-api

我得到:

We just received this message: 
Ankanth, expect messages like this!
We got a message in the main loop()!
Now we will write to the pipe.

We just received this message: 
Ankanth, expect messages like this!
We got a message in the main loop()!
Now we will write to the pipe.

We just received this message: 
null
We got a message in the main loop()!
error in writeMessage java.lang.NullPointerException
java.lang.NullPointerException
    at com.rollioapp.nlp.Api.writeMessage(Api.java:49)
    at com.rollioapp.nlp.Main.main(Main.java:41)
java.lang.Exception: error in writeMessage 

有证据表明有第三次写入,尽管我只进行了两次写入。

第 49 行是:

        String m = message.concat(" ...said the awesome NLP app!!!! Hell yeah!!!!!");

最佳答案

显然,您在构造函数中遇到了异常,忽略了它,或者不知道它,然后继续执行,认为它没有发生。

不要写这样的代码。构造函数应该抛出异常,而不是捕获它。那么剩下的代码就无法执行了。

关于java - 在 Java 中,尝试写入命名管道时出现空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30922759/

相关文章:

java - 使用 Dataflow 和 Java 删除 Firestore 集合

c - AF_UNIX 套接字开销?

c++ - libarb.so : cannot open shared object file: No such file or directory

linux - 监控目录变化

java - onActivityCreated/onStart/onViewCreated 方法中的 getView() 出现 NullPointerException 警告

java - 未添加 JPA 一对一关系

java - 添加到 TreeSet 时记录位置

java - 什么是NullPointerException,我该如何解决?

mysql - 在 WSO2BPS 上执行 LoadTest 时出现 NullPointerExceptions

java - Sonarqube:查看覆盖源代码的单元测试