Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NullPointerException thrown when ConnectivityInspector is used as GraphListener #1162

Open
dschoepe opened this issue Sep 11, 2023 · 1 comment

Comments

@dschoepe
Copy link

 * JGraphT version: head (commit `4ec044c3e3078d6b837fcb04fad8cbc66f9fdc2f`)
 * Java version (java -version)/platform: 
openjdk version "17.0.8" 2023-07-18 LTS
OpenJDK Runtime Environment Corretto-17.0.8.7.1 (build 17.0.8+7-LTS)
OpenJDK 64-Bit Server VM Corretto-17.0.8.7.1 (build 17.0.8+7-LTS, mixed mode, sharing)

Issue

The documentation of ConnectivityInspector notes that it can be used as a graph listener to allow updating its results rather than recomputing when the graph changes. However, ConnectivityInspector does not ensure that connectedSets is not null when accessing it on updates, while some graph update methods set it to null in response to vertex or edge removal. As a result, the other update methods then cause a NullPointerException. This can be fixed by using lazyFindConnectedSets in the graph update methods rather than accessing connectedSets directly (I'll prepare a PR to make this change if we can confirm this issue).

Steps to reproduce (small coding example)

    @Test
    public void testConnectivityInspectorUpdatesOnVertexAdd() {
        ListenableGraph<String, DefaultEdge> g = new DefaultListenableGraph<>(new DefaultDirectedGraph<>(DefaultEdge.class));
        ConnectivityInspector<String, DefaultEdge> inspector = new ConnectivityInspector<>(g);
        g.addGraphListener(inspector);
        g.addVertex("foo"); // NullPointerException here because connectedSets is not yet initialized
        assertEquals(inspector.connectedSets().size(), 1);
    }

    @Test
    public void testConnectivityInspectorUpdatesOnVertexRemovalAndAdd() {
        ListenableGraph<String, DefaultEdge> g = new DefaultListenableGraph<>(new DefaultDirectedGraph<>(DefaultEdge.class));
        ConnectivityInspector<String, DefaultEdge> inspector = new ConnectivityInspector<>(g);
        g.addGraphListener(inspector);
        inspector.connectedSets(); // this initializes `connectedSets` preventing the previous issue.
        g.addVertex("foo");
        assertEquals(inspector.connectedSets().size(), 1);
        // Removing a vertex resets connectedSets to `null`
        g.removeVertex("foo");
        // Adding a vertex causes the now-null connectedSets to be accessed:
        g.addVertex("bar"); // NPE here
    }

    @Test
    public void testConnectivityInspectorUpdatesOnEdgeRemovalAndAddition() {
        ListenableGraph<String, DefaultEdge> g = new DefaultListenableGraph<>(new DefaultDirectedGraph<>(DefaultEdge.class));
        ConnectivityInspector<String, DefaultEdge> inspector = new ConnectivityInspector<>(g);
        g.addGraphListener(inspector);
        inspector.connectedSets();
        g.addVertex("foo");
        g.addVertex("bar");
        assertEquals(inspector.connectedSets().size(), 2);
        g.addEdge("foo", "bar");
        g.removeEdge("foo", "bar"); // also resets connectedSets to `null`
        g.addEdge("foo", "bar"); // NPE here
        assertEquals(inspector.connectedSets().size(), 1);
    }

Expected behaviour

The above tests run successfully.

Other information

@jsichi
Copy link
Member

jsichi commented Sep 11, 2023

Thank you for spotting this, yes, a PR would be much appreciated!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants