To develop a Tree Structure
<p>Please someone help!!i m a newbie in Java. i want to make a tree structure from an Array List. My input is </p>

<pre><code>1.1
1.2
1.3
1.3.1.1
1.3.1.2
1.4
1.4.1.1
1.4.2.1
</code></pre>

<p>and my aim is to get a Tree like </p>

<pre><code>               1.1
               1.2
               1.3
       1.4           1.3.1.1
 1.4.1.1   1.5          1.3.1.2    
 1.4.1.2
</code></pre>

<p>and so on.  </p>

<p>Please find below my classes for the same. I get a nullPoniter at test.tree.Node.addChild(Node.java:28) and I know it is because the 'children' is null but I dont know how to set the children for the first time. Please Help... :(</p>

<pre><code>public class Tree {
private Node root;

public Tree(String rootData)
{
    root=new Node();
    root.data=rootData;
    root.children=new ArrayList&lt;Node&gt;();
}
public Tree() {
    super();
}
 public Node getRoot(){
    return this.root;
    }
 public void setRoot(Node rootElement) {
    this.root = rootElement;
}
}
</code></pre>

<p>and Node class</p>

<pre><code>class Node { 
String data;
Node parent;
List&lt;Node&gt; children;

public Node() {
    super();
}
public Node(String name)
{
    super();
    this.data=name;
}
public void addChild(String name) {
    this.addChild(new Node(name));
}
public void addChild(Node child) {
    this.children.add(child);
}
public void removeChild(Node child) {
    this.children.remove(child);
}
public void removeChild(String name) {
    this.removeChild(this.getChild(name));
}
public Node getChild(int childIndex) {
    return this.children.get(childIndex);
}
public Node getChild(String childName) {
    for (Node child : this.children) {
        if (child.data.equals(childName)) { return child; }
    }
    return null;
}
public List&lt;Node&gt; getChildren() {
    if (this.children == null) {
        return new ArrayList&lt;Node&gt;();
    }
    return this.children;
}
 public void setChildren(List&lt;Node&gt; children) {
    this.children = children;
}
public Node getParentNode() {
    return this.parent;
}
}
</code></pre>

<p>and the Test class is</p>

<pre><code>public class TreeTest {
public static void main(String[] args) {
    TreeTest tt = new TreeTest();
    ArrayList&lt;String&gt; newArr= new ArrayList&lt;String&gt;();
    newArr.add("1.1");
    newArr.add("1.2");
    newArr.add("1.3");
    newArr.add("1.3.1.1");
    newArr.add("1.3.1.2");
    newArr.add("1.4");
    newArr.add("1.4.1.1");
    newArr.add("1.4.2.1");
    int lCount=0;
    int maxCount= newArr.size();
    Tree tr= new Tree();
    Node rootNode = new Node();
    String parent_name=null;
    Node currentNode= new Node();
    for(String line: newArr){
        if(lCount==0){
        rootNode = tt.getTree(line);
        tr.setRoot(rootNode);
        currentNode= rootNode;
        }
        else{
            List&lt;Integer&gt; cur =  new ArrayList&lt;Integer&gt;();
            List&lt;Integer&gt; pre =  new ArrayList&lt;Integer&gt;();
            cur= tokenize(line);
            pre= tokenize(newArr.get(lCount-1));
            if(cur.size()==pre.size()){

                currentNode.addChild(tt.getTree(line));
                currentNode= tt.getTree(line);
                }
            else if (cur.size()&gt;pre.size()){
                currentNode.addChild(tt.getTree(line));
                parent_name= newArr.get(lCount-1);
                currentNode= tt.getTree(line);
                }
            else if(cur.size()&lt; pre.size()){
                currentNode= tt.getTree(parent_name);
                currentNode.addChild(tt.getTree(line));
                currentNode= tt.getTree(line);
            }
        }

        lCount++;
        }
        }
private Node getTree(String string) {
    // TODO Auto-generated method stub
        Node rootNode = new Node(string);
        return rootNode;
    }
private static List&lt;Integer&gt; tokenize(String line) {
    // TODO Auto-generated method stub
    List&lt;Integer&gt; line_Arr =  new ArrayList&lt;Integer&gt;();

    String[] tokens = line.split("\\.");
    int i=0;
    for(String atr: tokens)
        line_Arr.add(Integer.parseInt(atr));

    return line_Arr;
}
</code></pre>

<p>}</p>
