title: nullpointer exception on statement = conn.createStatement();
body: <p>I am using jdbc in android (Aware not recomended)</p>
  
  <p>I have following piece of code:</p>
  
  <pre><code> public int CheckUser(String uName,String password)
          {
  
               try {
  
                      String sql="select uid from UserMaster where username='"+uName+"' and password='"+password+"'";
                      statement = conn.createStatement();
                      resultSet = statement.executeQuery(sql);
                      int value=0;
                      if (resultSet.next()) {
                           value=resultSet.getInt(1);
                          }
  
  
                      return value;
  
                  }
                  catch (Exception e) 
                  {
                      e.printStackTrace();
                      return 0;
                  }
  }
  </code></pre>
  
  <p>I gets null pointer exception on  <code>statement = conn.createStatement();</code></p>
  
  <p><strong>Logcat:</strong></p>
  
  <pre><code>09-12 11:04:14.543: W/KeyCharacterMap(483): No keyboard for id 0
  09-12 11:04:14.553: W/KeyCharacterMap(483): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
  09-12 11:04:51.173: W/System.err(483): java.lang.NullPointerException
  09-12 11:04:51.194: W/System.err(483):  at com.example.messagesql.gaSQLConnect.CheckUser(gaSQLConnect.java:69)
  09-12 11:04:51.213: W/System.err(483):  at com.example.messagesql.Login$UserLogin.doInBackground(Login.java:109)
  09-12 11:04:51.213: W/System.err(483):  at com.example.messagesql.Login$UserLogin.doInBackground(Login.java:1)
  09-12 11:04:51.225: W/System.err(483):  at android.os.AsyncTask$2.call(AsyncTask.java:185)
  09-12 11:04:51.233: W/System.err(483):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
  09-12 11:04:51.233: W/System.err(483):  at java.util.concurrent.FutureTask.run(FutureTask.java:138)
  09-12 11:04:51.253: W/System.err(483):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
  09-12 11:04:51.253: W/System.err(483):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
  09-12 11:04:51.264: W/System.err(483):  at java.lang.Thread.run(Thread.java:1019)
  </code></pre>
  
  <p>Plz help.</p>
  
  <p>I tried declaring Statement object locally but no effect, still error persists.</p>
  
  <p>Code of my file which calls this function:</p>
  
  <pre><code>public class Login extends Activity {
  
  
      public static String resp = "";
      public static String name = "";
      String username;
      String password;
       EditText etLoginID;
      EditText etPassword;
      final DatabaseHandler db = new DatabaseHandler(this);
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_login);
  
  
  
          List&lt;Contact&gt; contacts = db.getAllContacts();
          if(contacts.size() != 0){
              for (Contact cn : contacts){
                  resp = cn.getID();
                  name = cn.getName();
              }
             // Intent i=new Intent(getApplicationContext(),Message.class);
              //startActivity(i);
          }
  
          etLoginID=(EditText)findViewById(R.id.etName);
          etPassword=(EditText)findViewById(R.id.etPassword);
  
          Button btnLogin =(Button)findViewById(R.id.btnLogin);
  
          final gaSQLConnect con=new gaSQLConnect();
  
          btnLogin.setOnClickListener(new OnClickListener() {
  
              @Override
              public void onClick(View arg0) {
                  // TODO Auto-generated method stub
  
                  con.setConnection("AndroidDB", "sa", "ok");
  
  
                      username=etLoginID.getText().toString();
                      password=etPassword.getText().toString();
                      String[] values=new String[2];
                      values[0]=username;
                      values[1]=password;
                      UserLogin ul=new UserLogin();
                      ul.execute(values);
  
  
  
                      //Intent i= new Intent(getApplicationContext(),Messages.class);
                      //startActivity(i);
  
  
              }
          });
  
  
      }
  
      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
          // Inflate the menu; this adds items to the action bar if it is present.
          getMenuInflater().inflate(R.menu.login, menu);
          return true;
      }
  
  
      private class UserLogin extends AsyncTask&lt;String[], Void, String&gt;{
  
          final gaSQLConnect con=new gaSQLConnect();
          String[] values;
          @Override
          protected String doInBackground(String[]... params) {
              // TODO Auto-generated method stub
  
              values=params[0];
              String result=null;
              int uid;
              db.deleteContact(new Contact(resp));
  
              if (!values[0].isEmpty() &amp;&amp; !values[1].isEmpty())
              {
  
                  uid=con.CheckUser(etLoginID.getText().toString(), etPassword.getText().toString());
  
  
                  return Integer.toString(uid) ;
  
              }
              else{
                  uid=0;;
              }
              return Integer.toString(uid) ;
          }
  
  
      protected void onPostExecute(String result){            
  
              Toast.makeText(getApplicationContext(),result, Toast.LENGTH_LONG).show();
              db.addContact(new Contact(result, values[0]));
              Intent i=new Intent(getApplicationContext(),Messages.class);
              startActivity(i);
  
      }   
      }
  </code></pre>
  
  <p>Above is the code of file from which i calls the function.</p>
  
  <p>In this onClick method of login i have setted connection.</p>
  
  <p>Then also why it does not persists through whole application?</p>
  
  <p>Whole code of DBConnection class:</p>
  
  <pre><code>public class gaSQLConnect 
  {
      String url ="";
      Connection conn=null;
      Statement statement=null;
      ResultSet resultSet=null;
  
  
  
  
          public ResultSet getMessages(int uid)
          {
  
  
            try
            {
              String sql="select  mid,uid,message,rstamp from MessagesMaster where uid='"+uid+"' aand rstamp = 0 order by tstamp desc";
              statement = conn.createStatement();
              resultSet = statement.executeQuery(sql);
              return resultSet;
            }
            catch(Exception ex)
            {
  
            }
          return resultSet;
  
          }
  
  
          public void setConnection(String DBName,String UserName,String Password)
          {
              try {
                  Class.forName("net.sourceforge.jtds.jdbc.Driver").newInstance();
                  url ="jdbc:jtds:sqlserver://10.0.2.2:1433;instanceName=14GRAFICALI\\MSSQLSERVER2008;DatabaseName="+DBName+";integratedSecurity=true;user="+UserName+";password="+Password+"";
                  conn =DriverManager.getConnection(url);
                  } catch (Exception e) {
                  e.printStackTrace();
                  }
          }
          int recFound=0;
          public int CheckUser(String uName,String password)
          {
  
               try {
                   Statement statement=null;
                      ResultSet resultSet=null;    
  
                      String sql="select uid from UserMaster where username='"+uName+"' and password='"+password+"'";
                      statement = conn.createStatement();
                      resultSet = statement.executeQuery(sql);
                      int value=0;
                      if (resultSet.next()) {
                           value=resultSet.getInt(1);
                          }
  
  
                      return value;
  
  
                    /*  if (value &gt; 0) {
                          recFound = 1;
                      } else {
                          recFound = 0;
                      }
                      if (recFound &gt; 0) 
                      {
                          return true;
                      } else {
                          return false;
                      }*/
                  }
                  catch (Exception e) 
                  {
                      e.printStackTrace();
                      return 0;
                  }
  }
  
          public void UpdateMessage(String[] mlst) {
              // TODO Auto-generated method stub
              try
              {
                  String sql="update messagesmaster set rstamp = 1 where mid in (" + mlst + ") ";
                  statement = conn.createStatement();
                  statement.executeUpdate(sql);
  
              }
              catch(Exception ex)
              {
  
              }
          }
  }
  </code></pre>