Note that there are some explanatory texts on larger screens.

plurals
  1. POi cannot add date value in to SQLite database it is showing error in the catalog ...please suggest the correct method
    primarykey
    data
    text
    <p>My application has a MainActivity and a Database Helpler class.</p> <p>MainActivity</p> <pre><code>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(); } } }); } } </code></pre> <p>DatabaseHelper Class is defined below</p> <pre><code> 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 } } </code></pre> <p>It is showing an error at the line <strong>db.execSQL(query);</strong> in the Database Helper Class(defined below) and at the line <strong>helper.AddDetail(date);</strong> inside the MainActivity. The Logcat is shown below</p> <pre><code> 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) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload