title: Java, using an interface as a callback
body: <p>I have been developing a simple touch handler for Android with the possibilites of firing callbacks like onUpdate (when the screen is touched) without having to setup threads. My problem is that my knowledge of Java is fairly limited and i can't do it because i know very little of how to use interfaces. I'm pretty sure that my problem may be a simple typo or something, but i get a NullPointerException when i execute the method from the touch handler (which processed the touch information) so that i can do what i need in the main activity class.</p>
  
  <p>This is the main class code (cut from the irrelevant stuff):</p>
  
  <pre><code>//package and imports
  
  public class Test extends Activity implements TouchHelper {
  
      StringBuilder builder = new StringBuilder();
      TextView textView;
      TouchReader touchReader;
      List&lt;TouchTable&gt; touchTablesArray;
      TouchTable touchTable;
      public static final String Tag = "TouchTest";
  
          @Override
          public void onCreate(Bundle savedInstanceState) 
          {
              super.onCreate(savedInstanceState);
              textView = new TextView(this);
              Log.d(Tag, "TextView initialized " + textView);
              textView.setText("Touch and drag (multiple fingers supported)!");
              touchReader = new TouchReader(textView);
              Log.d(Tag, "touchReader initialized");
              touchTablesArray = touchReader.getTouchTables();
              setContentView(textView);
          }
  
          @Override
          public void onTouchUpdate(int pointerId)
          {
              Log.d(Tag, "onTouchUpdate called");
              touchTable = touchTablesArray.get(pointerId);
              Log.d(Tag, "touchTable get successful");
              //writing on stringbuilder
          }
      }
  </code></pre>
  
  <p>This is the code of the handler itself:</p>
  
  <pre><code>//package and imports
  
  public class TouchReader implements OnTouchListener
  {
      public final static String Tag = "TouchReader";
      List&lt;TouchTable&gt; touchTables;
      TouchHelper helper;
      TouchTable touchTable = new TouchTable();
  
      public TouchReader(View view)
      {
          view.setOnTouchListener(this);
          touchTables = new ArrayList&lt;TouchTable&gt;(10);
          Log.d(Tag, "TouchReader initialized");
      }
  
      public boolean onTouch(View v, MotionEvent event)
      {
          synchronized(this)
          {
              //all the common code handling the actual handling, with switches and such
              touchTables.add(pointerId, touchTable); //obviously the pointerId is defined earlier
              Log.d(Tag, "Values updated");
              helper.onTouchUpdate(pointerId); //the exception is here
              Log.d(Tag, "Update called");
          }
          return true;
      }
      public List&lt;TouchTable&gt; getTouchTables()
      {
          synchronized(this)
          {
              return touchTables;
          }
      }
  }
  </code></pre>
  
  <p>As you can see the error is most likely due to my inability to correctly use an interface, and yet all the official docs confused me even more.</p>
  
  <p>Finally, the tiny code of the interface:</p>
  
  <pre><code>//package
  
  public interface TouchHelper 
  {
      public void onTouchUpdate(int pointerId);
  }
  </code></pre>
  
  <p>I hope this question isn't too noobish to post it here :)</p>
  
  <p>EDIT: Thanks to all for the help, in the end i followed Bughi's solution.</p>