-2

I am using a http request to get the data in JSON format which loads the image as well. I am trying to implement onTextChanged function but it always crashes whenever I enter any alphabet into the EditText box.

/** AsyncTask to parse json data and load ListView */
        private class ListViewLoaderTask extends AsyncTask<String, Void, SimpleAdapter>{
            // Doing the parsing of xml data in a non-ui thread
            @Override
            protected SimpleAdapter doInBackground(String... strJson) {
                try{
                    jObject = new JSONObject(strJson[0]);
                    StoreJSONParser countryJsonParser = new StoreJSONParser();
                    countryJsonParser.parse(jObject);
                }catch(Exception e){
                    Log.d("JSON Exception1",e.toString());
                }

                // Instantiating json parser class
                //StoreJSONParser countryJsonParser = new StoreJSONParser();

                try{
                    // Getting the parsed data as a List construct
                    countries = countryJsonParser.parse(jObject);
                }catch(Exception e){
                    Log.d("Exception",e.toString());
                }

                // Instantiating an adapter to store each items
                // R.layout.listview_layout defines the layout of each item
                //adapter = new SimpleAdapter(getBaseContext(), countries, R.layout.restaurant_list, from, to);
                return adapter;
            }

onPostExecute

protected void onPostExecute(SimpleAdapter adapter) {

            // Setting adapter for the listview
            adapter = new SimpleAdapter(getBaseContext(), countries, R.layout.restaurant_list, from, to);
            Log.d("What is country", String.valueOf(countries));
            mListView.setAdapter(adapter);
            searchBox = (EditText)findViewById(R.id.searchBox);

            searchBox.addTextChangedListener(new TextWatcher() {

                public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
                    // When user changed the Text
                    ChooseCategory.this.adapter.getFilter().filter(cs);
                }

                public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
                                              int arg3) {
                    // TODO Auto-generated method stub

                }

                public void afterTextChanged(Editable arg0) {
                    // TODO Auto-generated method stub
                    //ChooseCategory.this.adapter.getFilter().filter(arg0);
                }
            });

            for(int i=0;i<adapter.getCount();i++){
                HashMap<String, Object> hm = (HashMap<String, Object>) adapter.getItem(i);
                String imgUrl = (String) hm.get("flag_path");
                ImageLoaderTask imageLoaderTask = new ImageLoaderTask();

                HashMap<String, Object> hmDownload = new HashMap<String, Object>();
                hm.put("flag_path",imgUrl);
                hm.put("position", i);

                // Starting ImageLoaderTask to download and populate image in the listview
                imageLoaderTask.execute(hm);
            }
        }
    }

The error from logcat:

java.lang.NullPointerException
            at ChooseCategory$ListViewLoaderTask$1.onTextChanged(ChooseCategory.java:196)
            at android.widget.TextView.sendOnTextChanged(TextView.java:7408)
            at android.widget.TextView.handleTextChanged(TextView.java:7467)
            at android.widget.TextView$ChangeWatcher.onTextChanged(TextView.java:9183)
            at android.text.SpannableStringBuilder.sendTextChanged(SpannableStringBuilder.java:962)
            at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:496)
            at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:435)
            at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:30)
            at android.text.method.QwertyKeyListener.onKeyDown(QwertyKeyListener.java:223)
            at android.text.method.TextKeyListener.onKeyDown(TextKeyListener.java:136)
            at android.widget.TextView.doKeyDown(TextView.java:5532)
            at android.widget.TextView.onKeyDown(TextView.java:5343)
            at android.view.KeyEvent.dispatch(KeyEvent.java:2640)
            at android.view.View.dispatchKeyEvent(View.java:7665)
            at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1408)
            at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1408)
            at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1408)
            at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1408)
            at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchKeyEvent(PhoneWindow.java:2035)
            at com.android.internal.policy.impl.PhoneWindow.superDispatchKeyEvent(PhoneWindow.java:1505)
            at android.app.Activity.dispatchKeyEvent(Activity.java:2418)
            at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1962)
            at android.view.ViewRootImpl$ViewPostImeInputStage.processKeyEvent(ViewRootImpl.java:3852)
            at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:3826)
            at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3399)
            at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3449)
            at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3418)
            at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3525)
            at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3426)
            at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3582)
            at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3399)
            at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3449)
            at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3418)
            at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3426)
            at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3399)
            at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3449)
            at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3418)
            at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3558)
            at android.view.ViewRootImpl$ImeInputStage.onFinishedInputEvent(ViewRootImpl.java:3718)
            at android.view.inputmethod.InputMethodManager$PendingEvent.run(InputMethodManager.java:2010)
            at android.view.inputmethod.InputMethodManager.invokeFinishedInputEventCallback(InputMethodManager.java:1704)
            at android.view.inputmethod.InputMethodManager.finishedInputEvent(InputMethodManager.java:1695)
            at android.view.inputmethod.InputMethodManager$ImeInputEventSender.onInputEventFinished(InputMethodManager.java:1987)
            at android.view.InputEventSender.dispatchInputEventFinished(InputEventSender.java:141)
            at android.os.MessageQueue.nativePollOnce(Native Method)
            at android.os.MessageQueue.next(MessageQueue.java:138)
            at android.os.Looper.loop(Looper.java:123)
            at android.app.ActivityThread.main(ActivityThread.java:5017)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
            at dalvik.system.NativeStart.main(Native Method)
    java.lang.NullPointerException
                at ChooseCategory$ListViewLoaderTask$1.onTextChanged

Please dont make this as a duplicate. I have seen that post and I still can't fix it. That is why I'm posting here.

This is the full code for ChooseCategory.java

public class ChooseCategory extends Activity {
    ListView mListView;
    EditText searchBox;
    SimpleAdapter adapter;
    JSONObject jObject;
    StoreJSONParser countryJsonParser = new StoreJSONParser();
    // A list object to store the parsed countries list
    List<HashMap<String, Object>> countries = null;
    // Keys used in Hashmap
    String[] from = { "country","flag","details"};
    // Ids of views in listview_layout
    int[] to = { R.id.storename,R.id.icon,R.id.details};



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Getting a reference to ListView of activity_main
        mListView = (ListView) findViewById(R.id.list);

        // URL to the JSON data
        String strUrl = "http://10.0.2.2/get_stores.php";

        // Creating a new non-ui thread task to download json data
        DownloadTask downloadTask = new DownloadTask();

        // Starting the download process
        downloadTask.execute(strUrl);


    }

    /** A method to download json data from url */
    private String downloadUrl(String strUrl) throws IOException {
        String data = "";
        InputStream iStream = null;
        try{
            URL url = new URL(strUrl);

            // Creating an http connection to communicate with url
            HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();

            // Connecting to url
            urlConnection.connect();

            // Reading data from url
            iStream = urlConnection.getInputStream();

            BufferedReader br = new BufferedReader(new InputStreamReader(iStream));

            StringBuffer sb  = new StringBuffer();

            String line = "";
            while( ( line = br.readLine())  != null){
                sb.append(line);
            }

            data = sb.toString();

            br.close();

        }catch(Exception e){
            Log.d("Exception:", e.toString());
        }finally{
            iStream.close();
        }

        return data;
    }

    /** AsyncTask to download json data */
    private class DownloadTask extends AsyncTask<String, Integer, String>{
        String data = null;
        @Override
        protected String doInBackground(String... url) {
            try{
                data = downloadUrl(url[0]);
            }catch(Exception e){
                Log.d("Background Task",e.toString());
            }
            return data;
        }

        @Override
        protected void onPostExecute(String result) {

            // The parsing of the xml data is done in a non-ui thread
            ListViewLoaderTask listViewLoaderTask = new ListViewLoaderTask();

            // Start parsing xml data
            listViewLoaderTask.execute(result);
        }
    }

    /** AsyncTask to parse json data and load ListView */
        private class ListViewLoaderTask extends AsyncTask<String, Void, SimpleAdapter>{
            // Doing the parsing of xml data in a non-ui thread
            @Override
            protected SimpleAdapter doInBackground(String... strJson) {
                try{
                    jObject = new JSONObject(strJson[0]);
                    StoreJSONParser countryJsonParser = new StoreJSONParser();
                    countryJsonParser.parse(jObject);
                }catch(Exception e){
                    Log.d("JSON Exception1",e.toString());
                }

                // Instantiating json parser class
                //StoreJSONParser countryJsonParser = new StoreJSONParser();

                try{
                    // Getting the parsed data as a List construct
                    countries = countryJsonParser.parse(jObject);
                }catch(Exception e){
                    Log.d("Exception",e.toString());
                }

                // Instantiating an adapter to store each items
                // R.layout.listview_layout defines the layout of each item
                //adapter = new SimpleAdapter(getBaseContext(), countries, R.layout.restaurant_list, from, to);
                return adapter;
            }

        /** Invoked by the Android on "doInBackground" is executed */
        @Override
        protected void onPostExecute(SimpleAdapter adapter) {

            // Setting adapter for the listview
            adapter = new SimpleAdapter(getBaseContext(), countries, R.layout.restaurant_list, from, to);
            Log.d("What is country", String.valueOf(countries));
            mListView.setAdapter(adapter);
            searchBox = (EditText)findViewById(R.id.searchBox);

            searchBox.addTextChangedListener(new TextWatcher() {

                public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
                    // When user changed the Text
                    ChooseCategory.this.adapter.getFilter().filter(cs);
                }

                public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
                                              int arg3) {
                    // TODO Auto-generated method stub

                }

                public void afterTextChanged(Editable arg0) {
                    // TODO Auto-generated method stub
                    //ChooseCategory.this.adapter.getFilter().filter(arg0);
                }
            });

            for(int i=0;i<adapter.getCount();i++){
                HashMap<String, Object> hm = (HashMap<String, Object>) adapter.getItem(i);
                String imgUrl = (String) hm.get("flag_path");
                ImageLoaderTask imageLoaderTask = new ImageLoaderTask();

                HashMap<String, Object> hmDownload = new HashMap<String, Object>();
                hm.put("flag_path",imgUrl);
                hm.put("position", i);

                // Starting ImageLoaderTask to download and populate image in the listview
                imageLoaderTask.execute(hm);
            }
        }
    }

    /** AsyncTask to download and load an image in ListView */
    private class ImageLoaderTask extends AsyncTask<HashMap<String, Object>, Void, HashMap<String, Object>>{

        @Override
        protected HashMap<String, Object> doInBackground(HashMap<String, Object>... hm) {

            InputStream iStream=null;
            String imgUrl = (String) hm[0].get("flag_path");
            int position = (Integer) hm[0].get("position");

            URL url;
            try {
                url = new URL(imgUrl);

                // Creating an http connection to communicate with url
                HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();

                // Connecting to url
                urlConnection.connect();

                // Reading data from url
                iStream = urlConnection.getInputStream();

                // Getting Caching directory
                File cacheDirectory = getBaseContext().getCacheDir();

                // Temporary file to store the downloaded image
                File tmpFile = new File(cacheDirectory.getPath() + "/wpta_"+position+".png");

                // The FileOutputStream to the temporary file
                FileOutputStream fOutStream = new FileOutputStream(tmpFile);

                // Creating a bitmap from the downloaded inputstream
                Bitmap b = BitmapFactory.decodeStream(iStream);

                // Writing the bitmap to the temporary file as png file
                b.compress(Bitmap.CompressFormat.PNG,100, fOutStream);

                // Flush the FileOutputStream
                fOutStream.flush();

                //Close the FileOutputStream
                fOutStream.close();

                // Create a hashmap object to store image path and its position in the listview
                HashMap<String, Object> hmBitmap = new HashMap<String, Object>();

                // Storing the path to the temporary image file
                hmBitmap.put("flag",tmpFile.getPath());

                // Storing the position of the image in the listview
                hmBitmap.put("position",position);

                // Returning the HashMap object containing the image path and position
                return hmBitmap;

            }catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(HashMap<String, Object> result) {
            // Getting the path to the downloaded image
            String path = (String) result.get("flag");

            // Getting the position of the downloaded image
            int position = (Integer) result.get("position");

            // Getting adapter of the listview
            SimpleAdapter adapter = (SimpleAdapter ) mListView.getAdapter();

            // Getting the hashmap object at the specified position of the listview
            HashMap<String, Object> hm = (HashMap<String, Object>) adapter.getItem(position);

            // Overwriting the existing path in the adapter
            hm.put("flag",path);

            // Noticing listview about the dataset changes
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        //getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

}

3 Answers3

0

Do you have a class variable named adapter ? It seems ChooseCategory.this.adapter is not the local variables adapter = new SimpleAdapter(getBaseContext(), countries, R.layout.restaurant_list, from, to);

you should name the class variable mAdapter.

0

Change in your Asyn class as follows:

private class ListViewLoaderTask extends AsyncTask<String, Void, String>{  **//Change SimpleAdapter to String**
            // Doing the parsing of xml data in a non-ui thread
            @Override **//Change SimpleAdapter to String**
            protected String doInBackground(String... strJson) {
                try{
                    jObject = new JSONObject(strJson[0]);
                    StoreJSONParser countryJsonParser = new StoreJSONParser();
                    countryJsonParser.parse(jObject);
                }catch(Exception e){
                    Log.d("JSON Exception1",e.toString());
                }

                // Instantiating json parser class
                //StoreJSONParser countryJsonParser = new StoreJSONParser();

                try{
                    // Getting the parsed data as a List construct
                    countries = countryJsonParser.parse(jObject);
                }catch(Exception e){
                    Log.d("Exception",e.toString());
                }

                // Instantiating an adapter to store each items
                // R.layout.listview_layout defines the layout of each item
                //adapter = new SimpleAdapter(getBaseContext(), countries, R.layout.restaurant_list, from, to);
                return null;
            }

        /** Invoked by the Android on "doInBackground" is executed */
        @Override **//Change SimpleAdapter to String**
        protected void onPostExecute(String result) {

            // Setting adapter for the listview
            adapter = new SimpleAdapter(ChooseCategory.this, countries, R.layout.restaurant_list, from, to);
            Log.d("What is country", String.valueOf(countries));
            mListView.setAdapter(adapter);
            searchBox = (EditText)findViewById(R.id.searchBox);

            searchBox.addTextChangedListener(new TextWatcher() {

                public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
                    // When user changed the Text
                   adapter.getFilter().filter(cs);
                }

                public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
                                              int arg3) {
                    // TODO Auto-generated method stub

                }

                public void afterTextChanged(Editable arg0) {
                    // TODO Auto-generated method stub
                    //ChooseCategory.this.adapter.getFilter().filter(arg0);
                }
            });

            for(int i=0;i<adapter.getCount();i++){
                HashMap<String, Object> hm = (HashMap<String, Object>) adapter.getItem(i);
                String imgUrl = (String) hm.get("flag_path");
                ImageLoaderTask imageLoaderTask = new ImageLoaderTask();

                HashMap<String, Object> hmDownload = new HashMap<String, Object>();
                hm.put("flag_path",imgUrl);
                hm.put("position", i);

                // Starting ImageLoaderTask to download and populate image in the listview
                imageLoaderTask.execute(hm);
            }
        }
    }
Pankaj
  • 7,908
  • 6
  • 42
  • 65
  • I have tried this and it gives this error java.lang.NullPointerException at android.widget.SimpleAdapter.getCount(SimpleAdapter.java:93). My listview is able to display perfectly. Just that when I type something in the edittext box, it crashes. – StackOverFlow123 Jun 19 '15 at 08:44
0

I think there is problem with your edittext initialization.

searchBox = (EditText)findViewById(R.id.searchBox);

move this line from onPostexcute() to OnCreate()

Meenaxi
  • 567
  • 5
  • 17