Search code examples
java2d-games

ArrayIndexOutOfBoundsException 2D game Development


i was following this tutorial but i don't know where is the error the following 2 class are explaining every thing because it's only two class i tried to watch the tutorial again but still i did not find the error

    /*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package gameofthrones;

import java.awt.*;
import javax.swing.*;

/**
 *
 * @author issba
 */
public class ClassOGP extends JFrame{
    boolean fse =false;
    int fsm = 0;
    GraphicsDevice device = GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices()[1];
    public ClassOGP(String title,int width,int height){
        setTitle(title);
        setSize(width,height);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setResizable(false);


    }

    private void setfullscreen(){
        switch(fsm){
            case 0:
                System.out.println("No fullscreen");
                setUndecorated(false);
            break;
            case 1:
                setExtendedState(JFrame.MAXIMIZED_BOTH);
                setUndecorated(true);
            break;
            case 2:
                device.setFullScreenWindow(this);
                setUndecorated(true);
            break;
        }

    }

    public void setFullscreen(int fsnm){
           fse = true;
           if(fsm <= 2){
           this.fsm = fsnm;
           setfullscreen();
           }else{
           System.err.println("Error " + fsnm + " is not supported");
           }
        }
    }

this is the main class there is no much code in it.

    /*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package gameofthrones;


/**
 *
 * @author issba
 */
public class Main {

    /**
     * @param args the command line arguments
     */

    public static void main(String[] args) {
        ClassOGP frame = new ClassOGP("Game Of thrones",1280,720);
        frame.setFullscreen(1);
        frame.setVisible(true);
    }

}

the error message here

    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
    at gameofthrones.ClassOGP.<init>(ClassOGP.java:18)
    at gameofthrones.Main.main(Main.java:20)
C:\Users\issba\AppData\Local\NetBeans\Cache\8.1\executor-snippets\run.xml:53: Java returned: 1
BUILD FAILED (total time: 1 second)

Solution

  • This line:

    GraphicsDevice device = GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices()[1]; 
    

    Is throwing the error. Try changing the 1 to a 0.

    That is a just a quick fix though, you should declare the device as an instance or class member and assign it in the constructor. Then you can do error checking if there are no screen devices. Like the following:

    public class ClassOGP extends JFrame{
        /* other code */
    
        public GraphicsDevice device;
    
        public ClassOGP(String title,int width,int height) {
            if(GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices().length() > 0) {
                // you can also check for multiple devices here to see if you want
                // to use one other than the zero'th index
                device = GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices()[0];
            } else {
                System.out.println("ERROR: No devices ... exiting.");
                System.exit();
            }
    
            /* other code */
        }
    
        /* rest of class */
    }