Menu

Null Pointer Exception when calling ModbusTCPTransaction.execute()

2016-07-25
2016-11-17
  • Mark Walton

    Mark Walton - 2016-07-25

    Hello:
    I have an active TCP connection that has been verfied. When I run the code below I get a NullPointerException when calling trans.execute. Is there anyone that can tell me what I am doing wrong. Thanks in advance.

    public void readWriteTest(){

        int refRead = Integer.parseInt("30001", 16);
    
        ReadMultipleRegistersRequest request = new ReadMultipleRegistersRequest(refRead, 1);
        ModbusTCPTransaction trans = new ModbusTCPTransaction(tcpMasterConnection);
        trans.setRequest(request);
    
       try{
            trans.execute();
        }
        catch(ModbusSlaveException modbusSlaveException){}
        catch(ModbusIOException modbusIOException){}
        catch(ModbusException modbusException){}
    
       ReadMultipleRegistersResponse response = (ReadMultipleRegistersResponse)trans.getResponse();
    
       for(int iIndex = 0; iIndex < response.getWordCount(); iIndex++){
           System.out.println("Value = " + response.getRegisterValue(0));
       }
    }
    
     
  • R. J. Mathar

    R. J. Mathar - 2016-11-17

    I propose you put the modbusSlaveException.printStackTrace() into the block of the ModbusSlaveException and the corresponding stack traces into the other two blocks to get more information on why the execute() fails.

     
  • Andrey Lavrukhin

    I have the same issue. I connected my TCPMasterConnection to the modbus TCP emulator (modsim32). If m_Transaction.setReconnecting(false); then m_Transaction.execute() throw java.lang.NullPointerException at com.ghgande.j2mod.modbus.io.ModbusTCPTransport.writeMessage(ModbusTCPTransport.java:151)
    at com.ghgande.j2mod.modbus.io.ModbusTCPTransaction.execute(ModbusTCPTransaction.java:218)

    The higher frequency of requests (lower delays in Thread.sleep() inside loop) - the fasters NullPointerException appears. But always in 1-5 seconds from start.

    If m_Transaction.setReconnecting(true), then application create too many (over hundred) TCP-connetions in TIME_WAIT state. It's unacceptable in production, cause many of modbus-tcp slaves have limit of simultaneously incoming connection (max 16 connection in my case).

    I need to request modbus-slaves several times in second.
    How can I work around NullPointerException without hundreds of unclosed connection?

     

    Last edit: Andrey Lavrukhin 2017-05-02
  • Andrey Lavrukhin

    This issue was on j2mod-1.06.
    Found it solved in j2mod-2.3.3

     

Log in to post a comment.