title: Dynamically add table row to table layout on button click
body: <p>I am trying to add a new row to a table every time a button is clicked on my android app. Everytime I hit the add row button it crashes and gives me an error on the line that it's trying to add the row to the table. The error is java.lang.NullPointerException (not sure what that means I'm pretty new to android development).</p>
  
  <p>Here's code I have below. Any input helps. </p>
  
  <p>The error occurs on the table.addView(tr, new.......    line(almost the last line). </p>
  
  <pre><code>public class addItem extends Activity 
  {
  
      private TableLayout table = null;
  
      protected void onCreate(Bundle savedInstanceState) 
      {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_additem);
  
          table = (TableLayout)findViewById(R.id.maintable);
  
          Button additembutton = (Button) findViewById(R.id.additembutton);
          additembutton.setOnClickListener(new OnClickListener() {
              @Override
              public void onClick(View v) {
  
                  update();
              }
          });
  
      }
  
      private void update()
      {
          TableRow tr = new TableRow(addItem.this);
          tr.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.WRAP_CONTENT));
  
          TextView tv = new TextView(addItem.this);
          tv.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
          tv.setText("New Entry");
          tr.addView(tv);
  
          table.addView(tr, new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
  
  
      }}
  </code></pre>
  
  <p>it seems as though every works no problems besides that last line. </p>
  
  <p>logcat report:</p>
  
  <pre><code>08-19 06:57:54.002: E/AndroidRuntime(3486): FATAL EXCEPTION: main 08-19 06:57:54.002: E/AndroidRuntime(3486): java.lang.NullPointerException 08-19 06:57:54.002: E/AndroidRuntime(3486): at com.mirafriends.recipeme.addItem.update(addItem.java:47) 08-19 06:57:54.002: E/AndroidRuntime(3486): at com.mirafriends.recipeme.addItem.access$0(addItem.java:37) 08-19 06:57:54.002: E/AndroidRuntime(3486): at com.mirafriends.recipeme.addItem$1.onClick(addItem.java:30) 08-19 06:57:54.002: E/AndroidRuntime(3486): at android.view.View.performClick(View.java:4204) 
  </code></pre>