file - 处理时output.println()中的错误

我正在尝试从处理中读取一些数据并将其写入文件。数据是正确的,因为我可以毫无问题地进行绘制。但是,当我尝试将其写入文件时,会引发以下错误:

Error, disabling serialEvent() for /dev/ttyACM0
null

具体来说,我发现了问题所在。在此功能中:

void serialEvent(Serial myPort) {
  int inByte = myPort.read();
  if (inByte >= 0 && inByte <= 255)
    {
    // This is what makes the problem arise
    output.println("test: "  + inByte);

    // ...
    }
  }

我什至更改了行output.println();与此,然后相同的功能工作,将其打印到正确的文件(但显然不是我想要的):

// This does work
point(mouseX, mouseY);
output.println(mouseX);

知道问题可能在哪里吗?我正在使用arduino,它从串行传递0到255之间的值。这些值似乎正确,因为我可以毫无问题地绘制它们。我也尝试过将println()更改为print(),但没有运气。

编辑。经过测试,我发现这确实很奇怪。这有效:

point(mouseX, mouseY);
output.println(inByte);

但是,如果没有point(),它将无法正常工作(相同的错误)。作为临时解决方案,我可以将output.println放在函数的末尾,但这显然不是长期解决方案。

最佳答案

我最终在代码中做了一个简单的检查:

if (output != null) {
  output.println(t + " " + inByte);
  }

它就是有效的。但是,我认为polymorphism would be even better here。有一个对象,如果输出未初始化,则该对象吸收文本,如果输出未初始化,则将其打印到文件中。

https://stackoverflow.com/questions/22119282/

相关文章:

c# - 为什么SubscribeOn在这里不能作为扩展方法而是直接调用?

objective-c - 错误Maddage : expected expression? [du

vba - VBA中的子级

compiler-errors - 模式匹配Erlang?

c# - C#asp.net中UPDATE语句中的语法错误

oop - 对字段和方法使用私有(private)而不是 protected 原因

generics - Ada : invalid prefix in selected compon

objective-c - 使用typedef枚举时,返回类型枚举EnumName返回枚举成员时会产

compiler-errors - 编译器无法识别显式类型

haskell - 如何编写map语句来比较haskell中两个列表的每个元素?