title: How to resolve NullPointerException error in android studio?
body: <p>I am trying to connect my android app to MS SQL Server 2008. I am using Android Studio. This is my <code>MainActivity.java</code> file:</p>
  
  <pre><code>import android.annotation.SuppressLint;
  import android.os.Bundle;
  import android.os.StrictMode;
  import android.support.v7.app.ActionBarActivity;
  import android.util.Log;
  import android.view.View;
  import android.widget.Button;
  import android.widget.EditText;
  import android.widget.TextView;
  
  import java.sql.Connection;
  import java.sql.DriverManager;
  import java.sql.PreparedStatement;
  import java.sql.SQLException;
  import java.sql.Statement;
  
  public class MainActivity extends ActionBarActivity {
  
  Button add;
  TextView errorlbl;
  EditText name, address, pincode;
  Connection connect;
  PreparedStatement preparedStatement;
  Statement st;
  String ipaddress, db, username, password;
  
  @SuppressLint("NewApi")
  private Connection ConnectionHelper(String user, String password,
                                      String database, String server) {
      StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
              .permitAll().build();
      StrictMode.setThreadPolicy(policy);
      Connection connection = null;
      String ConnectionURL = null;
      try {
          Class.forName("net.sourceforge.jtds.jdbc.Driver");
          ConnectionURL = "jdbc:jtds:sqlserver://" + server + ";"
                  + "databaseName=" + database + ";user=" + user
                  + ";password=" + password + ";";
          connection = DriverManager.getConnection(ConnectionURL);
      } catch (SQLException se) {
          Log.e("ERRO", se.getMessage());
      } catch (ClassNotFoundException e) {
          Log.e("ERRO", e.getMessage());
      } catch (Exception e) {
          Log.e("ERRO", e.getMessage());
      }
      return connection;
  }
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
  
      add = (Button) findViewById(R.id.btnadd);
  
      errorlbl = (TextView) findViewById(R.id.lblerror);
  
      name = (EditText) findViewById(R.id.txtname);
      address = (EditText) findViewById(R.id.txtaddress);
      pincode = (EditText) findViewById(R.id.txtpincode);
  
      ipaddress = "192.168.0.9";
      db = "mydatabase";
      username = "myusername";
      password = "mypassword";
      connect = ConnectionHelper(username, password, db, ipaddress);
      add.setOnClickListener(new View.OnClickListener() {
  
          @Override
          public void onClick(View v) {
              try {
  
  
        connect = ConnectionHelper(username, password, db, ipaddress);
        st = connect.createStatement();
  
                  preparedStatement = connect
                          .prepareStatement("insert into studentRecord(Name,Address,Pincode) values ('"
                                  + name.getText().toString()
                                  + "','"
                                  + address.getText().toString()
                                  + "','"
                                  + pincode.getText().toString() + "')");
                  preparedStatement.executeUpdate();
                  errorlbl.setText("Data Added successfully");
              } catch (SQLException e) {
                  errorlbl.setText(e.getMessage().toString());
              }
  
          }
      });
  
  }
  }
  </code></pre>
  
  <p>But I'm getting the following error: </p>
  
  <pre><code>java.lang.NullPointerException: Attempt to invoke interface method   'java.sql.Statement java.sql.Connection.createStatement()' on a null object reference at app.mysqlapp.MainActivity$1.onClick(MainActivity.java:77)
  </code></pre>
  
  <p>The error points to the below code</p>
  
  <pre><code>st = connect.createStatement();
  </code></pre>
  
  <p>How to I solve this? </p>
  
  <p>I've included the jtds-1.2.7 jar file and I've properly set up the manifest file.</p>
  
  <p>I am a beginner in Android Development. Please help me to fix this issue. Thanks!</p>