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
- to get users tower code.
- to create a database.
- to get an input string for location name from user.
- to store these values in database.
- to display these values when requested.
modules 1 and 2 were working perfectly individually, but when i integrate them I get a Fatal main and a null pointer exception.
My code is as below
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<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()>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
}
});
}
}
My logcat is as follows

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.
Now, my queries are
- Why I’m a getting a null point exception error with my database connection and how can I resolve it?
-
I’ve used the following code to insert values into a table,
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?
Thank you.
The CREATE statement has
EXISTSmisspelled.The INSERT statement should read
But I think the most likely cause is that the
openOrCreateDatabasecall hasn’t run, so nothing is assigned tocd.