3

I have a listview that have 5 item in it if am trying to set error on the 4 th and 5th item .Then it is throwing a null pointer exception. Exception

08-22 13:34:49.523: E/AndroidRuntime(16952): FATAL EXCEPTION: main
08-22 13:34:49.523: E/AndroidRuntime(16952): java.lang.NullPointerException
08-22 13:34:49.523: E/AndroidRuntime(16952):    at com.example.iweenflightbookingpage.MainActivity$1.onClick(MainActivity.java:116)
08-22 13:34:49.523: E/AndroidRuntime(16952):    at android.view.View.performClick(View.java:4091)
08-22 13:34:49.523: E/AndroidRuntime(16952):    at android.view.View$PerformClick.run(View.java:17072)
08-22 13:34:49.523: E/AndroidRuntime(16952):    at android.os.Handler.handleCallback(Handler.java:615)
08-22 13:34:49.523: E/AndroidRuntime(16952):    at android.os.Handler.dispatchMessage(Handler.java:92)
08-22 13:34:49.523: E/AndroidRuntime(16952):    at android.os.Looper.loop(Looper.java:153)
08-22 13:34:49.523: E/AndroidRuntime(16952):    at android.app.ActivityThread.main(ActivityThread.java:4987)
08-22 13:34:49.523: E/AndroidRuntime(16952):    at java.lang.reflect.Method.invokeNative(Native Method)
08-22 13:34:49.523: E/AndroidRuntime(16952):    at java.lang.reflect.Method.invoke(Method.java:511)
08-22 13:34:49.523: E/AndroidRuntime(16952):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:821)
08-22 13:34:49.523: E/AndroidRuntime(16952):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)
08-22 13:34:49.523: E/AndroidRuntime(16952):    at dalvik.system.NativeStart.main(Native Method)

Code To set Error

click.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
   allValues = myAdapter.getAllValues();
                ArrayList<Integer> errorList = new ArrayList<Integer>();
                for(int i = 0; i < allValues.length; i++){
                   for(int j=0;j<staticPasengerList.length;j++){
                       if(allValues[i] == staticPasengerList[j]){
                            Log.d("Lop Count", ""+allValues[i]+"="+staticPasengerList[j]);

                            // Add position to errorList
                            errorList.add(i);
                        }
                   }

                }
                 myAdapter.setErrorList(errorList);
                 myAdapter.notifyDataSetChanged();
          }

        }
    }

BaseAdapter Class

class data extends BaseAdapter {
    String[] Title;
    Activity activity;

    public data (MainActivity mainActivity, String[] text) {
        Title = text;
        activity = mainActivity;
    }

    public String[] getAllValues() {
        return Title;
    }

    public int getCount() {
        // TODO Auto-generated method stub
        return Title.length;
    }

    public Object getItem(int arg0) {
        // TODO Auto-generated method stub
        return null;
    }

    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = activity.getLayoutInflater();
        View row ;
        row = inflater.inflate(android.R.layout.simple_list_item_1, parent, false);
        TextView title;
        title = (TextView) row.findViewById(android.R.id.text1);
        title.setText(Title[position]);
        return (row);
    }

This exception is occuring in case of 4 and 5 will be the value of i. Please help me to resolve the issue

3 Answers3

2

You should consider that getChildAt(4) may return null .

you can setError() in your adapter's method getView() where position will be 4 . . .

EDITED:

class data extends BaseAdapter {
    String [] passengersList;
    String[] Title;
    Activity activity;

    public data (MainActivity mainActivity, String[] text, String[] passengersList) {
        Title = text;
        activity = mainActivity;
        this.passengersList = passengersList;
    }

    public String[] getAllValues() {
        return Title;
    }

    public int getCount() {
        // TODO Auto-generated method stub
        return Title.length;
    }

    public Object getItem(int arg0) {
        // TODO Auto-generated method stub
        return null;
    }

    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = activity.getLayoutInflater();
        View row ;
        row = inflater.inflate(android.R.layout.simple_list_item_1, parent, false);
        TextView title;
        title = (TextView) row.findViewById(android.R.id.text1);
        if(position == 4){
           checkAndSetError(title);
        }
        title.setText(Title[position]);
        return (row);
    }

    private void checkAndSetError(TextView title, int position){
        allValues = this.Title;
        Log.d("this is my array", "arr: " + Arrays.toString(allValues));
        for(int i=0;i<allValues.length;i++){
           if(allValues[position] == this.passengerList[i]){
               Log.d("Lop Count", ""+allValues[i]+"="+i);
               title.setError("Please change the data");
          }
        }
    }
}

It Should look something like this.

