I am new to android programming, I am trying to develop a basic SQLite application which is throwing runtime exception. For this purpose I have divided code into three classes which are Mainactivity, DBhelper and Message. I am not getting where actually the problem is in the code. Logcat when running in AVD I have given below.
03-18 20:07:56.904: E/Trace(736): error opening trace file: No such file or directory (2)
03-18 20:07:57.434: D/AndroidRuntime(736): Shutting down VM
03-18 20:07:57.474: W/dalvikvm(736): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
03-18 20:07:57.494: E/AndroidRuntime(736): FATAL EXCEPTION: main
03-18 20:07:57.494: E/AndroidRuntime(736): java.lang.RuntimeException: Unable to start activity ComponentInfo{one.example.sqlitetesting/one.example.sqlitetesting.MainActivity}: java.lang.NullPointerException
03-18 20:07:57.494: E/AndroidRuntime(736): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
03-18 20:07:57.494: E/AndroidRuntime(736): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
03-18 20:07:57.494: E/AndroidRuntime(736): at android.app.ActivityThread.access$600(ActivityThread.java:130)
03-18 20:07:57.494: E/AndroidRuntime(736): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
03-18 20:07:57.494: E/AndroidRuntime(736): at android.os.Handler.dispatchMessage(Handler.java:99)
03-18 20:07:57.494: E/AndroidRuntime(736): at android.os.Looper.loop(Looper.java:137)
03-18 20:07:57.494: E/AndroidRuntime(736): at android.app.ActivityThread.main(ActivityThread.java:4745)
03-18 20:07:57.494: E/AndroidRuntime(736): at java.lang.reflect.Method.invokeNative(Native Method)
03-18 20:07:57.494: E/AndroidRuntime(736): at java.lang.reflect.Method.invoke(Method.java:511)
03-18 20:07:57.494: E/AndroidRuntime(736): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
03-18 20:07:57.494: E/AndroidRuntime(736): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
03-18 20:07:57.494: E/AndroidRuntime(736): at dalvik.system.NativeStart.main(Native Method)
03-18 20:07:57.494: E/AndroidRuntime(736): Caused by: java.lang.NullPointerException
03-18 20:07:57.494: E/AndroidRuntime(736): at android.widget.Toast.<init>(Toast.java:92)
03-18 20:07:57.494: E/AndroidRuntime(736): at android.widget.Toast.makeText(Toast.java:238)
03-18 20:07:57.494: E/AndroidRuntime(736): at one.example.sqlitetesting.Message.<init>(Message.java:9)
03-18 20:07:57.494: E/AndroidRuntime(736): at one.example.sqlitetesting.DbHelper.<init>(DbHelper.java:19)
03-18 20:07:57.494: E/AndroidRuntime(736): at one.example.sqlitetesting.MainActivity.onCreate(MainActivity.java:14)
03-18 20:07:57.494: E/AndroidRuntime(736): at android.app.Activity.performCreate(Activity.java:5008)
03-18 20:07:57.494: E/AndroidRuntime(736): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
03-18 20:07:57.494: E/AndroidRuntime(736): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
03-18 20:07:57.494: E/AndroidRuntime(736): ... 11 more
03-18 20:08:01.104: I/Process(736): Sending signal. PID: 736 SIG: 9`
Here is Mainactivity class is:
package one.example.sqlitetesting;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class MainActivity extends Activity {
DbHelper X;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
X=new DbHelper(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Here is DBhelper class is:
package one.example.sqlitetesting;
import android.content.Context;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DbHelper extends SQLiteOpenHelper {
static String AKHIL_DATABASE="mydatabase";
String TABLE_NAME="usersdata";
static int version=1;
Context context;
//this is a super class constructor which
public DbHelper(Context context) {
super(context, AKHIL_DATABASE, null, version);//parameters: context,name of DB, custom cursor(null:defaul),version
context=this.context;
Message m=new Message(context, "constructer called");
}
@Override
public void onCreate(SQLiteDatabase db) {
//create table usersdata (_id INTEGER PRIMARY KEY AUTOINCREMENT,name VARCHAR(255));
String query="create table "+TABLE_NAME+" usersdata (_id INTEGER PRIMARY KEY AUTOINCREMENT,name VARCHAR(255));";
try {
db.execSQL(query);
Message m=new Message(context, "onccreate is called");
} catch (SQLException e) {
// TODO Auto-generated catch block
Message m=new Message(context, ""+e);
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int arg1, int arg2) {
try {
db.execSQL("drop table userdata if exists;");
onCreate(db);
} catch (SQLException e) {
// TODO Auto-generated catch block
}
}
}
Here is Message class which gives toast messages at different levels: package one.example.sqlitetesting;
import android.content.Context;
import android.widget.Toast;
public class Message {
public Message(Context context,String message)
{
Toast.makeText(context, message, Toast.LENGTH_LONG).show();
}
}