Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Exception when making window fullscreen

  1. #1
    Member
    Join Date
    Oct 2011
    Posts
    37
    My Mood
    Where
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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:

    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.
    Last edited by Freaky Chris; November 2nd, 2011 at 04:41 AM.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Exception when making window fullscreen

    In your screen class, when do you set the class gd variable?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Member
    Join Date
    Oct 2011
    Posts
    37
    My Mood
    Where
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Exception when making window fullscreen

    What do you mean by the Class gd variable?

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Exception when making window fullscreen

    Quote Originally Posted by JamEngulfer221 View Post
    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.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Member
    Join Date
    Oct 2011
    Posts
    37
    My Mood
    Where
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Exception when making window fullscreen

    What do you mean by set it? Like:
    public static GraphicsDevice gd;
    Or is there something else I am not doing here?

    Sorry, I am kind of in-experienced.

  6. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default 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?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    Member
    Join Date
    Oct 2011
    Posts
    37
    My Mood
    Where
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Exception when making window fullscreen

    Ah, ok. Thanks! Made a quick change to my code, then it was fine!

Similar Threads

  1. How to activate fullscreen in a different class?
    By JamEngulfer221 in forum AWT / Java Swing
    Replies: 7
    Last Post: November 1st, 2011, 12:32 PM
  2. I need help with my FullScreen Code
    By JamEngulfer221 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 31st, 2011, 06:21 AM
  3. Applet and Fullscreen !!
    By rt4ever in forum Java Applets
    Replies: 21
    Last Post: September 5th, 2011, 07:43 PM
  4. Craating non active window ,and inputing to the non active window.
    By java-beginner in forum Java Theory & Questions
    Replies: 0
    Last Post: February 25th, 2011, 10:13 PM
  5. FullScreen isn't working in Win 7
    By Ahmed.Ibrahem in forum AWT / Java Swing
    Replies: 0
    Last Post: February 6th, 2011, 11:48 AM