我正在尝试通过java将arduino连接到我的计算机,我发现在线的主代码写了arduino发送到stdo的内容。我正在徘徊如何把它放在一个文件上我创建了“Scrittore”类,而在没有线程的情况下使用它一切都很好,但是当它与主代码一起使用时它给了我错误:
**Exception in thread "Thread-1" java.lang.NullPointerException
at ricezionearduinoseriale.Scrittore.scrivi(Scrittore.java:32)
at ricezionearduinoseriale.SerialTest$SerialReader.run(SerialTest.java:67)
at java.lang.Thread.run(Thread.java:724)**
有人可以帮助我找到错误或我做错了吗?(我刚刚开始使用java,我仍然是一个非常的菜鸟)
public class SerialTest{
public SerialTest()
{
super();
}
void connect ( String portName ) throws Exception
{
CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(portName);
if ( portIdentifier.isCurrentlyOwned() )
{
System.out.println("Error: Port is currently in use");
}
else
{
CommPort commPort = portIdentifier.open(this.getClass().getName(),2000);
if ( commPort instanceof SerialPort )
{
SerialPort serialPort = (SerialPort) commPort;
serialPort.setSerialPortParams(9600,SerialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_NONE);
InputStream in = serialPort.getInputStream();
OutputStream out = serialPort.getOutputStream();
(new Thread(new SerialReader(in))).start();
(new Thread(new SerialWriter(out))).start();
}
else
{
System.out.println("Error: Only serial ports are handled by this example.");
}
}
}
/** */
public static class SerialReader implements Runnable
{
InputStream in;
Scrittore scrittura=new Scrittore();
public SerialReader ( InputStream in )
{
this.in = in;
}
public void run ()
{
byte[] buffer = new byte[1024];
int len = -1;
try
{
scrittura.open("output.txt");
//System.out.println(new String(buffer,0,len));
while ( ( len = this.in.read(buffer)) > -1 )
{
String out=new String(buffer,0,len);
System.out.print(out);
scrittura.scrivi(out);
}
}
catch ( IOException e )
{
e.printStackTrace();
}
}
}
/** */
public static class SerialWriter implements Runnable
{
OutputStream out;
public SerialWriter ( OutputStream out )
{
this.out = out;
}
public void run ()
{
try
{
int c = 0;
while ( ( c = System.in.read()) > -1 )
{
this.out.write(c);
}
}
catch ( IOException e )
{
e.printStackTrace();
}
}
}
public static void main ( String[] args )
{
try
{
(new SerialTest()).connect("/dev/ttyACM0");
}
catch ( Exception e )
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public class Scrittore {
FileWriter fw=null;
BufferedWriter bw=null;
public void open(String name){
try{
File file = new File(name);
// if file doesnt exists, then create it
if (!file.exists()) {
file.createNewFile();
}
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
System.out.println("Aperto");
}catch(IOException e){
e.printStackTrace();
}
}
public void scrivi(/*String content*/) {//Skipped input while debugging
String content="ciao";
try{
// System.out.println("Done");
bw.write(content);
}
catch(IOException e){
e.printStackTrace();
}
}
public void chiudi(){
try{
bw.close();
fw.close();
}catch(IOException e){
e.printStackTrace();
}
System.out.println("Chiuso");
}
}
答案 0 :(得分:0)
你在scrivi函数和chiudi函数中使用bw,fw变量,它们是类的私有变量,而在open函数中你使用了一个声明为
的临时变量FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
我相信这一行应该是,
fw = new FileWriter(file.getAbsoluteFile());
bw = new BufferedWriter(fw);
希望,这有助于.....