user2905867
user2905867

Reputation: 27

Eclipse data not inserting into SQLite Database and crashing

When I click on the button, it's supposed to store the typed data into the database, along with the longitude and latitude that has been collected from a separate activity. I'm unsure what I did wrong, my other database works fine. Any help would be appreciated. I'll post the code below:

This is the activity java.

public class NoteActivity extends Activity implements OnClickListener {

Button buttonLeaveNote; 

private EditText mTitle;
private EditText mDesc;

protected NoteDBHelper noteDB = new NoteDBHelper(NoteActivity.this);

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_note);

    buttonLeaveNote = (Button) findViewById(R.id.buttonLeaveNote);
    buttonLeaveNote.setOnClickListener(this);

    mTitle = (EditText)findViewById(R.id.etitle);
    mDesc = (EditText)findViewById(R.id.edesc);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.note, menu);
    return true;
}

@Override
public void onClick(View v) {
    switch(v.getId()){

    case R.id.buttonLeaveNote:

           String title = mTitle.getText().toString();
           String desc = mDesc.getText().toString();
           Intent intent = getIntent();
           String lati = intent.getExtras().getString("lati");
           String lng = intent.getExtras().getString("lng");

           boolean invalid = false;

           if(title.equals(""))
           {
            invalid = true;
            Toast.makeText(getApplicationContext(), "Please enter a title",          Toast.LENGTH_SHORT).show();
           }
           else
               if(desc.equals(""))
               {
                  invalid = true;
                  Toast.makeText(getApplicationContext(), "Please enter description", Toast.LENGTH_SHORT).show();

               } 

               else

                   if(invalid == false)
                   {
                       addEntry(title, desc, lati, lng);
                       Intent i_note = new Intent(NoteActivity.this, JustWanderingActivity.class);
                       startActivity(i_note);
                       //finish();
                      }

                      break;
                      }
                    }

public void onDestroy()
{
    super.onDestroy();
    noteDB.close();
}

private void addEntry(String title, String desc, String lati, String lng) 
 {
    SQLiteDatabase notedb = noteDB.getWritableDatabase();
    ContentValues values = new ContentValues();
      values.put("title", title);
      values.put("desc", desc);
      values.put("lati", lati);
      values.put("lng", lng);
      try
      {
       long newRowId;
       newRowId = notedb.insert(NoteDBHelper.DATABASE_TABLE_NAME, null, values);

       Toast.makeText(getApplicationContext(), "Note successfully added", Toast.LENGTH_SHORT).show();
      }

      catch(Exception e)
      {
       e.printStackTrace();
      } 
}
}

This is the SQLite DB Java

public class NoteDBHelper extends SQLiteOpenHelper 

{
private SQLiteDatabase notedb;

public static final String NOTE_ID = "_nid";
public static final String NOTE_TITLE = "title";
public static final String NOTE_DESC = "desc";
public static final String NOTE_LAT = "lati";
public static final String NOTE_LONG = "lng";


NoteDBHelper noteDB = null;
private static final String DATABASE_NAME = "stepsaway.db";
private static final int DATABASE_VERSION = 2;
public static final String DATABASE_TABLE_NAME = "notes";


private static final String DATABASE_TABLE_CREATE =
        "CREATE TABLE" + DATABASE_TABLE_NAME + "(" +
        "_nid INTEGER PRIMARY KEY AUTOINCREMENT," +
        "title TEXT NOT NULL, desc LONGTEXT NOT NULL, lati TEXT NOT NULL, lng, TEXT NOT NULL);";

 public NoteDBHelper(Context context) {

 super(context, DATABASE_NAME, null, DATABASE_VERSION);
 System.out.println("In constructor");
}


@Override
public void onCreate(SQLiteDatabase notedb) {

try{

notedb.execSQL(DATABASE_TABLE_CREATE);


}catch(Exception e){
e.printStackTrace();
}
}



@Override
public void onUpgrade(SQLiteDatabase notedb, int oldVersion, int newVersion) {
// TODO Auto-generated method stub

}




public Cursor rawQuery(String string, String[] strings) {
// TODO Auto-generated method stub
return null;
}




public void open() {

getWritableDatabase(); 
}


public Cursor getDetails(String text) throws SQLException 
{

Cursor mCursor =
        notedb.query(true, DATABASE_TABLE_NAME, 
          new String[]{NOTE_ID, NOTE_TITLE, NOTE_DESC, NOTE_LAT, NOTE_LONG}, 
          NOTE_TITLE + "=" + text, 
          null, null, null, null, null);
if (mCursor != null) 
{
    mCursor.moveToFirst();
}
return mCursor;


}
}

Here's the logcat

FATAL EXCEPTION: main
java.lang.NullPointerException
atcom.example.stepsaway.NoteActivity.onClick(NoteActivity.java:53)
at android.view.View.performClick(View.java:4240)
at android.view.View$PerformClick.run(View.java:17721)
at android.os.Handler.handleCallback(Handler.java:730)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)

Here's the putExtra() calls.

    tvLatitude.setText("Latitude:" +  location.getLatitude());
    tvLongitude.setText("Longitude:" + location.getLongitude());
    String lati = tvLatitude.getText().toString();
    String lng = tvLongitude.getText().toString();
    Intent i = new Intent(this, NoteActivity.class);
    i.putExtra("lati", lati);
    i.putExtra("lng", lng);

Upvotes: 2

Views: 320

Answers (2)

Tarsem Singh
Tarsem Singh

Reputation: 14199

Without logcat its very difficult to help you... so also post logcat

One mistake is in your create table query inside your NoteDBHelper class...

you are forgetting to put space between keyword table and table name (DATABASE_TABLE_NAME)

So either change to:

private static final String DATABASE_TABLE_CREATE =
        "CREATE TABLE " + DATABASE_TABLE_NAME + "(" +
        "_nid INTEGER PRIMARY KEY AUTOINCREMENT," +
        "title TEXT NOT NULL, desc LONGTEXT NOT NULL, lati TEXT NOT NULL, lng, TEXT NOT NULL);";

Or to:

public static final String DATABASE_TABLE_NAME = " notes";

PS.

NullPointerException

Check for null before using intent.getExtras() and also make sure you are passing String Not double in intent.putExtra()

intent.putExtra("lati","CheckHere it must be String not double");
intent.putExtra("lng","CheckHere it must be String not double");

And also following code would avoid the crash and will help you to debug properly

Intent intent = getIntent();
if(intent != null)
{
    String lati = intent.getExtras().getString("lati");
    String lng = intent.getExtras().getString("lng");
   if(lati == null)
    {
      Log.e("Checking lati","Its Null");
      lati="0";
    }
   if(lng == null)
    {
      Log.e("Checking lng","Its Null");
      lng="0";
    }
}
else
{
  Log.e("Checking intent","Its Null");
}

Upvotes: 1

jyomin
jyomin

Reputation: 1967

The addentry method should be called as follows:

noteDB.addEntry(title, desc, lati, lng);

Upvotes: 0

Related Questions