title: android database (SQLite) connection
body: <p>I'm trying to create an android application which can store users location (cell tower code) with a name of his choice, i.e for example the cell tower code is 405673 and I want to store that as a 'Home' location.
  So, essentially my program contains the following modules</p>
  
  <ol>
  <li>to get users tower code.</li>
  <li>to create a database.</li>
  <li>to get an input string for location name from user.</li>
  <li>to store these values in database.</li>
  <li>to display these values when requested.</li>
  </ol>
  
  <p>modules 1 and 2 were working perfectly individually, but when i integrate them I get a Fatal main and a null pointer exception.</p>
  
  <p>My code is as below</p>
  
  <pre><code>package my.project.mil;
  
  import android.content.Context;
  import android.content.Intent;
  import android.database.sqlite.SQLiteDatabase;
  import android.os.Bundle;
  import android.view.View;
  import android.widget.Button;
  import android.widget.EditText;
  import android.widget.Toast;
  
  public class Cbdata extends MainActivity {
  
      public String str;
  
      public void onReceive(Context context, Intent intent) {
          //---get the CB message passed in---
          Bundle bundle = intent.getExtras();        
          SmsCbMessage[] msgs = null;
          str = "";            
          if (bundle != null)  {
              //---retrieve the SMS message received---
              Object[] pdus = (Object[]) bundle.get("pdus");
              msgs = new SmsCbMessage[pdus.length];            
              for (int i=0; i&lt;msgs.length; i++) {
                  msgs[i] = SmsCbMessage.createFromPdu((byte[])pdus[i]);                
                  str += "CB " + msgs[i].getGeographicalScope() + msgs[i].getMessageCode() + msgs[i].getMessageIdentifier() + msgs[i].getUpdateNumber();                     
                  str += " :";
                  str += msgs[i].getMessageBody().toString();
                  str += "\n";        
              }}
  }
  
      SQLiteDatabase cd = openOrCreateDatabase("mydata", MODE_WORLD_READABLE, null);
  
  
  
  Button submit;
  Button viewdb;
  EditText name;
  
  @Override
  public void onCreate(Bundle savedInstanceState)
  {
      // TODO Auto-generated method stub
      super.onCreate(savedInstanceState);
      setContentView(R.layout.seventh);
  
      viewdb = (Button) findViewById(R.id.viewdb);
      submit = (Button) findViewById(R.id.submit);
      name = (EditText) findViewById(R.id.name);
  
      submit.setOnClickListener(new View.OnClickListener() 
      {
  
          public void onClick(View arg0) {
  
              String locname = name.getText().toString();
              if (locname.length()&gt;0)
              {
  
                  cd.execSQL("CREATE TABLE IF NOT EXITSTS MLITable     (CblocationCode INT(10), CblocationName VARCHAR);");
                  cd.execSQL("INSERT INTO MLITable VALUES (' ',' ');" +str +locname);
                  Toast.makeText(getBaseContext(), "value successfully entered.", Toast.LENGTH_LONG).show();
              }
              else
              {
                  Toast.makeText(getBaseContext(), "Please enter the location name, for example 'Home'.", Toast.LENGTH_LONG).show();
              }
  
          }
      });
  
      cd.close();
  
      viewdb.setOnClickListener(new View.OnClickListener() 
      {
  
          public void onClick(View arg0) {
              // TODO Auto-generated method stub
              // here comes the code for viewing the database
  
  
          }
      });
  }
  
  
  
  } 
  </code></pre>
  
  <p>My logcat is as follows </p>
  
  <p><img src="https://i.stack.imgur.com/Jk3R7.png" alt="logcat"></p>
  
  <p>Interestingly, I've removed database commands to see if my code is working or not, from that I could conclude that I made a mistake in database connection line. </p>
  
  <p>Now, my queries are</p>
  
  <ol>
  <li>Why I'm a getting a null point exception error with my database connection and how can I resolve it?</li>
  <li><p>I've used the following code to insert values into a table,</p>
  
  <p>cd.execSQL("CREATE TABLE IF NOT EXITSTS MLITable     (CblocationCode INT(10), CblocationName VARCHAR);");
  cd.execSQL("INSERT INTO MLITable VALUES (' ',' ');" +str +locname);
  is it the correct way to pass user values to my database?</p></li>
  </ol>
  
  <p>Thank you.</p>