0

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
  • Please include the code for `MainWindow.updateTextBoxes()` and point out line 143 in that code. – gla3dr Sep 04 '15 at 22:55
  • Did you read the error message ? – Dici Sep 04 '15 at 22:58
  • Down/Close voters, please read the entire post. The NPE appears to be incidental (or a subsequent error). The OP is asking why `Beginning loop of nullvalues, of size 2` but the loop executes only once before dropping out to call `updateTextBoxes()` – Jim Garrison Sep 04 '15 at 23:00
  • Please step through your code in an IDE debugger. If you are not using an IDE, get one ASAP (Eclipse, NetBeans, etc). Debugging code with print statements is so 1980's :-) – Jim Garrison Sep 04 '15 at 23:02

1 Answers1

1

Inside EquationLoop you are modifying the list nullvalues, which is being iterated over by the outer loop. This is undefined behavior, you are not allowed to modify a collection while iteration is in progress.

I'm surprised you didn't get ConcurrentModificationException instead.

Jim Garrison
  • 81,234
  • 19
  • 144
  • 183