Naresh Cherry
Naresh Cherry

Reputation: 9

i cannot add date value in to SQLite database it is showing error in the catalog ...please suggest the correct method

My application has a MainActivity and a Database Helpler class.

MainActivity

package com.example.sample;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import android.os.Bundle;
import android.app.Activity;
import android.database.sqlite.SQLiteDatabase;
import android.text.format.DateFormat;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity{


Button b1;
EditText et1;
protected DatabaseHelper helper;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    helper=new DatabaseHelper(this);
    b1=(Button)findViewById(R.id.button1);
    et1=(EditText)findViewById(R.id.editText1);

    b1.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub



    SimpleDateFormat formatter=new SimpleDateFormat("dd/MM/yyyy");
    Date dateobject;

    String date_var=(et1.getText().toString());
    try {
        dateobject=formatter.parse(date_var);
        String date=new SimpleDateFormat().format(dateobject);
        helper.AddDetail(date);
Toast.makeText(getApplicationContext(),""+date, Toast.LENGTH_SHORT).show();    
        helper.close();
    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

        }
    });

}



}

DatabaseHelper Class is defined below

 package com.example.sample;

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

public class DatabaseHelper extends SQLiteOpenHelper {

DatabaseHelper helper;
SQLiteDatabase db;
static final int VERSION=2;
static final String DATA_NAME="Mydata";
Context c;

public DatabaseHelper(Context context) {
    super(context, DATA_NAME, null, VERSION);
    c=context;
    // TODO Auto-generated constructor stub
}

@Override
public void onCreate(SQLiteDatabase arg0) {
    // TODO Auto-generated method stub
    String query="create table if not exists Mytable(id Integer primary key,Daate text 
    not null)";
    db.execSQL(query);
}
public long AddDetail(String date)
{
    db=getWritableDatabase();
    ContentValues vals=new ContentValues();
    vals.put("Daate",date);
    long a=db.insert("Mytable", null, vals);
    db.close();
    return a;

}

@Override
public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
    // TODO Auto-generated method stub

}

    }

It is showing an error at the line db.execSQL(query); in the Database Helper Class(defined below) and at the line helper.AddDetail(date); inside the MainActivity. The Logcat is shown below

     09-03 16:51:22.481: E/AndroidRuntime(3240): FATAL EXCEPTION: main
     09-03 16:51:22.481: E/AndroidRuntime(3240): java.lang.NullPointerException
     09-03 16:51:22.481: E/AndroidRuntime(3240):    at                                                              com.example.sample.DatabaseHelper.onCreate(DatabaseHelper.java:27)
     09-03 16:51:22.481: E/AndroidRuntime(3240):    at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:252)
     09-03 16:51:22.481: E/AndroidRuntime(3240):    at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
     09-03 16:51:22.481: E/AndroidRuntime(3240):    at com.example.sample.DatabaseHelper.AddDetail(DatabaseHelper.java:31)
     09-03 16:51:22.481: E/AndroidRuntime(3240):    at com.example.sample.MainActivity$1.onClick(MainActivity.java:48)
     09-03 16:51:22.481: E/AndroidRuntime(3240):    at android.view.View.performClick(View.java:4204)
     09-03 16:51:22.481: E/AndroidRuntime(3240):    at android.view.View$PerformClick.run(View.java:17355)
     09-03 16:51:22.481: E/AndroidRuntime(3240):    at android.os.Handler.handleCallback(Handler.java:725)
     09-03 16:51:22.481: E/AndroidRuntime(3240):    at android.os.Handler.dispatchMessage(Handler.java:92)
     09-03 16:51:22.481: E/AndroidRuntime(3240):    at android.os.Looper.loop(Looper.java:137)
     09-03 16:51:22.481: E/AndroidRuntime(3240):    at android.app.ActivityThread.main(ActivityThread.java:5041)
     09-03 16:51:22.481: E/AndroidRuntime(3240):    at java.lang.reflect.Method.invokeNative(Native Method)
     09-03 16:51:22.481: E/AndroidRuntime(3240):    at java.lang.reflect.Method.invoke(Method.java:511)
     09-03 16:51:22.481: E/AndroidRuntime(3240):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
     09-03 16:51:22.481: E/AndroidRuntime(3240):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
     09-03 16:51:22.481: E/AndroidRuntime(3240):    at dalvik.system.NativeStart.main(Native Method)

