I found this source code from a tutorial but it crashes when I run it.
My logcat:
01-25 00:26:30.898: E/AndroidRuntime(2762): FATAL EXCEPTION: main
01-25 00:26:30.898: E/AndroidRuntime(2762): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.pinchtapzoom/com.pinchtapzoom.AssignmentTracker}: java.lang.NullPointerException
01-25 00:26:30.898: E/AndroidRuntime(2762): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1967)
01-25 00:26:30.898: E/AndroidRuntime(2762): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1992)
01-25 00:26:30.898: E/AndroidRuntime(2762): at android.app.ActivityThread.access$600(ActivityThread.java:127)
01-25 00:26:30.898: E/AndroidRuntime(2762): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1158)
01-25 00:26:30.898: E/AndroidRuntime(2762): at android.os.Handler.dispatchMessage(Handler.java:99)
01-25 00:26:30.898: E/AndroidRuntime(2762): at android.os.Looper.loop(Looper.java:137)
01-25 00:26:30.898: E/AndroidRuntime(2762): at android.app.ActivityThread.main(ActivityThread.java:4441)
01-25 00:26:30.898: E/AndroidRuntime(2762): at java.lang.reflect.Method.invokeNative(Native Method)
01-25 00:26:30.898: E/AndroidRuntime(2762): at java.lang.reflect.Method.invoke(Method.java:511)
01-25 00:26:30.898: E/AndroidRuntime(2762): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
01-25 00:26:30.898: E/AndroidRuntime(2762): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
01-25 00:26:30.898: E/AndroidRuntime(2762): at dalvik.system.NativeStart.main(Native Method)
01-25 00:26:30.898: E/AndroidRuntime(2762): Caused by: java.lang.NullPointerException
01-25 00:26:30.898: E/AndroidRuntime(2762): at com.pinchtapzoom.AssignmentTracker.onCreate(AssignmentTracker.java:70)
01-25 00:26:30.898: E/AndroidRuntime(2762): at android.app.Activity.performCreate(Activity.java:4465)
01-25 00:26:30.898: E/AndroidRuntime(2762): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
01-25 00:26:30.898: E/AndroidRuntime(2762): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1931)
01-25 00:26:30.898: E/AndroidRuntime(2762): ... 11 more
This is where the code starts to crash:
//---retrieves all the records---
public Cursor getAllRecords()
{
db = DBHelper.getWritableDatabase();
Cursor mCursor = db.query(DATABASE_TABLE, new String[] {KEY_ROWID, KEY_TITLE,
KEY_DUEDATE, KEY_COURSE, KEY_NOTES}, null, null, null, null, null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
and
//---get all Records---
db.open();
Cursor c = db.getAllRecords();
startManagingCursor(c);
if (c.moveToFirst())
{
do {
DisplayRecord(c);
} while (c.moveToNext());
}
db.close();
Here is the code for AssignmentTracker.java:
<code>
package com.pinchtapzoom;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.Toast;
public class AssignmentTracker extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button addBtn = (Button)findViewById(R.id.add);
addBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(AssignmentTracker.this, addassignment.class);
startActivity(i);
}
});
try {
String destPath = "/data/data/" + getPackageName() + "/databases/AssignmentDB.db";
File f = new File(destPath);
if (!f.exists()) {
CopyDB( getBaseContext().getAssets().open("mydb"),
new FileOutputStream(destPath));
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
DBAdapter db = new DBAdapter(this);
//---add an assignment---
db.open();
long id = db.insertRecord("Hello World", "2/18/2012", "DPR 224", "First Android Project");
id = db.insertRecord("Workbook Exercises", "3/1/2012", "MAT 100", "Do odd numbers");
db.close();
//---get all Records---
db.open();
Cursor c = db.getAllRecords();
startManagingCursor(c);
if (c.moveToFirst())
{
do {
DisplayRecord(c);
} while (c.moveToNext());
}
db.close();
/*
//---get a Record---
db.open();
Cursor c = db.getRecord(2);
if (c.moveToFirst())
DisplayRecord(c);
else
Toast.makeText(this, "No Assignments found", Toast.LENGTH_LONG).show();
db.close();
*/
//---update Record---
/*
db.open();
if (db.updateRecord(1, "Hello Android", "2/19/2012", "DPR 224", "First Android Project"))
Toast.makeText(this, "Update successful.", Toast.LENGTH_LONG).show();
else
Toast.makeText(this, "Update failed.", Toast.LENGTH_LONG).show();
db.close();
*/
/*
//---delete a Record---
db.open();
if (db.deleteRecord(1))
Toast.makeText(this, "Delete successful.", Toast.LENGTH_LONG).show();
else
Toast.makeText(this, "Delete failed.", Toast.LENGTH_LONG).show();
db.close();
*/
}
private class DBAdapter extends BaseAdapter {
private LayoutInflater mInflater;
public DBAdapter(AssignmentTracker assignmentTracker) {
// TODO Auto-generated constructor stub
}
public Cursor getAllRecords() {
// TODO Auto-generated method stub
return null;
}
public Cursor getAllRecords(int i, int j) {
// TODO Auto-generated method stub
return null;
}
//private ArrayList<>
public void close() {
// TODO Auto-generated method stub
}
public long insertRecord(String string, String string2, String string3,
String string4) {
// TODO Auto-generated method stub
return 0;
}
public void open() {
// TODO Auto-generated method stub
}
@Override
public int getCount() {
return 0;
}
@Override
public Object getItem(int arg0) {
return null;
}
@Override
public long getItemId(int arg0) {
return 0;
}
@Override
public View getView(int arg0, View arg1, ViewGroup arg2) {
return null;
}
}
public void CopyDB(InputStream inputStream, OutputStream outputStream)
throws IOException {
//---copy 1K bytes at a time---
byte[] buffer = new byte[1024];
int length;
while ((length = inputStream.read(buffer)) > 0) {
outputStream.write(buffer, 0, length);
}
inputStream.close();
outputStream.close();
}
public void DisplayRecord(Cursor c)
{
Toast.makeText(this,
"id: " + c.getString(0) + "\n" +
"Title: " + c.getString(1) + "\n" +
"Due Date: " + c.getString(2),
Toast.LENGTH_SHORT).show();
}
public void addAssignment(View view)
{
Intent i = new Intent("com.pinchtapzoom.addassignment");
startActivity(i);
Log.d("TAG", "Clicked");
}
}
Here is the code for DBAdapter.java:
package com.pinchtapzoom;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class DBAdapter {
public static final String KEY_ROWID = "id";
public static final String KEY_TITLE = "title";
public static final String KEY_DUEDATE = "duedate";
public static final String KEY_COURSE = "course";
public static final String KEY_NOTES = "notes";
private static final String TAG = "DBAdapter";
private static final String DATABASE_NAME = "AssignmentsDB.db";
private static final String DATABASE_TABLE = "assignments";
private static final int DATABASE_VERSION = 2;
private static final String DATABASE_CREATE =
"create table if not exists assignments (id integer primary key autoincrement, "
+ "title VARCHAR not null, duedate date, course VARCHAR, notes VARCHAR );";
private final Context context;
private DatabaseHelper DBHelper;
private SQLiteDatabase db;
public DBAdapter(Context ctx)
{
this.context = ctx;
DBHelper = new DatabaseHelper(context);
}
private static class DatabaseHelper extends SQLiteOpenHelper
{
DatabaseHelper(Context context)
{
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db)
{
try {
db.execSQL(DATABASE_CREATE);
} catch (SQLException e) {
e.printStackTrace();
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS contacts");
onCreate(db);
}
}
//---opens the database---
public DBAdapter open() throws SQLException
{
db = DBHelper.getWritableDatabase();
return this;
}
//---closes the database---
public void close()
{
DBHelper.close();
}
//---insert a record into the database---
public long insertRecord(String title, String duedate, String course, String notes)
{
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_TITLE, title);
initialValues.put(KEY_DUEDATE, duedate);
initialValues.put(KEY_COURSE, course);
initialValues.put(KEY_NOTES, notes);
return db.insert(DATABASE_TABLE, null, initialValues);
}
//---deletes a particular record---
public boolean deleteContact(long rowId)
{
return db.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) > 0;
}
//---retrieves all the records---
public Cursor getAllRecords()
{
db = DBHelper.getWritableDatabase();
Cursor mCursor = db.query(DATABASE_TABLE, new String[] {KEY_ROWID, KEY_TITLE,
KEY_DUEDATE, KEY_COURSE, KEY_NOTES}, null, null, null, null, null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
//---retrieves a particular record---
public Cursor getRecord(long rowId) throws SQLException
{
Cursor mCursor =
db.query(true, DATABASE_TABLE, new String[] {KEY_ROWID,
KEY_TITLE, KEY_DUEDATE, KEY_COURSE, KEY_NOTES},
KEY_ROWID + "=" + rowId, null, null, null, null, null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
//---updates a record---
public boolean updateRecord(long rowId, String title, String duedate, String course, String notes)
{
ContentValues args = new ContentValues();
args.put(KEY_TITLE, title);
args.put(KEY_DUEDATE, duedate);
args.put(KEY_COURSE, course);
args.put(KEY_NOTES, notes);
return db.update(DATABASE_TABLE, args, KEY_ROWID + "=" + rowId, null) > 0;
}
}
The tutorial is: here
It stops working when I uncommented the code.
There are lots of questions about this question but no clear solution.
I just started learning about SQLite so need some help to find the solution.
Thanks in advance
The problem is that you have two classes named
DBAdapterand you are using the wrong one…Simply remove the entire DBAdapter class that starts with:
This
DBAdapterinside AssignmentTracker does nothing anyway.