Inside java stream the map function does not recognize my local variable

1.3k Views Asked by At

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);
        });
2

There are 2 best solutions below

0
On

If your node is null, this has nothing to do with streams or Java 8, it's just that your arrayNodeToListNode operation is returning some null json nodes in the list.

The error (if any) is in the arrayNodeToListNode method. If it's valid for that method to return a list with some null elements, you can perfectly filter nulls out before using Stream.map, by using Stream.filter(Objects::nonNull):

List<JsonNode> jsonNodes = arrayNodeToListNode((ArrayNode) t.get("coverageList"));
List<JsonNode> collect = jsonNodes.stream()
    .filter(Objects::nonNull)
    .map(node -> node.get("/coverage/coverageTypeLevel"))
    ...
0
On

In fact I have solved my problem; I was debugging my application in intelliJ and I faced with the fact that in IntelliJ's debugger the node was null. The reason was simply because I did not know in order to add the break points in the debugger mode I needed to chose the \lambda instead of "line".

That was the problem. In fact All was good, no null pointer problem. I had in fact another porblem with a filter and as soon as I discovered how the intelliJ's debugger functions for lambda-expressions, I solved my problem. At the end I have this code that functions very well:

StringBuilder response = new StringBuilder();
    List<JsonNode> msgs = arrayNodeToListNode((ArrayNode) kmsResponse.at(kmsResponsePath));
    List<JsonNode> collect1 = msgs.stream().filter(node -> {
                List<JsonNode> collect = arrayNodeToListNode((ArrayNode) node.get("coverageList")).stream()
                        .map(entry -> entry.at("/coverage/coverageTypeLevel"))
                        .filter(enttry -> formule.equals(enttry.get("aggReference").textValue())).collect(Collectors.toList());
                return collect.size() > 0;
            }

    ).collect(Collectors.toList());
response.append(collect1);

Thanks However.