title: Displaying an auto-increment number on android
body: <p>The goal of my program is pretty simple. It is to display an integer that adds two to itself every second.<br>
  Since, some of the answers get updated by latest methods, I have tried many, alas, none of them work. I have tried Timer, Handler the while loop, executor etc. With timer I get Timer-0 error, Executor runs only once, and Handler wants me to declare the auto-incremented variable to be Final. I do want the number to be incremented for an hour or endlessly if possible. I am showing one of the options I have tried here.</p>
  
  <pre><code>public class stats extends AppCompatActivity {
  
  
      TextView textView2;
      TextView textView1;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_stats);
          textView2 = findViewById(R.id.textView2);
  
          final int total = 30000; // the total number
  //...
  //when you want to start the counting start the thread bellow.
          new Thread(new Runnable() {
              int counter = 0;
              public void run() {
                  while (counter &lt; total) {
                      try {
                          Thread.sleep(1000);
                      } catch (InterruptedException e) {
                          // TODO Auto-generated catch block
                          e.printStackTrace();
                      }
                      textView1.post(new Runnable() {
  
                          public void run() {
                              textView1 = findViewById(R.id.textView1);
                              textView1.setText("" + counter);
  
                          }
  
                      });
                      counter += 2;
                  }
  
              }
  
          }).start();
      }
  </code></pre>
  
  <p>and here is the error </p>
  
  <blockquote>
    <p>java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.widget.TextView.post(java.lang.Runnable)' on a null object reference. Now, this error is for the line textView1.post(New runnable)</p>
  </blockquote>
  
  <p>Could there be a more simpler approach to the problem? </p>