patrickdmw commented on issue #2266:
URL: https://github.com/apache/plc4x/issues/2266#issuecomment-4490182037

   I ran into a similar problem when using 
https://github.com/hivemq/hivemq-edge which uses 0.13.1 of the plc4x library.
   When Edge tries to poll data from non-existing / malformed S7 tags, it runs 
into an NPE inside the plc4x library, without any helpful error message
   ```java
   2026-05-13 19:50:09,524 [hivemq-edge-scheduled-group-0] DEBUG 
com.hivemq.edge.modules.adapters.impl.polling.PollingTask#handleExceptionDuringPolling
 - Application Error 2 in sampler S7-PLC-MUC13 -> 
java.lang.NullPointerException: Cannot invoke 
"org.apache.plc4x.java.spi.messages.utils.PlcTagItem.getResponseCode()" because 
the return value of "java.util.LinkedHashMap.get(Object)" is null
   java.util.concurrent.CompletionException: java.lang.NullPointerException: 
Cannot invoke 
"org.apache.plc4x.java.spi.messages.utils.PlcTagItem.getResponseCode()" because 
the return value of "java.util.LinkedHashMap.get(Object)" is null
           at 
java.base/java.util.concurrent.CompletableFuture.wrapInCompletionException(Unknown
 Source)
           at 
java.base/java.util.concurrent.CompletableFuture.encodeThrowable(Unknown Source)
           at 
java.base/java.util.concurrent.CompletableFuture.uniApplyNow(Unknown Source)
           at 
java.base/java.util.concurrent.CompletableFuture.uniApplyStage(Unknown Source)
           at 
java.base/java.util.concurrent.CompletableFuture.thenApply(Unknown Source)
           at 
org.apache.plc4x.java.spi.connection.AbstractPlcConnection.read(AbstractPlcConnection.java:212)
           at 
org.apache.plc4x.java.spi.messages.DefaultPlcReadRequest.execute(DefaultPlcReadRequest.java:61)
           at 
com.hivemq.edge.adapters.plc4x.impl.Plc4xConnection.read(Plc4xConnection.java:179)
           at 
com.hivemq.edge.adapters.plc4x.impl.AbstractPlc4xAdapter.poll(AbstractPlc4xAdapter.java:110)
           at 
com.hivemq.protocols.northbound.PerAdapterSampler.execute(PerAdapterSampler.java:61)
           at 
com.hivemq.edge.modules.adapters.impl.polling.PollingTask.run(PollingTask.java:84)
           at 
java.base/java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
           at java.base/java.util.concurrent.FutureTask.run(Unknown Source)
           at 
java.base/java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(Unknown
 Source)
           at 
java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
           at 
java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
           at java.base/java.lang.Thread.run(Unknown Source)
   Caused by: java.lang.NullPointerException: Cannot invoke 
"org.apache.plc4x.java.spi.messages.utils.PlcTagItem.getResponseCode()" because 
the return value of "java.util.LinkedHashMap.get(Object)" is null
           at 
org.apache.plc4x.java.spi.messages.DefaultPlcReadRequest.getTagResponseCode(DefaultPlcReadRequest.java:86)
           at 
org.apache.plc4x.java.spi.connection.AbstractPlcConnection.lambda$0(AbstractPlcConnection.java:221)
           ... 15 common frames omitted
   2026-05-13 19:50:09,524 [hivemq-edge-scheduled-group-0] DEBUG 
com.hivemq.edge.modules.adapters.impl.ProtocolAdapterStateImpl#setConnectionStatus
 - Setting connection status of adapter 'S7-PLC-MUC13' to 'ERROR'
   ```
   
   The problem in my case comes from the `AbstractPlcConnection.read` method 
https://github.com/apache/plc4x/blob/develop/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/connection/AbstractPlcConnection.java#L209-L231
   ```java
       @Override
       public CompletableFuture<PlcReadResponse> read(PlcReadRequest 
readRequest) {
           PlcReadRequest filteredReadRequest = 
getFilteredReadRequest((DefaultPlcReadRequest) readRequest);
           return internalRead(filteredReadRequest)
               .thenApply(filteredReadResponse -> {
                   // Shortcut for the case that all tags were valid.
                   if(readRequest.getNumberOfTags() == 
filteredReadRequest.getNumberOfTags()) {
                       return filteredReadResponse;
                   }
   
                   Map<String, PlcResponseItem<PlcValue>> values = new 
HashMap<>();
                   for (String tagName : readRequest.getTagNames()) { // TODO 
this loops over all requested tag names, including those not present in the 
"filteredReadRequest" because they did not exist on the server
                       // If the tag was correct, then we expect a response in 
the response.
                       if(filteredReadRequest.getTagResponseCode(tagName) != 
null) { // TODO getTagResponseCode(tagName) runs into NPE in 
org/apache/plc4x/java/spi/messages/DefaultPlcReadRequest.java because it calls 
"return tags.get(tagName).getResponseCode();" where "tags.get(tagName)" is 
null. this should be checked before / at least caught and an error message 
logged, indicating which tag name was not found
                           values.put(tagName, ((DefaultPlcReadResponse) 
filteredReadResponse).getPlcResponseItem(tagName));
                       }
                       // In all other cases forward the initial error to the 
final output.
                       else {
                           values.put(tagName, new 
DefaultPlcResponseItem<>(readRequest.getTagResponseCode(tagName), null));
                       }
                   }
                   return new DefaultPlcReadResponse(readRequest, values);
               });
       }
   ```
   
   `filteredReadRequest.getTagResponseCode(tagName)` here can lead to NPEs when 
reading tag names that are not present on the server, or using the wrong S7 
format for the tag name.
   
   In this case I was trying to read `DB19B40.2` from an S7_300 machine, 
instead of using the correct address format `%DB19:40.2`. But since the 
problematic tag is never logged, it was not that easy to figure out.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to