I am trying to search inside the flow of a jsonNode using the java stream. At one point I receive an ArrayNode and I have converted it to a List of JsonNodes using a simple private methode in my class; however, when I want to use the map fucntion to map my node I have realized that the local variable (node; inside the first map) is null. I do not understand why and how to solve this problem, considering that I am new in Java 8. I am posting my code here:
List<JsonNode> msgs = arrayNodeToListNode((ArrayNode) kmsResponse.at(kmsResponsePath));
        msgs.stream().forEach(t -> {
            List<JsonNode> jsonNodes = arrayNodeToListNode((ArrayNode) t.get("coverageList"));
            List<JsonNode> collect = jsonNodes.stream()
                    .map(node -> node.get("/coverage/coverageTypeLevel"))
                    .filter(node -> formule.equals(node.get("aggReference").textValue()))
                    .map(node -> t.get("premiumSplittedList"))
                    .map(node -> node.get("value")).collect(Collectors.toList());
            String value = collect.toString();
            response.append(collect);
        });
 
                        
If your
nodeisnull, this has nothing to do with streams or Java 8, it's just that yourarrayNodeToListNodeoperation is returning somenulljson nodes in the list.The error (if any) is in the
arrayNodeToListNodemethod. If it's valid for that method to return a list with somenullelements, you can perfectly filternulls out before usingStream.map, by usingStream.filter(Objects::nonNull):