I've been writing a simple JFrame program to solve basic math equations, the program takes 5 values, up to two of which may be null, identifies the correct equation and calculates the null values. This section of code below takes the 5 values in a hashmap, identified by a string (V,U,S,A,T), identifies which ones are null and adds them to a seperate array (nullvalues), which is then iterated through (nullValueLoop) to identify a suitable equation and solve the unknown.
The problem arises in nullValueLoop, it only appears to iterate once, stopping abruptly after the first iteration.
I've added a lot of system.out's while trying to debug this, and have included the console output to hopefully show whats going on. The exception called is from another class (as the program continues) attempting to call the second null value and finding it to be null.
Hopefully i've explained this well, any help is much appreciated.
public void calculate() {
// Stores the keys to reference the null values in HashMap<Double> values;
ArrayList<String> nullvalues = new ArrayList<String>();
for(String tempKey : values.keySet()){
if(values.get(tempKey)==null){
nullvalues.add(tempKey);
System.out.print("Found null value " + tempKey + ", adding to array. \n");
}
}
System.out.print("Beginning loop of nullvalues, of size " + nullvalues.size() + "\n");
nullValueLoop:
for(String nullvalue : nullvalues){
System.out.print("Starting outerloop, iterating nullvalue " + nullvalue + "\n");
EquationLoop:
for(Equation e : registeredEquations){
// Flag to keep ` of if we have any unknowns yet
boolean foundUnknown = false;
// Iterate through the values required
// If the loop does not exit, the equation only requires one of our null values and the program continues.
for(String s : e.getRequiredChars()){
// If we have a null value and havent yet had one, all is good
if(nullvalues.contains(s) && foundUnknown == false){
foundUnknown = true;
// We have more than one null value, abort
} else if(foundUnknown == true && nullvalues.contains(s)){
continue EquationLoop;
}
}
System.out.print("Using equation " + e.getIdentifier() + "\n");
System.out.print("Found suitable equation.\n");
Double returnValue = e.calculate(values, nullvalue);
System.out.print("Calculated return value.\n");
values.put(nullvalue, returnValue);
nullvalues.remove(nullvalue);
System.out.print("Added new value to values array\n");
System.out.print("Calculated value " + nullvalue + " to " + values.get(nullvalue) + "\n");
break EquationLoop;
}
System.out.print("Ending outerloop iteration \n");
}
mainWindow.updateTextBoxes();
}
Console output:
Couldn't load value T | Assigning to null
Couldn't load value A | Assigning to null
Found null value T, adding to array.
Found null value A, adding to array.
Beginning loop of nullvalues, of size 2
Starting outerloop, iterating nullvalue T
Using equation 3
Found suitable equation.
Calculated return value.
Added new value to values array
Calculated value T to 10.0
Ending outerloop iteration
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at uk.co.ElliotPurvis.MainWindow.updateTextBoxes(MainWindow.java:143)
at uk.co.ElliotPurvis.Main.calculate(Main.java:136)
at uk.co.ElliotPurvis.MainWindow.actionPerformed(MainWindow.java:118)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
Process finished with exit code 0