title: How to assign values to an ArrayList in a class?
body: <p>I have been having trouble for the longest time on figuring out why you seemingly cannot assign values to an ArrayList without getting an error.</p>
  
  <p>The first block of code is the main method which gets line by line from a textfile and splits the line using a delimiter. The first string is stored in one variable as a long, then other 9 strings are stored in an ArrayList. It does this for every line in the file.</p>
  
  <p>I have debugged this code many times and it shows that the array is getting the right values. </p>
  
  <p>The issues is when the code reaches the part where it calls insert, which I have commented in the code.</p>
  
  <p>It first goes to create the node, but when it reaches the part when it is going to add the values from the first ArrayList to the newly created ArrayList, everything breaks. The for loop stops functioning as it should and it continues to increment even though the limit was reached.</p>
  
  <p>I have decided to omit the BinaryTree Class which I also use for this project because it works as it should. </p>
  
  <p>So how would I go about properly assigning the values from the ArrayList that I pass to the Node ArrayList?</p>
  
  <pre><code>package assignment7;
  import java.io.BufferedReader;
  import java.io.FileReader;
  import java.io.IOException;
  import java.io.PrintWriter;
  import static java.lang.Long.parseLong;
  import java.util.Scanner;
  import java.util.ArrayList;
  
  public class Assignment7 {
  
      public static void main(String[] args) throws IOException {
  
      boolean headerLine = true;
      String firstLine = "";
      String catchLine;
      String token;
      long s_cid;
      ArrayList&lt;String&gt; Arr = new ArrayList&lt;String&gt;();
      Scanner delimS;
      int count = 0;
      BinaryTree snomedTree = new BinaryTree();
  
      try(BufferedReader br = new BufferedReader(new FileReader("Data.txt"))) {
  
          while ((catchLine = br.readLine()) != null) {
  
              for(int i = 0; i &lt; 9; i++){
  
                  Arr.add("");
  
              }
  
              if(headerLine){
  
                  firstLine = catchLine;
                  headerLine = false;
  
              }
  
              else{
  
                  delimS = new Scanner(catchLine);
                  delimS.useDelimiter("\\|");
                  s_cid = parseLong(delimS.next());
  
                  while(delimS.hasNext()){
  
                      token = delimS.next();
                      Arr.set(count, token);
                      count++;
  
                  }
  </code></pre>
  
  <p>//Runs fine up to here then this insert function is called</p>
  
  <pre><code>                  snomedTree.insert(new Node(s_cid, Arr));
  
              }
  
              Arr.clear();
  
          }
      }
  
      try (PrintWriter writer = new PrintWriter("NewData.txt")) {
  
          writer.printf(firstLine);
          snomedTree.inorder(snomedTree.root, writer);
  
      }
  }
  
  }
  </code></pre>
  
  <p>Here is the Node class:</p>
  
  <pre><code>package assignment7;
  
  import java.util.ArrayList;
  
  
  
  class Node {
  
  public long cid;
  public ArrayList&lt;String&gt; Satellite;
  public Node l;
  public Node r;
  
  public Node(long cid, ArrayList&lt;String&gt; Sat) {
      this.Satellite = new ArrayList&lt;String&gt;();
      this.cid = cid;
  </code></pre>
  
  <p>//This for loop continues to run even after i = 9
          for(int i = 0; 0 &lt; 9; i++){</p>
  
  <pre><code>        Satellite.add(Sat.get(i));
  
      }
  
  }
  
  }
  </code></pre>
  
  <p>And this is the Binary Tree class:</p>
  
  <pre><code>package assignment7;
  import java.util.ArrayList;
  import java.io.PrintWriter;
  
  
  public class BinaryTree {
  public Node root;
  public BinaryTree() {
      this.root = null;
  }
  public void insert(Node x) {
      if (root == null) {
          root = x;
      } 
  
      else {
          Node current = root;
          while (true) {
  
              if (x.cid &gt; current.cid) {
                  if (current.r == null) {
                      current.r = x;
                      break;
                  } 
  
                  current = current.r;
              }
  
              else {
                  if (current.l == null) {
                      current.l = x;
                      break;
                  } 
                  current = current.l;
              }
          }
  
      }
  }
  
  public void inorder(Node x, PrintWriter out) {
  
      if (x != null) {
  
      inorder(x.l, out);
          out.printf(String.valueOf(x.cid) + "|");
          for(int i = 0; i &lt; 9; i++){
  
              if(i != 8){
  
                  out.printf(x.Satellite.get(i) + "|");
  
              }
  
              else{
  
                  out.printf(x.Satellite.get(i) + "\n");
  
              }
          }
          inorder(x.r, out);
      }
  
  
  }
  }
  </code></pre>