Question

I am creating app in which i am displaying data in custom ListView......the data is coming from XMLHandler. but, when i scroll the list it gives null pointer error.....

There are six items coming from xmlHandler at a time only 3 items are displayed on the screen....but, when i scroll getView() is supposed to be called for remaining 3 items.....but, it it called for one more item....hence it gives null pointer......i dont know what to do...kindly help me..... Thank you in adv.

*Sorry i forgot add getview() in my question but it is there in my actual code. i have edited my code...pls review this code......***

public class SearchedRouteAdapter extends ArrayAdapter<BusGetSetters> {
    private final Activity activity;
    private List<BusGetSetters> listBusGS = Utils.arrayBusGetSetRoutes;
    String routeID, routeName, travelsName, busType, depTime, arrTime,
            seatsAvail, sellFare, ResCode;
    BusGetSetters searchBus = new BusGetSetters();

    public SearchedRouteAdapter(Activity activity, List<BusGetSetters> objects) {
        super(activity, R.layout.route_list_item, objects);
        this.activity = activity;
        this.listBusGS = objects;
    }

    @SuppressWarnings("deprecation")
    public View getView(int position, View convertView, ViewGroup parent) {
        View rowView = convertView;
        RouteSearchView hsView = null;

              if (rowView == null) {
            // Get a new instance of the row layout view
            LayoutInflater inflater = activity.getLayoutInflater();
            rowView = inflater.inflate(R.layout.route_list_item, null);
            Log.v("Log position", "2");

            // Hold the view objects in an object,
            // so they don't need to be re-fetched
            hsView = new RouteSearchView();
            hsView.routeSVname = (TextView) rowView
                    .findViewById(R.id.routeName);
            hsView.routeSVtravelsName = (TextView) rowView
                    .findViewById(R.id.travelsName);
            hsView.routeSVbusType = (TextView) rowView
                    .findViewById(R.id.busType);
            hsView.routeSVdepTime = (TextView) rowView
                    .findViewById(R.id.depTime);
            hsView.routeSVarrTime = (TextView) rowView
                    .findViewById(R.id.arrTime);
            hsView.routeSVseatsAvail = (TextView) rowView
                    .findViewById(R.id.seatsAvail);
            hsView.routeSVsellFare = (TextView) rowView
                    .findViewById(R.id.sellFare);
            hsView.routeSVcmdCheck = (Button) rowView
                    .findViewById(R.id.cmdCheck);
            // Cache the view objects in the tag,
            // so they can be re-accessed later
            Log.v("Log position", "3");
            rowView.setTag(hsView);

        } else {
            hsView = (RouteSearchView) rowView.getTag();
        }
        // Transfer the stock data from the data object
        // to the view objects
        searchBus = listBusGS.get(position);

            routeID = searchBus.getRoute_id().toString();
            routeName = searchBus.getService_name().toString();
            travelsName = searchBus.getTravels_name().toString();
            busType = searchBus.getBus_type().toString();
            depTime = searchBus.getDeparture_time().toString();
            arrTime = searchBus.getArival_time().toString();
            seatsAvail = searchBus.getSeats_available().toString();
            sellFare = searchBus.getSell_fare().toString();

            hsView.routeSVname.setText(routeName);
            hsView.routeSVtravelsName.setText(travelsName);
            hsView.routeSVbusType.setText(busType);
            hsView.routeSVdepTime.setText(depTime);
            hsView.routeSVarrTime.setText(arrTime);
            hsView.routeSVseatsAvail.setText(seatsAvail);
            hsView.routeSVsellFare.setText(sellFare);


        }

        return rowView;
    }
protected static class RouteSearchView {

        protected TextView routeSVname;
        protected TextView routeSVtravelsName;
        protected TextView routeSVbusType;
        protected TextView routeSVdepTime;
        protected TextView routeSVarrTime;
        protected TextView routeSVseatsAvail;
        protected TextView routeSVsellFare;
        protected Button routeSVcmdCheck;
    }
}

Following is Logcat output:

02-01 15:59:22.852: E/AndroidRuntime(1964): FATAL EXCEPTION: main
02-01 15:59:22.852: E/AndroidRuntime(1964): java.lang.NullPointerException
02-01 15:59:22.852: E/AndroidRuntime(1964):     at com.demo.busbooking.SearchedRouteAdapter.getView(SearchedRouteAdapter.java:107)
02-01 15:59:22.852: E/AndroidRuntime(1964):     at android.widget.AbsListView.obtainView(AbsListView.java:1430)
02-01 15:59:22.852: E/AndroidRuntime(1964):     at android.widget.ListView.makeAndAddView(ListView.java:1745)
02-01 15:59:22.852: E/AndroidRuntime(1964):     at android.widget.ListView.fillDown(ListView.java:670)
02-01 15:59:22.852: E/AndroidRuntime(1964):     at android.widget.ListView.fillFromTop(ListView.java:727)
02-01 15:59:22.852: E/AndroidRuntime(1964):     at android.widget.ListView.layoutChildren(ListView.java:1598)
02-01 15:59:22.852: E/AndroidRuntime(1964):     at android.widget.AbsListView.onLayout(AbsListView.java:1260)
02-01 15:59:22.852: E/AndroidRuntime(1964):     at android.view.View.layout(View.java:7175)
02-01 15:59:22.852: E/AndroidRuntime(1964):     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1254)
02-01 15:59:22.852: E/AndroidRuntime(1964):     at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1130)
02-01 15:59:22.852: E/AndroidRuntime(1964):     at android.widget.LinearLayout.onLayout(LinearLayout.java:1047)
02-01 15:59:22.852: E/AndroidRuntime(1964):     at android.view.View.layout(View.java:7175)
02-01 15:59:22.852: E/AndroidRuntime(1964):     at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
02-01 15:59:22.852: E/AndroidRuntime(1964):     at android.view.View.layout(View.java:7175)
02-01 15:59:22.852: E/AndroidRuntime(1964):     at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
02-01 15:59:22.852: E/AndroidRuntime(1964):     at android.view.View.layout(View.java:7175)
02-01 15:59:22.852: E/AndroidRuntime(1964):     at android.view.ViewRoot.performTraversals(ViewRoot.java:1140)
02-01 15:59:22.852: E/AndroidRuntime(1964):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1859)
02-01 15:59:22.852: E/AndroidRuntime(1964):     at android.os.Handler.dispatchMessage(Handler.java:99)
02-01 15:59:22.852: E/AndroidRuntime(1964):     at android.os.Looper.loop(Looper.java:123)
02-01 15:59:22.852: E/AndroidRuntime(1964):     at android.app.ActivityThread.main(ActivityThread.java:3683)
02-01 15:59:22.852: E/AndroidRuntime(1964):     at java.lang.reflect.Method.invokeNative(Native Method)
02-01 15:59:22.852: E/AndroidRuntime(1964):     at java.lang.reflect.Method.invoke(Method.java:507)
02-01 15:59:22.852: E/AndroidRuntime(1964):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-01 15:59:22.852: E/AndroidRuntime(1964):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-01 15:59:22.852: E/AndroidRuntime(1964):     at dalvik.system.NativeStart.main(Native Method)
Was it helpful?

Solution 3

I have solved the problem, which was in xml handler not this code but thank you for your efforts!

OTHER TIPS

You need to override the getView() method of ArrayAdapter and place this code inside.

public View getView(){  
if (rowView == null) {
            // Get a new instance of the row layout view
            LayoutInflater inflater = activity.getLayoutInflater();
            rowView = inflater.inflate(R.layout.route_list_item, null);
            Log.v("Log position", "2");

            // Hold the view objects in an object,
            // so they don't need to be re-fetched
            hsView = new RouteSearchView();
            hsView.routeSVname = (TextView) rowView
                    .findViewById(R.id.routeName);
            hsView.routeSVtravelsName = (TextView) rowView
                    .findViewById(R.id.travelsName);
            hsView.routeSVbusType = (TextView) rowView
                    .findViewById(R.id.busType);
            hsView.routeSVdepTime = (TextView) rowView
                    .findViewById(R.id.depTime);
            hsView.routeSVarrTime = (TextView) rowView
                    .findViewById(R.id.arrTime);
            hsView.routeSVseatsAvail = (TextView) rowView
                    .findViewById(R.id.seatsAvail);
            hsView.routeSVsellFare = (TextView) rowView
                    .findViewById(R.id.sellFare);
            hsView.routeSVcmdCheck = (Button) rowView
                    .findViewById(R.id.cmdCheck);
            // Cache the view objects in the tag,
            // so they can be re-accessed later
            Log.v("Log position", "3");
            rowView.setTag(hsView);

        } else {
            hsView = (RouteSearchView) rowView.getTag();
        }
        // Transfer the stock data from the data object
        // to the view objects
        searchBus = listBusGS.get(position);

            routeID = searchBus.getRoute_id().toString();
            routeName = searchBus.getService_name().toString();
            travelsName = searchBus.getTravels_name().toString();
            busType = searchBus.getBus_type().toString();
            depTime = searchBus.getDeparture_time().toString();
            arrTime = searchBus.getArival_time().toString();
            seatsAvail = searchBus.getSeats_available().toString();
            sellFare = searchBus.getSell_fare().toString();

            hsView.routeSVname.setText(routeName);
            hsView.routeSVtravelsName.setText(travelsName);
            hsView.routeSVbusType.setText(busType);
            hsView.routeSVdepTime.setText(depTime);
            hsView.routeSVarrTime.setText(arrTime);
            hsView.routeSVseatsAvail.setText(seatsAvail);
            hsView.routeSVsellFare.setText(sellFare);


        }

        return rowView;
    }

Something along those lines.

The ArrayAdapter constructor does not return Views. You should implement getView() to return the View to be placed in a specific row in ListView.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top