0

I have this code using the RXTX library:

public void sendSerial(String msg) throws IOException{
    try{
        out1.write(msg.getBytes());
    }catch(Exception e){
        e.printStackTrace();
    }
    try{
        out1.flush();
    }catch(Exception e){
        e.printStackTrace();
    }
}

I am basing this off of the 2 way communication example. out1 (global variable) is set like this after the out variable has been set:

out1 = out;

But when I try to write or flush, it gives me a nullPointerException at the line of the writing. Here's my full code:

package net.codepixl.serialPortBuk;

import gnu.io.CommPort;
import gnu.io.CommPortIdentifier;
import gnu.io.SerialPort;

import java.io.ByteArrayInputStream;
import java.io.FileDescriptor;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.Charset;
import java.util.Scanner;

import org.bukkit.command.CommandSender;

public class Serial
{
        public Serial()
    {
        super();
    }
        OutputStream out1;
        CommPort commPort;
    CommPortIdentifier portIdentifier = null;
    SerialPort serialPort;
    void connect ( String portName, CommandSender s) throws Exception
    {
        portIdentifier = CommPortIdentifier.getPortIdentifier(portName);
        if ( portIdentifier.isCurrentlyOwned() )
        {
            System.out.println("Error: Port is currently in use");
            s.sendMessage("Error: Port COM3 is currently in use");
        }
        else
        {
            commPort = portIdentifier.open(this.getClass().getName(),2000);

            if ( commPort instanceof SerialPort )
            {
                serialPort = (SerialPort) commPort;
                serialPort.setSerialPortParams(9600,SerialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_NONE);

                InputStream in = serialPort.getInputStream();
                OutputStream out = serialPort.getOutputStream();
                out1 = out;

                (new Thread(new SerialReader(in))).start();
                (new Thread(new SerialWriter(out))).start();

            }
            else
            {

            }
        }    
    }

    public static class SerialWriter implements Runnable
    {
        OutputStream out;

        public SerialWriter ( OutputStream out )
        {
            this.out = out;
        }

        public void run ()
        {      
        }
    }

    public void sendSerial(String msg,CommandSender s) throws IOException{
                s.sendMessage("Sending "+msg+"...");
                try{
                out1.write(msg.getBytes());
                }catch(Exception e){
                        e.printStackTrace();
                }
        s.sendMessage("Flushing output...");
        try{
        out1.flush();
        }catch(Exception e){
                e.printStackTrace();
        }
        s.sendMessage("Done!");
    }

    public void disconnect() {
        if (commPort != null) {
            try {
                // close the i/o streams.
                commPort.getInputStream().close();
                commPort.getOutputStream().close();
            } catch (IOException ex) {
                // don't care
            }
            // Close the port.
            commPort.close();
        }
    }

    /** */
    public static class SerialReader implements Runnable
    {
        InputStream in;

        public SerialReader ( InputStream in )
        {
            this.in = in;
        }

        public void run ()
        {
            byte[] buffer = new byte[1024];
            int len = -1;
            try
            {
                while ( ( len = this.in.read(buffer)) > -1 )
                {
                    System.out.print(new String(buffer,0,len,"US-ASCII"));
                }
            }
            catch ( IOException e )
            {
                e.printStackTrace();
            }            
        }
    }

    public void startSerial(CommandSender s)
    {
        try
        {
            (new Serial()).connect("COM3",s);
        }
        catch ( Exception e )
        {
            s.sendMessage("COM port in use! (Maybe by this server...)");
        }
    }
}

(Don't worry about the commandSender stuff, this is a CraftBukkit mod, and if you don't know what it is, don't worry. Just know that when the program starts, startSerial is called, and when writing a string, sendSerial is called.)

trooper
  • 4,444
  • 5
  • 32
  • 32
ByteDuck
  • 1,821
  • 3
  • 15
  • 27
  • Do you have the NPE stack trace available? – Arnaud Kleinveld Dec 27 '14 at 13:05
  • @ArnaudKleinveld the only stacktrace I have is: Java.lang.NullPointerException that points to the line on which I write my string. – ByteDuck Dec 27 '14 at 16:41
  • I suggest to add some debug code. For example start with the runnables to see if they are actually started: System.out.print("Starting SerialReader"); – Arnaud Kleinveld Dec 27 '14 at 17:14
  • @ArnaudKleinveld I did already; and they do. Everything runs. I added a message at the end of each Method saying when it finishes, and the ones that are supposed to finish do. The runnables are being ran, and it does get past the out1.flush(), but throws an error. – ByteDuck Dec 27 '14 at 17:16
  • @ArnaudKleinveld I feel stupid now. My out1 wasn't static XD – ByteDuck Dec 27 '14 at 18:47
  • If you need to make it static it means you're not calling sendSerial on the same object instance. That can cause other concurrency problems. – Arnaud Kleinveld Dec 28 '14 at 03:20

1 Answers1

0

I just had to make my out1 variable static XD

ByteDuck
  • 1,821
  • 3
  • 15
  • 27