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

I am working on a Java project that uses Apache POI to manipulate .docx files. Two days ago I ran into a problem that I could not solve. I really don't know where my error is and what is causing it. The only thing I know is what the error is doing.

Everything worked fine two days ago. I selected my .docx, clicked on the radio buttons that determine what changes will be made, and then I clicked the submit button to get this working. It worked like this: Get all text from .docx using Apache POI methods, step through each launch, change keyword and grammar, and put the updated Paragraph with its results into the document. This process is repeated for changes from the second switch. Finally, the document with changes from both radio buttons is saved with a suffix in the file name. If I now select one special radio button, the manipulation goes through the entire document successfully. But then, during manipulation of the second run (which is determined by the second switch), it looks like the execution hangs at a certain point,because the Eclipse console doesn't show any changes. In fact, if I terminate the application and look at the resulting document, I see the same piece of text being inserted over and over again into the document. The document is about 11 pages long. This buggy attachment increases its size by over 330 pages in a matter of seconds. This makes me think there must be an endless loop. Something else doesn't make any sense to me.Something else doesn't make any sense to me.Something else doesn't make any sense to me.

It's strange that I have never changed anything with this algorithm (I think). One of the last things I did was upgrade my Java version to 8u45, but since I reverted to my previous version and the error still persists, I don't look like the upgrade caused the error somehow.

Hopefully one of you, me, has some advice. I don't think posting my code here is helpful because it's almost a complete copy of the other methods that work fine, but I'll do it anyway. This world of code is the only part (after selecting the buggy radio button) where I use a while loop. All other loops are for loops. Confuses me:

  • What is the first radion sound (buggy) so that the next change ends up in an infinite loop?
  • If this is an input loop, how is it recorded in my document? As you can see, I am only writing text in the document when I leave the while loop. When I complete it manually in Eclipse, there should be no 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 of this.

UPDATE: The error code called when the mentioned radio button is selected does not affect methods called by other radio buttons. It looks like this. Actually the buggy world of code is writing such a huge text to my variable, at which point I think the output stream is still busy writing huge text in my document while the following methods are trying to access the document. So far, this answers the first question from the list above. Another question and problem is still open.

+3


source to share


1 answer


I found a solution for this a few days ago.

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

After some desperate time trying to figure out why my cycle is not working properly, I decided to digest a bitter pill and try to figure out step by step what is causing the error.

Thus, I put most of the code in comments and without comments from each part of the code, step by step. I found out that the loop was correctly implemented and the error was hidden in the method, which helps replace some text in the document.

I have something like this:

String checkString = "";

      

as a simple seed for my variable.



In some special cases "checkString" will receive 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 that is defined like this:

if(baseString.contains(checkString))

      

Do you see what happens here if "checkString" is an empty string? Yes, of course "baseString" contains "" anyway. This if-true condition was true every time. The other lines in this if-clause did some work, resulting in what looks like an infinite nesting of text.

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

      

fixed everything.

The way the error manifested made me think that the error was all over the place, but not in this method. It's funny how these simple mistakes make me so insane.

+1


source







All Articles