![](/img/trans.png)
[英]Java - Null Pointer Exception when trying to add JLabel and/or JTextField to an array
[英]In Java, Null Pointer Exception when trying to write to a Named Pipe
我是 Java 新手,不明白如何寫入命名管道。 我想讓我的 Java 從一個管道讀取數據,進行一些轉換,然后寫入另一個管道。
這是在 Linux 上。 我設置管道沒有問題,我沒有關於管道的錯誤。
很長一段時間,我在這條線上得到一個空指針異常:
writePipe.write(m);
我在'writePipe' 中使用了 FileOutputStream。 但現在我已經切換到 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 loop() 中永遠運行:
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 printlns 給了我:
We just received this message:
null
但是為什么消息會是空的? 我從終端寫入管道,如下所示:
echo "expect messages like this!" > api-sends-messages-to-nlp
應用程序“聽到”了一些東西,因為這一行返回了一些東西:
readPipe.readLine();
但是為什么它返回“null”? 為什么它不返回寫入管道的內容?
再次更新:
[編輯]
因此,在終端中,我使用“echo”對應用程序進行 2 次寫入,然后使用“cat”從中讀取:
echo “Ankanth,期待這樣的消息!” > api-sends-messages-to-nlp
echo “Ankanth,期待這樣的消息!” > api-sends-messages-to-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
有第三次寫入的證據,即使我只做了 2 次寫入。
第 49 行是:
String m = message.concat(" ...said the awesome NLP app!!!! Hell yeah!!!!!");
顯然,您在構造函數中遇到了異常,忽略了它,或者不知道它,並認為它沒有發生過。
不要寫這樣的代碼。 構造函數應該拋出異常,而不是捕獲它。 那么剩下的代碼就無法執行了。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.