title: Android - chronometer start and stop
body: <p>I want to create a chronometer which I can start and stop with one button. With my code below I just get a nullpointer exception. How can I avoid it, or set the two functions to the button anyway?</p>
  
  <pre><code>public class MainActivity extends ActionBarActivity {
    Chronometer focus;
    Button start, stop, reset;
    long timeElapsed = SystemClock.elapsedRealtime() - focus.getBase();
    int x;
  
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
  
      timeElapsed = x;
      start = (Button) findViewById(R.id.button1);       
      focus = (Chronometer) findViewById(R.id.chronometer1);
  
      start.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              // TODO Auto-generated method stub
              if(x == 0){
              focus.start();
              }
  
              if(x != 0){
              focus.stop();    
              }
          }
      });
  
  }
  
  }
  </code></pre>