0

Activity:

package hi.com;

import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

public class TestsqliteActivity extends Activity {
       /** Called when the activity is first created. */

    EditText inputContent1, inputContent2,inputContent3;
    Button buttonAdd, buttonDeleteAll;
    ListView listContent;

    Cursor cursor;
    SimpleCursorAdapter cursorAdapter;

    private handle mySQLiteAdapter;
    private SQLiteDatabase db;
    private static final String fields[] = {handle.KEY_ID,handle.COMPANY_NAME};
    private ContentValues conValues;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.asdasd);

        inputContent1 = (EditText)findViewById(R.id.content1);
        inputContent2 = (EditText)findViewById(R.id.content2);
        inputContent3 = (EditText)findViewById(R.id.content3);

        buttonAdd = (Button)findViewById(R.id.add);
        listContent = (ListView)findViewById(R.id.contentlist);

        mySQLiteAdapter = new handle(this);
        db = (new handle(this)).getWritableDatabase();
        Cursor data = db.query(handle.COMPANY_TABLE, fields, null, null, null, null, null);

        String[] from = new String[]{handle.COMPANY_NAME};
        int[] to = new int[]{R.id.text1};
        cursorAdapter = new SimpleCursorAdapter(this, R.layout.row, data, from, to);
        listContent.setAdapter(cursorAdapter);

        buttonAdd.setOnClickListener(buttonAddOnClickListener);
    }

    Button.OnClickListener buttonAddOnClickListener
    = new Button.OnClickListener(){
        @Override
        public void onClick(View arg0) {

        saveRecords();
        }
    };


private void updateList() {
    // TODO Auto-generated method stub
      cursor.requery();
}


protected void saveRecords() {
    // TODO Auto-generated method stub

                conValues.put(handle.COMPANY_NAME, inputContent1.getText().toString());


                try {

                    db.insert(handle.COMPANY_TABLE, null, conValues);

                    Log.d("DDMS'ye", "kayit basarili degerini yaz.");

                } catch (SQLiteException e) {

                    Log.d("eHata", e.getLocalizedMessage());

                }

            }

}

Error

ERROR/AndroidRuntime(19930): java.lang.NullPointerException 10-05 12:01:31.302: ERROR/AndroidRuntime(19930): at hi.com.TestsqliteActivity.saveRecords(TestsqliteActivity.java:75) 10-05 12:01:31.302: ERROR/AndroidRuntime(19930): at hi.com.TestsqliteActivity$1.onClick(TestsqliteActivity.java:61) 10-05 12:01:31.302: ERROR/AndroidRuntime(19930): at android.view.View.performClick(View.java:2408) 10-05 12:01:31.302: ERROR/AndroidRuntime(19930): at android.view.View$PerformClick.run(View.java:8816) 10-05 12:01:31.302: ERROR/AndroidRuntime(19930): at android.os.Handler.handleCallback(Handler.java:587) 10-05 12:01:31.302: ERROR/AndroidRuntime(19930): at android.os.Handler.dispatchMessage(Handler.java:92) 10-05 12:01:31.302: ERROR/AndroidRuntime(19930): at android.os.Looper.loop(Looper.java:123) 10-05 12:01:31.302: ERROR/AndroidRuntime(19930): at android.app.ActivityThread.main(ActivityThread.java:4627) 10-05 12:01:31.302: ERROR/AndroidRuntime(19930): at java.lang.reflect.Method.invokeNative(Native Method) 10-05 12:01:31.302: ERROR/AndroidRuntime(19930): at java.lang.reflect.Method.invoke(Method.java:521) 10-05 12:01:31.302: ERROR/AndroidRuntime(19930): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 10-05 12:01:31.302: ERROR/AndroidRuntime(19930): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 10-05 12:01:31.302: ERROR/AndroidRuntime(19930): at dalvik.system.NativeStart.main(Native Method)

Please advice me on what i have done wrong.... thank you

1
  • Where do you initialize your sqLiteDatabase variable? It seems like you've forgot to do it. Commented Oct 5, 2011 at 6:01

1 Answer 1

1

Be sure, to initialize your SQLiteDatabase object and also open your database. Here is the idea of a DatabaseHelper Class. You can find appropriate code snippets for this class as well.

public DatabaseHelper(Context aContext)
{

    sqlDB = aContext.openOrCreateDatabase(DatabaseName, SQLiteDatabase.CREATE_IF_NECESSARY, null);

    OpenHelper openHelper = new OpenHelper(aContext, sqlDB);

    sqlDB = openHelper.getWritableDatabase();
}

Here sqlDB is your SQLiteDatabase object. And OpenHelper is a helper class to open your database.

OpenHelper(Context context, SQLiteDatabase sql_db)
    {
        super(context, DatabaseName, null,DatabaseVersion);         
        onCreate(sql_db);
    }

You can implement your own constructor and classes. Get more help on this. Its easy to implement and handle using a DatabaseHelper class.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.