I created a function that saves a file with a custom extention ".nra"

however each time that i enter a file name, it throws ArrayIndexOutOfBoundsException

public static void SaveFileNra() throws Exception
{

    if(gl.DataStack.isEmpty())
    {
        System.out.println("There is nothing to save!\n");
        Menu();
    }

    else
    {
        System.out.print("Enter the directory you wish to save to (including final slash): ");
        gl.Directory = new Scanner(System.in).nextLine();

        File file = new File(gl.Directory + gl.RomName + ".rna");

        if(file.exists())
        {
            System.out.println("Would you like to Overwrite " + gl.Directory + gl.RomName + ".rna?");
            gl.m = new Scanner(System.in).nextInt();

            if(gl.m != 1)
            {
                System.out.println("Sorry! Cannot make a file that already exists!\n");
                Menu();
            }
            else
            {
                file.delete();
                file.createNewFile();
            }
        }
        file.createNewFile();

        FileWriter fstream = new FileWriter(file);
        BufferedWriter bw = new BufferedWriter(fstream);

        if(gl.IncludeTextMacro = true)
        {
            for(gl.k = 0; gl.k < gl.MacroStack.size();gl.k++)
            {               
                bw.write("; " + gl.MacroStack.elementAt(gl.k));
            }
        }

        for(gl.l = 0; gl.l < gl.DataStack.size(); gl.l++)
        {
            bw.write(gl.DataStack.elementAt(gl.l));
        }

        System.out.println("\nFile " + gl.RomName + " was sucsessfully saved!");
        Menu();
    }

}

THE GL CLASS SO PEOPLE ARN"T CONFUSED

class globals
{
    public boolean PadWithZeroes;
    public boolean SkipUnknownOpcodes;
    public boolean IncludeTextMacro;

    public Stack<String> DataStack = new Stack<String>();
    public Stack<String> MacroStack = new Stack<String>();

    public int i, j, k, l, ix, LineNum, ch;

    public int CurrentLocationDec = 0;

    public int RomSize;
    public String RomName;
    public String Directory;

    public int m;
} 

Ideas?

What statement does the exception occur on? what is the value of the index when it occurs?

The code needs to test array indexes and not use any that are past the end of the array.

Remember array indexes are zero based: 0 to the length-1

Well, the first thing the function does is test if it's empty. the for loops should not

try to save at a number higher than the maximum size of the stack, however, i think that it is

still trying to save at a number higher than the number of items in the stack for some reason.

note that in my for loops:

for(gl.l = 0; gl.l < gl.DataStack.size(); gl.l++)

the for loop should stop when gl.l is greater than DataStack's size.

however i think that it is still trying to continue for some odd reason.

You missed these questions:
What statement does the exception occur on? what is the value of the index when it occurs?

Why are you using a variable from the gl object to control the loop instead of a local variable defined in the for statement?

sorry. it occurs on line 282 of my program or in this case:

 bw.write("; " + gl.MacroStack.elementAt(gl.k));

as for the indeex, i would assume 3, seeing as when i tested the program, i fed the stack:
2 lines of text, and a blank line

What is the value of the index when the exception happens?
Print out the value of gl.k to see. You can't assume, you need to know.

Why are you using gl.k instead of a local loop variable?

This article has been dead for over six months. Start a new discussion instead.