0

我正在尝试通过 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");

    }

}

4

1 回答 1

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);

希望这可以帮助.....

于 2013-10-14T14:53:45.513 回答