I get this error when I try and run my program:
Exception in thread "main" java.lang.NullPointerException
at Screen.restoreScreen(Screen.java:74)
at Main.run(Main.java:28)
at Main.main(Main.java:11)
The source code for my two class files is here:
Screen class:
import java.awt.BorderLayout; import java.awt.Color; import java.awt.DisplayMode; import java.awt.Font; import java.awt.GraphicsDevice; import java.awt.GraphicsEnvironment; import java.awt.Window; import javax.swing.JFrame; import javax.swing.JLabel; @SuppressWarnings("serial") public class Screen{ private GraphicsDevice gd; public Screen(){ GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice(); GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment(); } public void setFullScreen(DisplayMode dm, JFrame window){ window.setUndecorated(true); window.setResizable(false); gd.setFullScreenWindow(window); if(dm != null && gd.isDisplayChangeSupported()){ try{ gd.setDisplayMode(dm); } catch (Exception ex) {} } } public Window getFullScreenWindow(){ return gd.getFullScreenWindow(); } public void restoreScreen(){ Window w = gd.getFullScreenWindow(); if(w != null){ w.dispose(); } gd.setFullScreenWindow(null); } }
Main class:
import java.awt.*; import javax.swing.JFrame; public class Main{ JFrame window = new JFrame(); public static void main(String [] args){ DisplayMode dm = new DisplayMode(800,600,16, DisplayMode.REFRESH_RATE_UNKNOWN); Main m = new Main(); m.run(dm); } public void run(DisplayMode dm){ /* setBackground(Color.BLUE); setForeground(Color.WHITE); setFont(new Font("Ariel", Font.PLAIN, 24)); */ Screen s = new Screen(); try{ s.setFullScreen(dm, window); try{ Thread.sleep(5000); } catch (Exception ex){} } finally { s.restoreScreen(); } } public void paint (Graphics g) { g.drawString("Hi! Did this work?", 200, 200); } /* private void setFont(Font font) { // TODO Auto-generated method stub } private void setForeground(Color white) { // TODO Auto-generated method stub } private void setBackground(Color blue) { // TODO Auto-generated method stub } */ }
Through testing I have found that it is most likely something to do with my m.run(); at the start of the Main class.