user7164767 user7164767 - 19 days ago 8
Java Question

Java program terminates in first recursion level white initializing a String

EDIT 2: Solved. Nothing special, just a wrong for-loop. Sorry.

This is my first question here. I am currently developing a GUI for easier creation of a config.xml for a certain importer software and I have basically written a data structure for XML with certain restrictions. Long story short, I tried to write a method as an alternative to toString which recursively prints all my data well-formatted. And well, as long as I don't get into the recursion everything goes just as planned. But as soon as I enable it...

This is the code:

private String details(boolean recursiveDetails, int tabs) {

if(recursiveDetails) System.out.println("DEBUG: launched details method with depth="+tabs);
String toStr = space(tabs)+"|============ <"+name+"> ============\n";
//recursion never reaches this point.
if(recursiveDetails) System.out.println("Debug check 1: before recursion with depth="+tabs);
int endlength = 28+name.length();
toStr += (space(tabs)+"| Parent: "+ parent +"\n");
if(contentString!=null && !contentString.equals(""))
toStr += (space(tabs)+"| Content String: \n| " + contentString+"\n");
toStr += (space(tabs)+"| Attributes: " + ((allAttr==null||allAttr.size()==0) ? "---" : "") + "\n");
for(Attribute a : allAttr)
toStr+=(space(tabs)+"| "+a+"\n");
toStr += (space(tabs)+"| Content: " + ((content==null || content.size()==0) ? "---" : "") + "\n");
//recursion!
for(Element e : content)
toStr+=(space(tabs)+"|" + (recursiveDetails ? e.details(true,tabs+1) : (" "+e)) + "\n");
if(recursiveDetails) System.out.println("Debug check 2: after recursion with depth="+tabs);
toStr += space(tabs)+"|";
for(int i=0; i<endlength; i++)
toStr+="=";
return toStr;
}

private String space(int tabs) {
String res="";
for(int i=0; i<tabs; tabs++)
res+=" ";
return res;
}


recursiveDetails
is true when going for recursion,
tabs
is simply for indenting.
content
is a java.util.List of the same type as the class this method is in (
Element
), containing a variable amount of Element-objects. Every Element and Attribute object has a valid
toString()
method.

If I run the method with
recursiveDetails = false
on a small test structure, I get:

|============ <Catalog> ============
| Parent: <ImportConfig>
| Attributes:
| [Str | Project="null"]
| [Int | Priority="null"]
| Content:
| <Entry>
| <Entry>
|===================================


But once I run it with
recursiveDetails = true
I get this:

DEBUG: launched details method with depth=0
Debug check 1: before recursion with depth=0
DEBUG: launched details method with depth=1


Using breakpoints, I found out that everything works perfectly well, until the first line after the println. If I comment that line out, the program terminates at the next line and so forth. No Exceptions, no anything, no more printing.

I have tried using StringBuilder instead, replacing all
+
with
.append()
. I have also tried avoiding the
a ? b : c
operator, using classic
if
s instead. The results are exactly the same.

Can anyone explain this to me?

EDIT: I am running Eclipse Java Neon on Ubuntu 16.04 Xenial with Java 8 OpenJDK amd64.

Answer

The loop in the spaces method is wrong. You have:

for(int i=0; i<tabs; tabs++)

and you almost certainly want to replace tabs++ with i++. Otherwise your loop will run indefinitely

Comments