在Java中,试图写入命名管道时出现空指针异常
我是Java新手,不知道如何编写命名管道。我想让我的Java从一个管道读取数据,进行一些转换,然后写入另一个管道 这是在Linux上实现的。我安装管道时没有问题,而且我没有关于管道的错误 很长一段时间以来,我在这一行遇到了空指针异常: writepe.write(m) 我正在为“WritePie”使用FileOutputStream。但现在我已经转行成为一名印刷作家 我正在使用两个终端窗口来测试我的代码。我在一个终端上运行我的应用程序,然后在另一个终端上,我会将此消息写入管道:在Java中,试图写入命名管道时出现空指针异常,java,linux,nullpointerexception,Java,Linux,Nullpointerexception,我是Java新手,不知道如何编写命名管道。我想让我的Java从一个管道读取数据,进行一些转换,然后写入另一个管道 这是在Linux上实现的。我安装管道时没有问题,而且我没有关于管道的错误 很长一段时间以来,我在这一行遇到了空指针异常: writepe.write(m) 我正在为“WritePie”使用FileOutputStream。但现在我已经转行成为一名印刷作家 我正在使用两个终端窗口来测试我的代码。我在一个终端上运行我的应用程序,然后在另一个终端上,我会将此消息写入管道: echo "An
echo "Ankanth, expect messages like this!" > api-sends-messages-to-nlp
cat nlp-sends-messages-to-api
直到15分钟前,这条消息还可以被正确读取,但是当我的应用程序试图写回消息时,我得到了一个空指针异常。现在,直到我从管道中读取以下内容时,才会出现空指针异常:
echo "Ankanth, expect messages like this!" > api-sends-messages-to-nlp
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);
管道的路径是正确的,那么问题是什么
更新:
好的,如果我像这样改变read方法:
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;
}
这两张照片给了我:
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向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!!!!!");
很明显,您在构造函数中遇到了异常,忽略了它,或者不知道它,并继续认为它没有发生
不要这样写代码。构造函数应该抛出异常,而不是捕获它。那么其余的代码将无法执行。您声明,
“管道的路径是正确的,那么会出现什么问题?”
--您如何知道这一事实?您是否以某种方式对此进行了测试,从而知道该路径是有效的?我只想给出一个一般性的警告,在解决错误之前,不要假设任何东西都是正确的,除非通过测试证明它是正确的。我可以从终端窗口“ls/path/to/pipe”。Java读取绝对路径吗?正如我在上面解释的:“我使用FileOutputStream作为'WritePie'。但现在我切换到了PrintWriter。”所以“write”变成了“println”。我明白了。但是,当你还没有修复NPE时,为什么要改变呢?许多文件类型NPE都是由于路径不正确造成的。我更改了它,因为我认为我使用的类是错误的。我是Java新手,我不知道FileOutputStream是比PrintWriter更好还是更差。如果我在Api构造函数中遇到错误,我不应该在终端中打印“Api构造函数中的错误”吗?此外,如果我在构造函数中遇到异常,为什么我会在终端中看到这一行打印出来?System.out.println(“我们在主循环()中收到一条消息!”;正如我上面所说的,代码确实打印出“现在我们将写入管道”,这意味着我们将一直使用writepe.println(m)到达该行;在抛出异常之前。