0

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.

Luke Moll
  • 428
  • 6
  • 18
  • Does setting the system property `System.setProperty("java.awt.headless", "true");` helps ? – Robin Oct 13 '16 at 12:03
  • @Robin I thought I had added that somewhere in my code, I'll try that (I'll be 5 minutes, need to SFTP it) – Luke Moll Oct 13 '16 at 12:04
  • @Robin appears not to have done anything :/ – Luke Moll Oct 13 '16 at 12:12
  • For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). – Andrew Thompson Oct 13 '16 at 12:23
  • @AndrewThompson neither of those are applicable as it's a specific combination of hardware and software causing the error, simply calling the `new JPanel()` constructor in this environment causes this error. – Luke Moll Oct 13 '16 at 12:32
  • Sure, you want to create an JPanel without having an display - how should this work? What are you trying to do? – Gyro Gearless Oct 13 '16 at 12:37
  • @GyroGearless I'm generating images with text in them, I need `FontMetrics` to measure the width of the text to create the `BufferedImage` of the right size. I need to create a `JComponent` to acquire the `FontMetrics` class – Luke Moll Oct 13 '16 at 12:38
  • *"neither of those are applicable"* I would agree with you if it were a [`HeadlessException`](http://docs.oracle.com/javase/8/docs/api/java/awt/HeadlessException.html). – Andrew Thompson Oct 13 '16 at 12:38
  • You might check the last answer here: http://stackoverflow.com/questions/2843601/java-fontmetrics-without-graphics – Gyro Gearless Oct 13 '16 at 12:44
  • @GyroGearless thanks, that solved it! I'll put that into an answer so this question gets closed. – Luke Moll Oct 13 '16 at 13:16

1 Answers1

0

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).

Community
  • 1
  • 1
Luke Moll
  • 428
  • 6
  • 18