Skip to content

Leetcode 2493: Java code will have Null pointer exception #64

@AthulRajuGit

Description

@AthulRajuGit
java.lang.NullPointerException: Cannot invoke "java.util.List.iterator()" because the return value of "java.util.Map.get(Object)" is null
  at line 6, Solution.isBipartite
  at line 79, Solution.magnificentSets

which is caused by by

 adj.computeIfAbsent(u, k -> new ArrayList<>()).add(v);
 adj.computeIfAbsent(v, k -> new ArrayList<>()).add(u);

can be resolved by

adj.get(u).add(v);
adj.get(v).add(u);

This occurs because some nodes in the graph may not have any neighboring nodes, which can lead to null pointer exception in isBipartite() method

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions