NPE when calling new JPanel() in headless (RepaintManager.currentManager)

删除回忆录丶 提交于 2019-12-13 05:43:07

问题


I'm running a headless linux box with openjdk-8-jdk-armhf and when I run my application, the line

METRICS = new JPanel().getFontMetrics(FONT);

Throws a NullPointerException reaching back to RepaintManager.currentManager(RepaintManager.java:262), which looks like this:

    public static RepaintManager currentManager(Component c) {
       // Note: DisplayChangedRunnable passes in null as the component, so if
       // component is ever used to determine the current
       // RepaintManager, DisplayChangedRunnable will need to be modified
       // accordingly.
       return currentManager(AppContext.getAppContext());
    }

The full trace is as follows:

java.lang.NullPointerException
    at javax.swing.RepaintManager.currentManager(RepaintManager.java:262)
    at javax.swing.JComponent.repaint(JComponent.java:4799)
    at java.awt.Component.repaint(Component.java:3303)
    at javax.swing.JComponent.setFont(JComponent.java:2756)
    at javax.swing.LookAndFeel.installColorsAndFont(LookAndFeel.java:208)
    at javax.swing.plaf.basic.BasicPanelUI.installDefaults(BasicPanelUI.java:66)
    at javax.swing.plaf.basic.BasicPanelUI.installUI(BasicPanelUI.java:56)
    at javax.swing.JComponent.setUI(JComponent.java:664)
    at javax.swing.JPanel.setUI(JPanel.java:153)
    at javax.swing.JPanel.updateUI(JPanel.java:126)
    at javax.swing.JPanel.<init>(JPanel.java:86)
    at javax.swing.JPanel.<init>(JPanel.java:109)
    at javax.swing.JPanel.<init>(JPanel.java:117)
    at lukemoll.MyClass.<init>(MyClass.java:15)

I can reproduce this error both through the jsvc and java commands.


回答1:


Thanks to Gyro Gearless pointing out this answer.

This (Canvas) will also work in headless mode.

I ended up using new Canvas().getFontMetrics(font);, I'm not sure how I got it to work last time but this Works On My Machine(tm).



来源:https://stackoverflow.com/questions/40020442/npe-when-calling-new-jpanel-in-headless-repaintmanager-currentmanager

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!