Upvotes: 0

Views: 470

Answers (3)

tanay tandon
tanay tandon

Reputation: 243

I solved your problem. In case you cannot fully understand my explanation refer to this wonderful tutorial. The DatabaseHelper class should not be a class, it should be the sub class of another class. I have made many changes to your code. Just change your code like I have written. Enough said lets go to the code.

There are very few changes in MainActivity.java i have commented on all the changes

package com.example.sample; 

import java.text.ParseException; 
import java.text.SimpleDateFormat; 
import java.util.Date; 

import android.os.Bundle; 
import android.app.Activity; 
import android.database.sqlite.SQLiteDatabase; 
import android.text.format.DateFormat; 
import android.util.Log; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.Toast; 

public class MainActivity extends Activity {

    Button b1;
    EditText et1;
    protected DatabaseAdapter helper; // type of variable helper has been changed

    @Override 
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        helper = new DatabaseAdapter(this); //the context of the current class is passed
        helper.open();
        b1 = (Button)findViewById(R.id.button1);
        et1 = (EditText)findViewById(R.id.editText1);

        b1.setOnClickListener(new OnClickListener() {

            @Override 
            public void onClick(View v) {
                // TODO Auto-generated method stub 

                SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
                Date dateobject;

                String date_var=(et1.getText().toString());
                try { 
                    dateobject=formatter.parse(date_var);
                    String date=new SimpleDateFormat().format(dateobject);
                    helper.AddDetail(date);
                    Toast.makeText(getApplicationContext(),""+date, Toast.LENGTH_SHORT).show();    
                    helper.close();
                } catch (ParseException e) {
                    // TODO Auto-generated catch block 
                    e.printStackTrace();
                } 
            } 
        });  
    } 
} 

Now come to DatabaseHelper.java i have made it a subclass of another class DatabaseAdapter.java

package com.example.sample;

import android.content.ContentValues;
import android.content.Context;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DatabaseAdapter {

    DatabaseHelper helper;
    SQLiteDatabase db;
    static final int VERSION=2;
    static final String DATA_NAME="Mydata";
    private static final String query =  "create table Mytable(id Integer primary key,Daate text not null)";  //the query has been changed
    private final Context mCtx;


     public DatabaseAdapter(Context ctx) {
            this.mCtx = ctx;
        }

            //DatabaseHelper has become a subclass
    public class DatabaseHelper extends SQLiteOpenHelper {

        public DatabaseHelper(Context context) {
            super(context, DATA_NAME, null, VERSION);
                        // TODO Auto-generated constructor stub 
        } 

        @Override 
        public void onCreate(SQLiteDatabase arg0) {
            // TODO Auto-generated method stub 
            arg0.execSQL(query); 
        } 

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

     public DatabaseAdapter open() throws SQLException {
         helper = new DatabaseHelper(mCtx);
            db=helper.getWritableDatabase();
            return this;
        }

      public void close() {
            helper.close();
        }

    public long AddDetail(String date)
    { 
        Log.d("tag",date);
        ContentValues vals=new ContentValues();
        vals.put("Daate",date);
        long a=db.insert("Mytable", null, vals);
        return a;
    } 
}

Upvotes: 0

Piyush
Piyush

Reputation: 18933

you have done silly mistake:

change

 SimpleDateFormat formatter=new SimpleDateFormat("dd/mm/yyyy");

with

 SimpleDateFormat formatter=new SimpleDateFormat("dd/MM/yyyy");

Just add this line after onCreate() method:because you have just declared varible..But didn't make object of it..

helper = DatabaseHelper(your parameters here);

Upvotes: 1

Sunil Kumar
Sunil Kumar

Reputation: 7092

change this line, mm means minute and MM means month

SimpleDateFormat formatter=new SimpleDateFormat("dd/MM/yyyy");

you need to create the object of DatabaseHelper inside the oncreate method

helper=new DatabaseHelper(parameters);

Upvotes: 2

Related Questions