Search code examples
javaandroidsqliteandroid-studioandroid-search

Error in search result Android SQLite


I created an app that shows a list of places and i created a search dialog wherein the user will type in edittext so he/she will find the desired place. but i got an error that i will post below.

Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference at com.example.ubaldo.myapplication.Place$4$1.onClick(Place.java:221)

This is the line 221 in my code

GetSearchPlace = dbhelper.getPlaceSearch(placeLocationEditText.getText().toString());

This is my DatabaseHelper

public List<PlaceModel> getPlaceSearch(String location) {
        List<PlaceModel> search = new ArrayList<PlaceModel>();

        String selectQuery = "SELECT * FROM listing_place where province_name like '" + location + "'";
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);

        if (cursor.moveToFirst()) {
            do {
                PlaceModel pm = new PlaceModel();
                pm.setlisting_title(cursor.getString(cursor.getColumnIndex(KEY_PLACE)));
                search.add(pm);
            }
            while (cursor.moveToNext());
        }
        cursor.close();
        return search;
    }

This is my main activity

 placeLocationEditText = (EditText)findViewById(R.id.placelocation);

                Button button = (Button) dialog.findViewById(R.id.btnplacesearch);

                button.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dialog.dismiss();

                        PlaceListView = findViewById(R.id.placelayout);
                        ViewGroup parent = (ViewGroup) PlaceListView.getParent();
                        parent.removeView(PlaceListView);
                        PlaceSearchView = getLayoutInflater().inflate(R.layout.searchresult_place, parent, false);
                        parent.addView(PlaceSearchView);

                        GetSearchPlace = dbhelper.getPlaceSearch(placeLocationEditText.getText().toString());
                        lv2 = (ListView) findViewById(R.id.searchplace_list);
                        lv2.setAdapter(new ViewAdapterSearchPlace());

Solution

  • replace

     placeLocationEditText = (EditText)findViewById(R.id.placelocation);
    

    with

     placeLocationEditText = (EditText)dialog.findViewById(R.id.placelocation);