Jilberta
  • 2,836
  • 5
  • 30
  • 44
  • this set error position will be dynamic i have to check in a loop then i have to set error on that –  Aug 22 '13 at 08:23
  • you can add a private method setting the error in your custom adapter class . . . Pass to Adapter Class that passengerList Array and make the check in getView. – Jilberta Aug 22 '13 at 08:24
  • how i have to call it from my activity file –  Aug 22 '13 at 08:38
  • you just set the adapter to listView and when the listview gets the View where you need to make some restrictions it will be done during this getView. You don't need to call setting error from the activity – Jilberta Aug 22 '13 at 08:40
  • confused ??Please help me more to understand –  Aug 22 '13 at 08:42
  • data myAdapter = new data(this, text, pasengerList); listView.setAdapter(myAdapter); – Jilberta Aug 22 '13 at 08:43
  • this is quite good question explain u r answer so that i can also understand it – Developer Aug 22 '13 at 08:43
  • @User can u tell me what you can't understand specificially, what you want to call in Activity? – Jilberta Aug 22 '13 at 08:45
  • @Jilberta In my Main Actitvity i have a button on click of that i want to set error of the item of the listview based on the some condition –  Aug 22 '13 at 08:47
  • 1
    so you want to set restrictions when the listItem is clicked ? Can u put all your code? – Jilberta Aug 22 '13 at 08:49
  • 1
    @Jilberta i think he wants to set the error on the item on click of a button while checking some condition – Developer Aug 22 '13 at 08:53
  • 1
    Do you still need some help or have you already resolved it ? – Jilberta Aug 22 '13 at 12:31
  • i am still waiting for u reply for help.i am not able to resolve the issue still –  Aug 23 '13 at 06:14
0

The following should solve your problem. Please read the comments for explanation:

class data extends BaseAdapter {
    String[] Title;
    Activity activity;
    ArrayList<Integer> errorPositionList;

    public data (MainActivity mainActivity, String[] text) {
        Title = text;
        activity = mainActivity;
        errorPositionList = new ArrayList<Integer>();
    }

    // Add this method
    public void setErrorList(ArrayList<Integer> eList) {
        errorPositionList = eList;
    }

    ....
    ....

    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = activity.getLayoutInflater();
        View row ;
        row = inflater.inflate(android.R.layout.simple_list_item_1, parent, false);
        TextView title;
        title = (TextView) row.findViewById(android.R.id.text1);

        if (errorPositionList.contains(position)) {
            title.setText(Title[position]);
            title.setError("Please change the data");
        } else {
            title.setText(Title[position]);
        }

        return (row);
    } 
}  

And the OnClickListener should now look like:

click.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View arg0) {
        allValues = myAdapter.getAllValues();
        Log.d("this is my array", "arr: " + Arrays.toString(allValues));

        // Declare and initiate the ArrayList
        ArrayList<Integer> errorList = new ArrayList<Integer>();

        for(int i = 0; i < allValues.length; i++){
            if(allValues[i] == passengerList[i]){
                Log.d("Lop Count", ""+allValues[i]+"="+i);

                // Add position to errorList
                errorList.add(i);
            }
        }         

        myAdapter = new PassengerListView(MainActivity.this, allValues);
        listView.setAdapter(myAdapter);

        // Use setErrorList(ArrayList<Integer> eList) method from adapter
        myAdapter.setErrorList(errorList);

        // Update the list
        // The if statement inside getView() will set the error message
        myAdapter.notifyDataSetChanged();
    }
}

Update:

OP will need to implement error notification. Relying on setError(CharSequence) will not work in this case.

Vikram
  • 51,313
  • 11
  • 93
  • 122
  • This is working fine on click of the button it is setting the error and also removing all the text value of items –  Aug 27 '13 at 04:55
  • And on the first time it is setting up error on the item if but on the second time it is not setting error on the items –  Aug 27 '13 at 05:04
  • 1
    @User `This is working fine on click of the button it is setting the error and also removing all the text value of items `. I have updated the `getView()` method in my answer above. Please make the necessary changes. – Vikram Aug 27 '13 at 05:13
  • suppose 9 items are there in the listview .On first time without updating any item value user click the button then it is shwoing error on all the items.After that he updates one item from the listview and then again if he clicks the button on rest 8 items it has to show error but it is not showing the error. –  Aug 27 '13 at 05:21
  • @User I just noticed that you are using nested `for-loops`. Are you sure if that's what you want? If you want to check if `allValues` and `staticPassengerList` contain the same values, you should use just one `for-loop`. If you need an example of this, let me know, and I'll add it to my answer above. Just to confirm: after the user updates the first item, and clicks the button, `allValues[0] == staticPasengerList[0]` should return `false`, or `true`? – Vikram Aug 27 '13 at 05:30
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/36295/discussion-between-user2558882-and-user) – Vikram Aug 27 '13 at 05:31
-1

ListView component did not render element on position 4. It is a lazy component that renders only what is on screen. So don't assume that getChildAt returns a non null view.

Tell your adapter to show error at position 4 and let ListView know that data set changed by calling notifyDataSetChanged.

  1. Extend your adapter with list of position, where to show error.
  2. On click, add position to that list.
  3. Call notifyDataSetChanged from adapter.
  4. Change getView method to show error if it is rendering a view with error.
Grzegorz Żur
  • 47,257
  • 14
  • 109
  • 105
  • perfect one this is the problem so how could i get out of it please help me i an new in android i have changed the height of ListView so all the items are visible.After that i didn't got the error.But that is not the solution .Please help me –  Aug 22 '13 at 08:16
  • please help me i am stucked in this please –  Aug 22 '13 at 11:52
  • 1
    @Grzegorz how we can use this to setError on the item the way u r explaining the solution .. – Developer Aug 23 '13 at 10:08