Ruben Laguna's blog

Apr 30, 2009 - 2 minute read - addchild connectionwidget fontdesignmetrics labelwidget layer layerwidget library metricskey netbeans nullpointerexception visual widget

NullPointerException in FontDesignMetrics$MetricsKey.init when using Netbeans Visual Library

If you’re getting a NullPointerException stacktrace like the one below while using the Netbeans Visual Library to create your own GraphScene, check that your adding your widgets propertly to scene. A good idea is to use LayerWidgets, declaring them in the constructor of your GraphScene:

mainLayer = new LayerWidget(this);
addChild(mainLayer);
connectionLayer = new LayerWidget(this);
addChild(connectionLayer);

and then add the widgets to those layers in attachNodeWidget and attachEdgeWidget:

@Override
protected Widget attachNodeWidget(String node) {
  LabelWidget w = new LabelWidget(this, node);
  w.setBorder(BORDER4);
  w.getActions().addAction(createSelectAction());
  w.getActions().addAction(createObjectHoverAction());
  w.getActions().addAction(ActionFactory.createMoveAction());
  this.mainLayer.addChild(w);
  return w;
}

@Override
protected Widget attachEdgeWidget(String edge) {
  ConnectionWidget w = new ConnectionWidget(this);
  w.setTargetAnchorShape(AnchorShape.TRIANGLE_FILLED);
  this.connectionLayer.addChild(w);
  return w;
}

The stacktrace:

java.lang.NullPointerException
at sun.font.FontDesignMetrics$MetricsKey.init(FontDesignMetrics.java:199)
at sun.font.FontDesignMetrics.getMetrics(FontDesignMetrics.java:267)
at sun.java2d.SunGraphics2D.getFontMetrics(SunGraphics2D.java:973)
at org.netbeans.api.visual.widget.LabelWidget.calculateClientArea(LabelWidget.java:260)
at org.netbeans.api.visual.widget.Widget.calculatePreferredBounds(Widget.java:1005)
at org.netbeans.api.visual.widget.Widget.getPreferredBounds(Widget.java:981)
at org.netbeans.modules.visual.graph.layout.HierarchicalLayout$BuildDatastructure.run(HierarchicalLayout.java:262)
at org.netbeans.modules.visual.graph.layout.HierarchicalLayout$AlgorithmPart.start(HierarchicalLayout.java:171)
at org.netbeans.modules.visual.graph.layout.HierarchicalLayout.performGraphLayout(HierarchicalLayout.java:206)
at org.netbeans.api.visual.graph.layout.GraphLayout.layoutGraph(GraphLayout.java:116)
at org.netbeans.api.visual.layout.LayoutFactory$1.performLayout(LayoutFactory.java:284)
at org.netbeans.api.visual.layout.SceneLayout$LayoutSceneListener.sceneValidated(SceneLayout.java:122)
at org.netbeans.api.visual.widget.Scene.validate(Scene.java:420)
at org.netbeans.api.visual.widget.SceneComponent.addNotify(SceneComponent.java:88)
at java.awt.Container.addImpl(Container.java:1062)
at java.awt.Container.add(Container.java:352)
...
...
...
at org.openide.windows.CloneableOpenSupport.openCloneableTopComponent(CloneableOpenSupport.java:197)
at org.openide.windows.CloneableOpenSupport$1.run(CloneableOpenSupport.java:98)
at org.openide.util.Mutex.doEvent(Mutex.java:1335)
at org.openide.util.Mutex.writeAccess(Mutex.java:452)
at org.openide.windows.CloneableOpenSupport.open(CloneableOpenSupport.java:95)
at org.openide.actions.OpenAction.performAction(OpenAction.java:81)
at org.openide.util.actions.NodeAction$DelegateAction$1.run(NodeAction.java:581)
at org.netbeans.modules.openide.util.ActionsBridge.doPerformAction(ActionsBridge.java:77)
at org.openide.util.actions.NodeAction$DelegateAction.actionPerformed(NodeAction.java:577)
at org.openide.explorer.view.TreeView$PopupSupport.mouseClicked(TreeView.java:1492)
at java.awt.AWTEventMulticaster.mouseClicked(AWTEventMulticaster.java:253)
at java.awt.AWTEventMulticaster.mouseClicked(AWTEventMulticaster.java:252)
at java.awt.AWTEventMulticaster.mouseClicked(AWTEventMulticaster.java:252)
at java.awt.AWTEventMulticaster.mouseClicked(AWTEventMulticaster.java:252)
at java.awt.Component.processMouseEvent(Component.java:6129)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3265)
at java.awt.Component.processEvent(Component.java:5891)
at java.awt.Container.processEvent(Container.java:2102)
at java.awt.Component.dispatchEventImpl(Component.java:4497)
at java.awt.Container.dispatchEventImpl(Container.java:2160)
at java.awt.Component.dispatchEvent(Component.java:4327)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4366)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4039)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3960)
at java.awt.Container.dispatchEventImpl(Container.java:2146)
at java.awt.Window.dispatchEventImpl(Window.java:2440)
at java.awt.Component.dispatchEvent(Component.java:4327)
[catch] at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at org.netbeans.core.TimableEventQueue.dispatchEvent(TimableEventQueue.java:104)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:300)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:210)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:200)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:195)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:187)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:121)

Hopefully if you run into the same exception this will help you solve it.