Please Note This forum exists for community support for the Mango product family and the Radix IoT Platform. Although Radix IoT employees participate in this forum from time to time, there is no guarantee of a response to anything posted here, nor can Radix IoT, LLC guarantee the accuracy of any information expressed or conveyed. Specific project questions from customers with active support contracts are asked to send requests to

Radix IoT Website Mango 3 Documentation Website Mango 4 Documentation Website

NullPointerException when trying to create RTU or ASCII Slave

  • We are trying to see if Modbus4J will work in our upcoming product. We are making a slave (server) application.

    I can get the TCP slave to work just fine, but when I try to create an RTU or ASCII slave, I get a NullPointerException immediately after calling ModbusSlaveSet start() method.

    Here's the code:

    public static void main(final String[] args)
        throws Exception
        ModbusSlaveSet listener;
        boolean tcp = false;  // TCP works, but RTU and ASCII do not
        final ModbusFactory modbusFactory = new ModbusFactory();
        if (tcp)
            listener = modbusFactory.createTcpSlave(false);
            final SerialParameters serialParams = new SerialParameters();
            listener = modbusFactory.createRtuSlave(serialParams);
        final BasicProcessImage processImage = new BasicProcessImage(1);
        new Thread(new Runnable()
            public void run()
                catch (ModbusInitException e)

    Here's the output:

    Stable Library
    Native lib Version = RXTX-2.1-7
    Java lib Version   = RXTX-2.1-7
    RXTX Warning:  Removing stale lock file. /var/lock/LCK..ttyUSB0
    com.serotonin.modbus4j.exception.ModbusInitException: java.lang.NullPointerException
    	at com.serotonin.modbus4j.serial.SerialSlave.start(
    	at com.serotonin.modbus4j.serial.rtu.RtuSlave.start(
    	at com.prozesstech.spectron.sandbox.Modbus4JTest$
    Caused by: java.lang.NullPointerException
    	at com.serotonin.messaging.StreamTransport.start(
    	at com.serotonin.modbus4j.serial.SerialSlave.start(
    	... 3 more

    Any idea what might be going on?


  • Adding to this discussion: the listener.start() calls into RtuSlave.start(), which immediately calls SerialSlave.start():

    in SerialSlave:

        public void start() throws ModbusInitException {
            try {
                serialPort = SerialUtils.openSerialPort(serialParameters);
                transport = new StreamTransport(serialPort.getInputStream(), serialPort.getOutputStream());
                transport.start("Modbus4J SerialSlave");  // <--- This line throws the exception
            catch (Exception e) {
                throw new ModbusInitException(e);

    The line that throws the NullPointerException is the third line, the transport.start("Modbus4J SerialSlave").

    This calls into the StreamTransport class of seroUtils.jar, which we do not have the source code for.

    Would it be possible for me to get this source code so that I can debug this problem?