0
    package macroreader;

    import java.io.BufferedReader;
    import java.io.FileReader;
    import java.io.IOException;

    public class MacroReader {

        public static Macro[] macroArray = new Macro[20];

        public static int macroID;

        public static BufferedReader br;

        public static void main(String[] args) throws IOException {
            br = new BufferedReader(new FileReader("Macros.txt"));
            String currentLine;
            while((currentLine = br.readLine()) != null) {
                if(currentLine.equalsIgnoreCase("#newmacro")) {
                    br.mark(1000);
                    createMacro();
                    br.reset();
                }
            }
            if (br != null) {
                br.close();
            }
        }

    public static void createMacro() throws IOException {
        String currentLine;
        macroID = getEmptyMacro();
        while((currentLine = br.readLine()) != null && !currentLine.equalsIgnoreCase("#newmacro")) {
            macroArray[macroID].readMacro(currentLine);
        }
        macroArray[macroID].inUse = true;
        macroArray[macroID].printData();
    }

    public static int getEmptyMacro() {
        for(int i = 0; i < macroArray.length; i++) {
            if(!macroArray[i].inUse) {
                return i;
            }
        }
        return 0;
    }

}

rather than assigning the values read from the file reader to the specified object in the array, in this case 'macroID', it assigns the values to all objects in the array

just edited in the whole file now but the issue is around the createMacro() void

here is my Macro class

package macroreader;

public class Macro {

    public static String key;
    public static String[] commands = new String[20];
    public static boolean inUse;

    public static void readMacro(String input) {
        if (!input.equals("")) {
            if (input.startsWith("key = ")) {
                key = input.substring(6);
                System.out.println("Key Value for Macro set to " + key);
            } else {
                for (int i = 0; i < commands.length; i++) {
                    if (commands[i] == null) {
                        commands[i] = input;
                        System.out.println("Command [" + input + "] assigned");
                        break;
                    }
                }
            }
        }
    }

    public static void printData() {
        System.out.println("Macro Key: " + key);
        for(int i = 0; i < commands.length; i++) {
            if(commands[i] != null) {
                System.out.println(commands[i]);
            }
        }
    }

}
4
  • What data type is macroArray holding, and how is it defined? I'm not good at guessing. --- could it be that inUse is static? Commented May 24, 2012 at 20:11
  • public static Macro[] macroArray = new Macro[20]; Commented May 24, 2012 at 20:17
  • The code you show can't possibly run to that point, because you are creating a new array without initializing its contents, so you will get a NullPointerException when trying to access the inUse field. (I tested it myself to be sure.) Please show the actual code you are working with. You don't have to include everything — just all of the code that works with macroArray. Commented May 24, 2012 at 20:25
  • Ah, it's static so you don't actually need any instances (and don't have any). Commented May 24, 2012 at 20:31

2 Answers 2

2

As I suspected - your inUse is static, so it will always be the same for all instances of the class. As are your other class members.

Sign up to request clarification or add additional context in comments.

3 Comments

I have changed the values from static. However, now I can't seem to make use of the readMacro method in the Macro class
That's because jou need to us it on an instance: Macro m = new Macro(); m.readMacro("inputstring...");
Thank you mata, i would vote you up but apparently i need 15 repuation... well, atleast you know you helped me out! cheers
2

The classic cause of "changing everything in an array" is if you have actually assigned the same object to every element in the array. We can't tell whether you're doing that because you didn't show us the initialization of macroArray, but it might be like:

Macro m = new Macro();
for (int i = 0; i < macroArray.length; i++) {
  macroArray[i] = m;
}

This will cause the results you describe. To fix it, create a separate object for every element of the array:

for (int i = 0; i < macroArray.length; i++) {
  macroArray[i] = new Macro();
}

1 Comment

I have changed the values from static. However, now I can't seem to make use of the readMacro method in the Macro class

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.