title: Why does this program throw an exception while using threads?
body: <p>I'm trying to create threads that add one to a variable named sum each time. Why am I getting an exception. Also if possible, could someone let me know why the threads are occurring in order? Sorry, very very new to threads.</p>
  <p>This is the code for the task class:</p>
  <pre><code>public class AddOneToSum extends Thread
  {
      private int sum = 0;
  
      @Override
      public void run()
      {
          try
          {
              System.out.println( this.getName() + &quot; : Sum is &quot; + sum );
              ++sum;
          }
      
          catch (Exception e)
          {
              e.printStackTrace();
          }
      }
  }
  </code></pre>
  <p>This is the code for the main method class:</p>
  <pre><code>import java.util.*; 
  
  public class Assignment9
  {
      public static void main(String[] args)
      {
          ArrayList&lt;Thread&gt; arrayOfThreads = new ArrayList&lt;Thread&gt;();
      
          try
          {
              for ( int i = 0 ; i &lt; 50 ; i++ )
              {
                  arrayOfThreads.add( new AddOneToSum() );
                  i++;
              }
      
              for ( int i = 0 ; i &lt; 50 ; i++ )
              {
                  arrayOfThreads.get(i).start();
              }
          }
      
          catch (Exception e)
          {
              e.printStackTrace();
          }
      }
  }
  </code></pre>
  <p>Thanks!</p>