简体   繁体   中英

Infinite while loop with an iterator, hasNext() and next()

I am working on a Java project, which uses Apache POI to manipulate .docx-files. Two days ago I experienced a problem, which I am not able to solve. I don't really know, where my error is, and what causes it. The only thing I know is, what the bug is doing.

Two days ago, everything worked fine. I chose my .docx, clicked the radio buttons, which define, what change will be executed, and then I clicked on my "submit" button to start this job. It worked like this: Get all the text from the .docx with Apache POI methods, iterate through every Run, change the keyword and the grammatic, and put the updated Paragraph with its runs back into the document. That process repeats for the changes from the second radio button. Finally the document, with the changes from both radio buttons, gets saved with a suffix in its filename. If I now choose one special radio button, the manipulation runs successfully through the whole document. But then, during the manipulation of the second run (which is defined by the second radio button) it looks like the execution freezes at a specific point, because the Eclipse console doesn't show any changes. In fact, if I terminate the Application, and look into the resulting document, I will see, that the same piece of text is pastet over and over into the document. The document is about 11 pages long. This buggy pasting increases its size by over 330 pages in a few seconds. This leads me to think, that there must be an infinite loop. Anything else doesn't make much sense to me.

The strange thing is, that I never changed anything on that algorithm (I think). One of the last things I did was updating my Java version to 8u45, but since I rolled back to my previous version, and the error still persists, I doesn't look like the update caused the error somehow.

I hope one of you guys has some advice for me. I don't think, that posting my code here is useful, because it is nearly a full copy of the other methods, which work fine, but I will do it anyways. This peace of code is the only part (after chosing the buggy radio button), where I use a while loop. All other loops are for-loops. What confuses me is:

  • What is the first radion buttong (the buggy one) doing, so that the execution of the following change ends in an infinite loop?
  • If it is an infinate loop, how does it write in my document? As you see I only write the content into the document, when I leave the while loop. When I terminate it by hand in Eclipse, there shouldn't ne any text in the document.

     public static void changeSingleMaleToMultiMale(String path, String title, XWPFDocument document) throws Exception { XWPFDocument oldDoc = document; Iterator<XWPFParagraph> iterator = document.getParagraphsIterator(); int length = title.length(); int counter = 0; while(iterator.hasNext()) { XWPFParagraph paragraph = iterator.next(); System.out.println("____ unchanged\\n" + paragraph.getText() + "\\n____\\n\\n"); List<XWPFRun> runs = paragraph.getRuns(); for(int i = 0; i < runs.size(); i++) { String text = runs.get(i).toString(); if(text.contains(title)) { runs.get(i).setText(StringFunctions.fromSingleMaleToMultiMale(text, title, length), 0); } } System.out.println("____ updated\\n" + paragraph.getText() + "\\n____\\n\\n"); document.setParagraph(paragraph, counter); counter++; } try { FileOutputStream output = new FileOutputStream(path.substring(0, path.length()-5) + "Aktualisiert.docx"); document.write(output); output.close(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } 

Has anyone experienced something similar? I would be glad for every little hint to solve this.

UPDATE: The buggy code, which is called by choosing the mentioned radio button does not affect the methods, which are called by the other radio buttons. It just looks like this. In fact the buggy peace of code writes such a huge text into my variable, and at this point I think, that the output stream is still busy writing that huge text into my document, while the following methods are trying to access the document. So far, that answers the first question of the list above. The other question and the problem still remains open.

Some days ago I found the solution for this.

There was no infinite loop, even if it looks like this, because the same piece of text got pasted thousands of times into my document.

After some desperating time, trying to find out, why my loop does not work correctly, I decided to swallow the bitter pill and try to find out, step by step, what is causing the error.

So I put most of the code into comments and uncommented every piece of code, step by step. I found out, that the loop was correctly implemented and the bug was hidden in a method, which helps replacing some text in the document.

I got something like this:

String checkString = "";

as an simple initial value for my variable.

In some special cases "checkString" would get a String, in some other cases it should be left as an empty String.

The error was in handling the second case. I have an if-clause which is defined like that:

if(baseString.contains(checkString))

Do you see here, what could happen if "checkString" is an empty String? Yes, of course, "baseString" contains "", in any case. That if-clause was true, everytime. The other lines in this if-clause did some work, which led to what looks like an infinite pasting of text.

if(checkString != "" && baseString.contains(checkString))

fixed everything.

The way the error manifested itself, led me to think, that the bug was everywhere, but not in that method. It's funny how those simple bugs make me so mad.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM