0

I am creating an app Where the user can search database with inputs provided by user through edittext boxes.I am passing them to the database file as string argument to query and I am getting error.attaching the code

This is the button click listener and autoname is Autotextview

 b1.setOnClickListener(new View.OnClickListener() {
 @Override
 public void onClick(View v) {

 MyDatabase m =new MyDatabase(getActivity());
 Cursor c=m.searchname(autoname.getText().toString());

 simpleCursorAdapter = new SimpleCursorAdapter(getActivity(),     R.layout.custom_layout_row, c,
                    new String[]{"_id", "name", "phone", "email", "subject"},
                    new int[]{R.id.rownumber, R.id.rowname, R.id.rowphone, R.id.rowmail, R.id.rowsubject}, 0);
            listView.setAdapter(simpleCursorAdapter);


        }
    });

This is the code in Mydatabase.java(Database file)

    public Cursor searchname(String argument) {
    Cursor cursor = null;
    cursor = s.query("student", null, "name=?", new String[]{argument}, null, null, null);


    return cursor;
    }

I am getting error in the query string

if i run raw query

Cursor cursor = db.rawQuery("select * from student where name =?", new String[]{argument});

i got this error:

java.lang.NullPointerException: Attempt to invoke virtual method 'android.database.Cursor android.database.sqlite.SQLiteDatabase.rawQuery(java.lang.String, java.lang.String[])' on a null object reference at com.techpalle.miniproject.MyDatabase.searchname(MyDatabase.java:107) at com.techpalle.miniproject.SearchFragment$2.onClick(SearchFragment.java:71)

Yazan
  • 6,074
  • 1
  • 19
  • 33
Phillen
  • 336
  • 5
  • 18

1 Answers1

2

The error says you are trying to run rawQuery on a null object :) That means your s is null :) You should get a writable database reference before you could execute any query on that :)

So you can try this :)

public Cursor searchname(String argument) {
     Cursor cursor = null;
     SQLiteDatabase s = this.getWritableDatabase(); 
     Cursor cursor = s.rawQuery("select * from student where name =?", new String[]{argument});
     return cursor;
}

Lemme know if you still have the issue :) Happy coding buddy :)

Sandeep Bhandari
  • 19,999
  • 5
  • 45
  • 78
  • Thank you So much.It worked fine. – Phillen Apr 07 '16 at 13:54
  • @Phillen Always welcome buddy :) Glad I could help :) – Sandeep Bhandari Apr 07 '16 at 13:55
  • this.getWritableDatabase(); was also having trouble so used database helper object instead of this..and it worked. Thank you so much again – Phillen Apr 07 '16 at 13:57
  • Isn't your Mydatabase.java extended SQLiteOpenHelper buddy ??? I usually extend my database files with SQLiteOpenHelper so I wrote this :) What was the issue with this.getWritableDatabase(); curious to know :) – Sandeep Bhandari Apr 07 '16 at 13:59
  • getWritableDatabase was in red and clicking on it showed --cannot resolve method getwritabledatabase-- and I am a beginner so I don't know in depth.I haven't extended from SQLLiteOpenHelper. I have imported it.I will extend and let you know the results. – Phillen Apr 07 '16 at 14:04
  • @Phillen If you extend you will have lot more things to tackle buddy :) If ok then dont extend on the other hand if you want to learn it completely here is atutorial you can make use of :) http://www.androidhive.info/2011/11/android-sqlite-database-tutorial/ – Sandeep Bhandari Apr 07 '16 at 14:07
  • I tried extending from the SQLiteOpenHelper and it asked me to changemy class to abstract.done it and it throwed red line on my default constructor saying there is no default constructor for the SQLiteOpenHelper Class.I am newbie so I think it will take a long time to figure what is wrong. – Phillen Apr 07 '16 at 14:07
  • Thank you so much for the tutorial.Let me go through it.You are a Star.Thanks again. – Phillen Apr 07 '16 at 14:08
  • @Phillen : did you read my previous comment buddy :) extending your data base classes is a good practice :) but when you do it you have to tackle few things :) Read the tutorial carefully :) learn it then you will understand what were you missing in your code :) Happy coding buddy :) and finally if its not too much to ask for, and if you are really happy with the answer, please accept it :) Happy coding :) – Sandeep Bhandari Apr 07 '16 at 14:10