Repository: apache/netbeans
Issue: Module graph fails to display

### Apache NetBeans version

Apache NetBeans 13

### What happened

Applies in both Apache Netbeans 14 and 13.

While module-info.java is loaded into editor window, click Graph view button (of Source, History, Graph). No graph display and an Unexpected Exception notification is generated. 

Priority: Silent; Category: Error.
Cannot invoke "org.netbeans.modules.java.graph.GraphNode.getImpl()" because the return value of "org.netbeans.modules.java.graph.DependencyGraphScene.getRootGraphNode()" is null.

Full text is:
```
java.lang.NullPointerException: Cannot invoke "org.netbeans.modules.java.graph.GraphNode.getImpl()" because the return value of "org.netbeans.modules.java.graph.DependencyGraphScene.getRootGraphNode()" is null
	at org.netbeans.modules.java.graph.DependencyGraphScene.highlightDepth(DependencyGraphScene.java:690)
	at org.netbeans.modules.java.graph.DependencyGraphScene.highlightDepthIntern(DependencyGraphScene.java:706)
	at org.netbeans.modules.java.graph.DependencyGraphScene.access$900(DependencyGraphScene.java:75)
	at org.netbeans.modules.java.graph.DependencyGraphScene$AllActionsProvider.select(DependencyGraphScene.java:672)
	at org.netbeans.modules.visual.action.SelectAction.mousePressed(SelectAction.java:69)
	at org.netbeans.api.visual.action.WidgetAction$Chain.mousePressed(WidgetAction.java:749)
	at org.netbeans.api.visual.widget.SceneComponent$Operator$2.operate(SceneComponent.java:529)
	at org.netbeans.api.visual.widget.SceneComponent.processLocationOperator(SceneComponent.java:306)
	at org.netbeans.api.visual.widget.SceneComponent.processLocationOperator(SceneComponent.java:261)
	at org.netbeans.api.visual.widget.SceneComponent.mousePressed(SceneComponent.java:133)
	at java.desktop/java.awt.Component.processMouseEvent(Component.java:6623)
	at java.desktop/javax.swing.JComponent.processMouseEvent(JComponent.java:3389)
	at java.desktop/java.awt.Component.processEvent(Component.java:6391)
	at java.desktop/java.awt.Container.processEvent(Container.java:2266)
	at java.desktop/java.awt.Component.dispatchEventImpl(Component.java:5001)
	at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2324)
	at java.desktop/java.awt.Component.dispatchEvent(Component.java:4833)
	at java.desktop/java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4948)
	at java.desktop/java.awt.LightweightDispatcher.processMouseEvent(Container.java:4572)
	at java.desktop/java.awt.LightweightDispatcher.dispatchEvent(Container.java:4516)
	at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2310)
	at java.desktop/java.awt.Window.dispatchEventImpl(Window.java:2780)
	at java.desktop/java.awt.Component.dispatchEvent(Component.java:4833)
	at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:773)
	at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:722)
	at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:716)
	at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
	at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
	at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:97)
	at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:746)
	at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:744)
	at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
	at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
	at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:743)
	at org.netbeans.core.TimableEventQueue.dispatchEvent(TimableEventQueue.java:136)
[catch] at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
	at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
	at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
	at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
	at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
	at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
```

### How to reproduce

Load a project, open module-info.java in the editor, click Graph button.

### Did this work correctly in an earlier version?

No

### Operating System

macos Monterey 12.4 (on Apple M1 Max)

### JDK

