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]);
}
}
}
}
macroArrayholding, and how is it defined? I'm not good at guessing. --- could it be thatinUseisstatic?NullPointerExceptionwhen trying to access theinUsefield. (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 withmacroArray.