Reputation: 42854
What i am doing:: I am trying to create database and insert values to a table in it
problem i am facing:: my database is not created (I checked in DDMS)
MainActivity.java
public class MainActivity extends Activity implements OnClickListener {
Button addUser;
EditText name,pwd;
DatabaseAdapter adapter;
Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addUser=(Button) findViewById(R.id.button1);
name=(EditText) findViewById(R.id.editText1);
pwd=(EditText) findViewById(R.id.editText2);
}
@Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
addUser.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()){
case R.id.button1:
long returnedRes = 0;
try {
returnedRes = adapter.insertData(name.getText().toString(), pwd.getText().toString());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.d("Log-EXCEPTION", e.toString());
}
if(returnedRes<0)
{
Message.message(this, "Unsuccessful");
Log.d("LOG-MSG", "Unsuccessful");
}else{
Message.message(this, "Successful");
Log.d("LOG-MSG", "Successful");
}
break;
}
}
}
DatabaseAdapter.java
public class DatabaseAdapter {
DatabaseHelper helper;
ContentValues cv;
SQLiteDatabase db;
//Constructor of outer class
DatabaseAdapter(Context context){
helper=new DatabaseHelper(context);//creating object of Inner class
}
public long insertData(String name,String password){
db=helper.getWritableDatabase();
cv.put(helper.NAME, name);
cv.put(helper.PASSWORD, password);
long id=db.insert(helper.TABLE_NAME, null, cv);
/*
* -1 if the value is inserted has some problem
* else it will return the rowId of the column inserted
*/
return id;
}
class DatabaseHelper extends SQLiteOpenHelper{
private Context context;
private static final String DATABASE_NAME="MyDatabase";
private static final String TABLE_NAME="MyTable";
private static final int DATABASE_VERSION=1;
private static final String ID="_id";
private static final String NAME="name";
private static final String PASSWORD="password";
private static final String CREATE_TABLE="CREATE TABLE "+TABLE_NAME+"("+ID+" INT PRIMARY KEY AUTO_INCREMENT, "+NAME+" VARCHAR(225), "+PASSWORD+" VARCHAR(225));";
private static final String DROP_TABLE="DROP TABLE IF EXISTS "+TABLE_NAME+"";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
this.context=context;
Message.message(context, "Constructor \n called");
Log.d("LOG-MSG", "Constructor \n called");
}
@Override
public void onCreate(SQLiteDatabase db) {
//CREATE TABLE MyTable(_id INT PRIMARY KEY AUTO_INCREMENT, name VARCHAR(225));
try {
Message.message(context, "onCreate \n called");
Log.d("LOG-MSG", "onCreate \n called");
db.execSQL(CREATE_TABLE);
} catch (SQLException e) {
Message.message(context, ""+e);
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
//DROP TABLE MyTable IF EXISTS;
try {
Message.message(context, "onUpgrade \n called");
Log.d("LOG-MSG", "onUpgrade \n called");
db.execSQL(DROP_TABLE);
onCreate(db);
} catch (SQLException e) {
Message.message(context, ""+e);
}
}
}
}
Log::
04-18 09:47:51.250: W/ActivityThread(6470): Application com.example.sqliteslidenerdproject is waiting for the debugger on port 8100...
04-18 09:47:51.350: I/System.out(6470): Sending WAIT chunk
04-18 09:47:51.600: I/dalvikvm(6470): Debugger is active
04-18 09:47:51.790: I/System.out(6470): Debugger has connected
04-18 09:47:51.790: I/System.out(6470): waiting for debugger to settle...
04-18 09:47:51.990: I/System.out(6470): waiting for debugger to settle...
04-18 09:47:52.191: I/System.out(6470): waiting for debugger to settle...
04-18 09:47:52.390: I/System.out(6470): waiting for debugger to settle...
04-18 09:47:52.600: I/System.out(6470): waiting for debugger to settle...
04-18 09:47:52.800: I/System.out(6470): waiting for debugger to settle...
04-18 09:47:53.002: I/System.out(6470): waiting for debugger to settle...
04-18 09:47:53.202: I/System.out(6470): waiting for debugger to settle...
04-18 09:47:53.410: I/System.out(6470): waiting for debugger to settle...
04-18 09:47:53.610: I/System.out(6470): waiting for debugger to settle...
04-18 09:47:53.820: I/System.out(6470): waiting for debugger to settle...
04-18 09:47:54.020: I/System.out(6470): waiting for debugger to settle...
04-18 09:47:54.220: I/System.out(6470): waiting for debugger to settle...
04-18 09:47:54.420: I/System.out(6470): waiting for debugger to settle...
04-18 09:47:54.630: I/System.out(6470): waiting for debugger to settle...
04-18 09:47:54.830: I/System.out(6470): debugger has settled (1427)
04-18 09:48:06.050: D/gralloc_goldfish(6470): Emulator without GPU emulation detected.
04-18 09:48:28.411: W/System.err(6470): java.lang.NullPointerException
04-18 09:48:28.441: W/System.err(6470): at com.example.sqliteslidenerdproject.MainActivity.onClick(MainActivity.java:47)
04-18 09:48:28.600: W/System.err(6470): at android.view.View.performClick(View.java:3480)
04-18 09:48:28.633: W/System.err(6470): at android.view.View$PerformClick.run(View.java:13983)
04-18 09:48:28.650: W/System.err(6470): at android.os.Handler.handleCallback(Handler.java:605)
04-18 09:48:28.661: W/System.err(6470): at android.os.Handler.dispatchMessage(Handler.java:92)
04-18 09:48:28.681: W/System.err(6470): at android.os.Looper.loop(Looper.java:137)
04-18 09:48:28.702: W/System.err(6470): at android.app.ActivityThread.main(ActivityThread.java:4340)
04-18 09:48:28.721: W/System.err(6470): at java.lang.reflect.Method.invokeNative(Native Method)
04-18 09:48:28.741: W/System.err(6470): at java.lang.reflect.Method.invoke(Method.java:511)
04-18 09:48:28.761: W/System.err(6470): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
04-18 09:48:28.791: W/System.err(6470): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
04-18 09:48:28.801: W/System.err(6470): at dalvik.system.NativeStart.main(Native Method)
04-18 09:48:28.821: D/Log-EXCEPTION(6470): java.lang.NullPointerException
04-18 09:48:32.651: D/LOG-MSG(6470): Successful
Upvotes: 0
Views: 296
Reputation: 47817
I think you forget to initialized DatabaseAdapter adapter
like below:
DatabaseAdapter adapter=new DatabaseAdapter(MainActivity.this);
Your app crashed it's because adapter is null
at adapter.insertData(.....)
on your Button Click
event where you used it.
Update: also you go wrong over here in your Create Table SQL command
"CREATE TABLE "+TABLE_NAME+"("+ID+" INT PRIMARY KEY AUTO_INCREMENT // Remove _ from AUTO_INCREMENT from after primary key and also change INT to INTEGER
Correct your Create Table SQL command
with below
private static final String CREATE_TABLE="CREATE TABLE "+TABLE_NAME+"("+ID+" INTEGER PRIMARY KEY AUTOINCREMENT, "+NAME+" VARCHAR(225), "+PASSWORD+" VARCHAR(225));";
Upvotes: 3
Reputation: 21201
I think you forgot to initialize adapter
variable. That is why you are getting NULL POINTER Exception
.Try to initialize it and run again.
Add this line in onCreate()
adapter = new DatabaseAdapter(MainActivity.this);
Upvotes: 2
Reputation: 1073
I think the adapter is null as you have not created the instance of the class DatabaseAdapter
try with below code.
try {
adapter = new DatabaseAdapter(MainActivity.this);
returnedRes = adapter.insertData(name.getText().toString(), pwd.getText().toString());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.d("Log-EXCEPTION", e.toString());
}
Upvotes: 1