- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我是 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/
我正在使用 Assets 管道来管理我的 Grails 3.0 应用程序的前端资源。但是,似乎没有创建 CoffeeScript 文件的源映射。有什么办法可以启用它吗? 我的 build.gradle
我有一个我想要的管道: 提供一些资源, 运行一些测试, 拆资源。 我希望第 3 步中的拆卸任务运行 不管 测试是否通过或失败,在第 2 步。据我所知 runAfter如果前一个任务成功,则只运行一个任
如果我运行以下命令: Measure-Command -Expression {gci -Path C:\ -Recurse -ea SilentlyContinue | where Extensio
我知道管道是一个特殊字符,我需要使用: Scanner input = new Scanner(System.in); String line = input.next
我再次遇到同样的问题,我有我的默认处理方式,但它一直困扰着我。 有没有更好的办法? 所以基本上我有一个运行的管道,在管道内做一些事情,并想从管道内返回一个键/值对。 我希望整个管道返回一个类型为 ps
我有三个环境:dev、hml 和 qa。 在我的管道中,根据分支,阶段有一个条件来检查它是否会运行: - stage: Project_Deploy_DEV condition: eq(varia
我有 Jenkins Jenkins ver. 2.82 正在运行并想在创建新作业时使用 Pipeline 功能。但我没有看到这个列为选项。我只能在自由式项目、maven 项目、外部项目和多配置之间进
在对上一个问题 (haskell-data-hashset-from-unordered-container-performance-for-large-sets) 进行一些观察时,我偶然发现了一个奇
我正在寻找有关如何使用管道将标准输出作为其他命令的参数传递的见解。 例如,考虑这种情况: ls | grep Hello grep 的结构遵循以下模式:grep SearchTerm PathOfFi
有没有办法不因声明性管道步骤而失败,而是显示警告?目前我正在通过添加 || exit 0 来规避它到 sh 命令行的末尾,所以它总是可以正常退出。 当前示例: sh 'vendor/bin/phpcs
我们正在从旧的 Jenkins 设置迁移到所有计划都是声明性 jenkinsfile 管道的新服务器……但是,通过使用管道,我们无法再手动清除工作区。我如何设置 Jenkins 以允许 手动点播清理工
我在 Python 中阅读了有关 Pipelines 和 GridSearchCV 的以下示例: http://www.davidsbatista.net/blog/2017/04/01/docume
我有一个这样的管道脚本: node('linux'){ stage('Setup'){ echo "Build Stage" } stage('Build'){ echo
我正在使用 bitbucket 管道进行培训 这是我的 bitbucket-pipelines.yml: image: php:7.2.9 pipelines: default:
我正在编写一个程序,其中输入文件被拆分为多个文件(Shamir 的 secret 共享方案)。 这是我想象的管道: 来源:使用 Conduit.Binary.sourceFile 从输入中读取 导管:
我创建了一个管道,它有一个应该只在开发分支上执行的阶段。该阶段还需要用户输入。即使我在不同的分支上,为什么它会卡在这些步骤的用户输入上?当我提供输入时,它们会被正确跳过。 stage('Deplo
我正在尝试学习管道功能(%>%)。 当试图从这行代码转换到另一行时,它不起作用。 ---- R代码--原版----- set.seed(1014) replicate(6,sample(1:8))
在 Jenkins Pipeline 中,如何将工件从以前的构建复制到当前构建? 即使之前的构建失败,我也想这样做。 最佳答案 Stuart Rowe 还在 Pipeline Authoring Si
我正在尝试使用 执行已定义的作业构建 使用 Jenkins 管道的方法。 这是一个简单的例子: build('jenkins-test-project-build', param1 : 'some-
当我使用 where 过滤器通过管道命令排除对象时,它没有给我正确的输出。 PS C:\Users\Administrator> $proall = Get-ADComputer -filter *
我是一名优秀的程序员,十分优秀!