Question

Ok, so as a little personal programming project I had been trying to make a program in the past which would (via a GUI) held to record data of item drops from certain stuff in a game (Guild Wars 2 if you're curious). The GUI is simple enough: JFrame containing two JLabels and JTextFields, to say what to input and well, the text fields to type it in. Basic GUI stuff. The inputs are the item name and the amount. Also, there are two JButtons, with a listener each to add items and subtract items and finally another JLabel to function as error message display.

The structure of the "mapping" of items to amount obtained is stored during the execution of the java program in a TreeMap.

I had been creating a crude, basic implemention, saving data to memory in a .csv file but I decided last night I wanted to rework the system. Thus, I got the idea of trying to "write" to a spreadsheet format, and I thought, "well why not OpenOffice? Consequently, I discovered the ODFToolkit API and such, specifically the ODFDOM tools have been of interest to me of course. So I had thought I had this stuff refactored (if that's the right term) well enough and that things would go smoothly but...here's my problem:

So, in the GUI I type some test input; "Rotten Egg" is a valid item (invalids make an error display in the window) and I put some positive integer (only possibly type of valid "amount") for the amount obtained. Now, the first time I do this, with a valid input like this it works fine, but if I try again with different input or the same it displays the error text that should show only when an invalid item is entered. I tried a few things at least to debug it looking through it, but I am at a loss as to how this horrible bug has been created.

I will link to pastebin for my own two classes here:
GUI class: http://pastebin.com/rEbFS27j
Other class i.e. the Map structure and other stuff going on "behind" the GUI: http://pastebin.com/BWA4SUY0

Was it helpful?

Solution

sigh I figured it out eventually. I have this loop in the method that writes to the spreadsheet.

while (it.hasNext() == true) {
            Map.Entry pairs = (Map.Entry)it.next();

            odt.getTableList().get(0).getCellByPosition("A" + i).setDisplayText((String) pairs.getKey()) ;
            odt.getTableList().get(0).getCellByPosition("B" + i).setDisplayText((pairs.getValue().toString())) ;
            it.remove(); // avoids a ConcurrentModificationException
            i++ ;
            }

The line it.remove() ("it" is an Iterator variable) is a fatal error...an unfortunate remnant from a copy-paste when I looked up stuff in the past about how to use iterators... :/ Indeed it was the cause of this horrid "only works once" kind of bug. Guess I just really need to make sure I'm not using something wrong or unnecessary when I paste snippets of someone else's code into my work...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top