I am writing an application that runs in a headless environment and should output to a BufferedImage instead of a screen. I have a Display class managing a BufferedImage . My application extends JPanel and in order to automatically redraw it when the component is updated, I re-executed repaint() as:
public void repaint(){ Graphics2D g = getDisplay().getGraphics(); paint(g); getDisplay().repaint(); }
Whenever I run my application, I get a NullPointerException when it tries to draw a Display . This is supposedly some code in the JPanel constructor that is trying to redraw. The problem is that getDisplay() returns null. However, Display has already been created and transferred to the application at this point. I checked this when Display printed its own properties on creation before sending it to the application.
The exception is the following: the topmost location refers to the line containing getDisplay() :
Exception in thread "main" java.lang.NullPointerException at com.mypapyri.clay.ui.App.repaint(App.java:28) at javax.swing.JComponent.setFont(JComponent.java:2746) 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:655) 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 com.mypapyri.clay.ui.App.<init>(App.java:18) at ClayOS.<init>(ClayOS.java:22) at ClayOS.main(ClayOS.java:84)
EDIT: I investigated this and could not find a satisfactory resolution.
source share