zulu17.34.19-ca-fx-jdk17.0.3-macosx_aarch64 (java --version: openjdk 17.0.3 2022-04-19 LTS OpenJDK Runtime Environment Zulu17.34+19-CA (build 17.0.3+7-LTS) OpenJDK 64-Bit Server VM Zulu17.34+19-CA (build 17.0.3+7-LTS, mixed mode, sharing)

### Apache NetBeans packaging

Apache NetBeans provided installer

### Anything else

As far as I can tell (I'm a very new user), this happens every time in both v13 and v14.

### Are you willing to submit a pull request?

No

### Code of Conduct

Yes

Changed files:
java/java.graph/src/org/netbeans/modules/java/graph/SearchVisitor.java
java/java.module.graph/src/org/netbeans/modules/java/module/graph/DependencyCalculator.java

diff --git a/java/java.graph/src/org/netbeans/modules/java/graph/SearchVisitor.java b/java/java.graph/src/org/netbeans/modules/java/graph/SearchVisitor.java
index 87cf0cdd76..c61f662229 100644
--- a/java/java.graph/src/org/netbeans/modules/java/graph/SearchVisitor.java
+++ b/java/java.graph/src/org/netbeans/modules/java/graph/SearchVisitor.java
@@ -44,7 +44,7 @@ class SearchVisitor implements GraphNodeVisitor {
         if (root == null) {
             root = node;
         }
-        if (scene.isIncluded(node)) {
+        if (!path.contains(node) && scene.isIncluded(node)) {
             GraphNode grNode = scene.getGraphNodeRepresentant(node);
             if (grNode == null) {
                 return false;
@@ -59,7 +59,7 @@ class SearchVisitor implements GraphNodeVisitor {
     }
 
     @Override public boolean endVisit(GraphNodeImplementation node) {
-        if (scene.isIncluded(node)) {
+        if (path.peek() == node) {
             path.pop();
         }
         return true;
diff --git a/java/java.module.graph/src/org/netbeans/modules/java/module/graph/DependencyCalculator.java b/java/java.module.graph/src/org/netbeans/modules/java/module/graph/DependencyCalculator.java
index e13cc4e9d2..b82c3b8c34 100644
--- a/java/java.module.graph/src/org/netbeans/modules/java/module/graph/DependencyCalculator.java
+++ b/java/java.module.graph/src/org/netbeans/modules/java/module/graph/DependencyCalculator.java
@@ -18,7 +18,7 @@
  */
 package org.netbeans.modules.java.module.graph;
 
-import com.sun.source.tree.Tree;
+import com.sun.source.tree.ModuleTree;
 import com.sun.source.util.TreePath;
 import java.io.IOException;
 import java.util.ArrayList;
@@ -31,13 +31,10 @@ import java.util.List;
 import java.util.Map;
 import java.util.stream.Collectors;
 import javax.lang.model.element.ModuleElement;
-import javax.tools.JavaFileObject;
 import org.netbeans.api.annotations.common.NonNull;
-import org.netbeans.api.java.classpath.ClassPath;
 import org.netbeans.api.java.source.ClasspathInfo;
 import org.netbeans.api.java.source.JavaSource;
 import org.openide.filesystems.FileObject;
-import org.openide.filesystems.URLMapper;
 import org.openide.util.Exceptions;
 import org.openide.util.Parameters;
 
@@ -81,9 +78,9 @@ final class DependencyCalculator {
                 try {
                     js.runUserActionTask((cc)-> {
                         cc.toPhase(JavaSource.Phase.ELEMENTS_RESOLVED);
-                        final List<? extends Tree> decls = cc.getCompilationUnit().getTypeDecls();
-                        final ModuleElement me =  !decls.isEmpty() && decls.get(0).getKind() == Tree.Kind.MODULE ?
-                                (ModuleElement) cc.getTrees().getElement(TreePath.getPath(cc.getCompilationUnit(), decls.get(0))) :
+                        final ModuleTree moduleTree = cc.getCompilationUnit().getModule();
+                        final ModuleElement me = moduleTree != null ?
+                                (ModuleElement) cc.getTrees().getElement(TreePath.getPath(cc.getCompilationUnit(), moduleTree)) :
                                 null;
                         if (me != null) {
                             final Map<String, ModuleNode> mods = new LinkedHashMap<>();