title: Not able to get the Threadlocal value
body: <p>I am trying to print numbers 0,1, 2 and so on using the three different threads in below order by using threadlocal variables :</p>
  
  <pre><code>0- thread 0
  1- Thread 1
  2- Thread 2
  3- thread 0
  4- Thread 1
  .
  .
  </code></pre>
  
  <p>Below is the code where i am putting an integer in the threadlocal storage and comparing it with the atomic integer which gets incremented by every thread. </p>
  
  <pre><code>  public class alternate123 
  {
  
  public static void main(String as[])
  {  
      Object ob = new Object ();AtomicInteger t = new AtomicInteger(0);
  
      Thread t1 = new Thread ( new printpattern(t,0),"t0");
      Thread t2 = new Thread ( new printpattern(t,1),"t1");
      Thread t3 = new Thread ( new printpattern(t,2),"t2" );
  
      t1.start();
      t2.start();
      t3.start();
  
  
  
  }
  
  }
  class printpattern implements Runnable
  {
      //Integer t ;
      //Object ob = new Object ();
      AtomicInteger ai;
      private  ThreadLocal&lt;Integer&gt; t = new ThreadLocal&lt;Integer&gt;() ;
      public printpattern(AtomicInteger at,Integer i)
      {
          //
          ai=at;
          t.set(i);
      }
      @Override
      public void run()
      {
          // TODO Auto-generated method stub
  
          synchronized (ai) { 
          while (true)
          {if (ai.get()%3==
              t.get()) // ----------------------- null pointer exception here 
              {
                  try {
                      ai.wait();
                  } catch (InterruptedException e) {
                      // TODO Auto-generated catch block
                      e.printStackTrace();
                  }
              }
              try {
                  Thread.sleep(300);
              } catch (InterruptedException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
              }
              System.out.println("Current thread id "+Thread.currentThread().getName()+"value of integer is "+ai.get());;
              ai.incrementAndGet();
              ai.notify();
          }
      }
      }
  
  }
  </code></pre>
  
  <p>i am getting null while trying to do t.get() and below is the stacktrace :</p>
  
  <pre><code>Exception in thread "t2" java.lang.NullPointerException
      at printpattern.run(alternate123.java:46)
      at java.lang.Thread.run(Thread.java:619)
  Exception in thread "t0" java.lang.NullPointerException
      at printpattern.run(alternate123.java:46)
      at java.lang.Thread.run(Thread.java:619)
  Exception in thread "t1" java.lang.NullPointerException
      at printpattern.run(alternate123.java:46)
      at java.lang.Thread.run(Thread.java:619)
  </code></pre>
  
  <p>i am setting threadlocal variable using set method which i doubt is erroneous . Please let me know what could be the issue .</p>