0

I made a table in SQL that stores id as a string. Now when I want to retrieve a particular record by comparing the string id, it is returning null pointer exception. I don't know how to solve this.

here is the activity that calls the database by passing the string

SearchDb.java

import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class SearchDb extends Activity implements OnClickListener {

    EditText enid;
    Button search;
    TextView id, name, age;
    String s;

    Bundle value;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.searchdb);

        enid = (EditText) findViewById(R.id.et_enid);
        search = (Button) findViewById(R.id.bsearch);
        id = (TextView) findViewById(R.id.tv_enid);
        name = (TextView) findViewById(R.id.tv_name);
        age = (TextView) findViewById(R.id.tv_age);

        search.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.bsearch:
            try {
                s = enid.getText().toString();

                CreateDB fetch = new CreateDB(this);
                value = fetch.fetchData(s);
                fetch.close();

                id.setText(value.getString("id"));
                name.setText(value.getString("name"));
                age.setText(value.getString("age"));

            } catch (Exception e) {
                Dialog d = new Dialog(this);
                TextView tv = new TextView(this);
                tv.setText(e.toString());
                d.setTitle("Error!!");
                d.setContentView(tv);
                d.show();

            }
        }
        // TODO Auto-generated method stub

    }

}

the CreateDB class constructor is--

public CreateDB(Context cxt) {
    myContext = cxt;
    myDB = myContext.openOrCreateDatabase(DbName, DbVersion, null);
}

and the method to search is like this--

public Bundle fetchData(String l) throws SQLException {


        String[] chck=new String[]{l};
        Cursor c = myDB.query(TbName, cols, KeyId+"=?", chck, null, null, null);
        if (c != null) {
            int iName = c.getColumnIndex(KeyName);
            int iAge = c.getColumnIndex(KeyAge);
            int iId = c.getColumnIndex(KeyId);

            c.moveToFirst();

            send.putString("id", c.getString(iId));
            send.putString("name", c.getString(iName));
            send.putString("age", c.getString(iAge));

            return send;
        }
        // TODO Auto-generated method stub
        return null;
    }

Finally I am getting a java.lang.NullPointerException in LogCat.

Please help!

6
  • Can you post you error log, that is logcat? Commented Mar 19, 2014 at 9:40
  • I tried to post a picture of my emulator, but i need to have 15 reputation for that. Since i am a new user, i dont have enough reputation. Commented Mar 19, 2014 at 9:41
  • 1
    No need to post image just copy your logcat and paste it here as a text. Commented Mar 19, 2014 at 9:44
  • my logcat does not post an error, since i have put the code in try and catch and am then printing the error as a dialog. i just checked my logcat, it is not giving any error statement at all. Commented Mar 19, 2014 at 9:53
  • Okay, can you post you full activity code. Commented Mar 19, 2014 at 9:55

1 Answer 1

1

Your code is totally correct. It is returning error because the bundle in which the values are stored is not initialized. So it becomes null hence null pointer exception error. Just initialize your bundle as Bundle send=new Bundle();. Then your code will work fine.

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.