Exception when making window fullscreen
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:
Code java:
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:
Code java:
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.
Re: Exception when making window fullscreen
In your screen class, when do you set the class gd variable?
Re: Exception when making window fullscreen
What do you mean by the Class gd variable?
Re: Exception when making window fullscreen
Quote:
Originally Posted by
JamEngulfer221
What do you mean by the Class gd variable?
private GraphicsDevice gd;
When do you set that? You seem to be using it in other methods, but I don't see you set it anywhere.
Re: Exception when making window fullscreen
What do you mean by set it? Like:
Code :
public static GraphicsDevice gd;
Or is there something else I am not doing here?
Sorry, I am kind of in-experienced.
Re: Exception when making window fullscreen
You never instantiate it. You never set it equal to an instance of GraphicsDevice. When you use that variable, what Object do you expect it to be using?
Re: Exception when making window fullscreen
Ah, ok. Thanks! Made a quick change to my code, then it was fine!