Mohan Mohan - 4 months ago 22
Java Question

ListView : getHeight() on a null object reference

I have a subclass of

ListView
and I have
onDraw()
method in which am trying to get the height dynamically. But i am getting a
NullPointerException
.

Error:

Fatal Exception: java.lang.NullPointerException
at in.justickets.android.view.ExpandedListView.onDraw(ExpandedListView.java:25)
at android.view.View.draw(View.java:14493)
at android.widget.AbsListView.draw(AbsListView.java:3869)
at android.view.View.getDisplayList(View.java:13390)
at android.view.View.getDisplayList(View.java:13432)
at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3077)
at android.view.View.getDisplayList(View.java:13328)
at android.view.View.getDisplayList(View.java:13432)
at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3077)
at android.view.View.getDisplayList(View.java:13328)
at android.view.View.getDisplayList(View.java:13432)
at android.view.View.draw(View.java:14210)
at android.view.ViewGroup.drawChild(ViewGroup.java:3103)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2940)
at android.widget.AbsListView.dispatchDraw(AbsListView.java:2467)
at android.view.View.draw(View.java:14496)
at android.widget.AbsListView.draw(AbsListView.java:3869)
at android.view.View.getDisplayList(View.java:13390)
at android.view.View.getDisplayList(View.java:13432)
at android.view.View.draw(View.java:14210)
at android.view.ViewGroup.drawChild(ViewGroup.java:3103)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2940)
at android.view.View.getDisplayList(View.java:13385)
at android.view.View.getDisplayList(View.java:13432)
at android.view.View.draw(View.java:14210)
at android.view.ViewGroup.drawChild(ViewGroup.java:3103)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2940)
at android.view.View.draw(View.java:14496)
at android.support.v4.view.ViewPager.draw(ViewPager.java:2336)
at android.view.View.getDisplayList(View.java:13390)
at android.view.View.getDisplayList(View.java:13432)
at android.view.View.draw(View.java:14210)
at android.view.ViewGroup.drawChild(ViewGroup.java:3103)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2947)
at android.view.View.draw(View.java:14496)
at android.view.View.getDisplayList(View.java:13390)
at android.view.View.getDisplayList(View.java:13432)
at android.view.View.draw(View.java:14210)
at android.view.ViewGroup.drawChild(ViewGroup.java:3103)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2940)
at android.view.View.getDisplayList(View.java:13385)
at android.view.View.getDisplayList(View.java:13432)
at android.view.View.draw(View.java:14210)
at android.view.ViewGroup.drawChild(ViewGroup.java:3103)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2940)
at android.view.View.draw(View.java:14496)
at android.widget.FrameLayout.draw(FrameLayout.java:472)
at android.view.View.getDisplayList(View.java:13390)
at android.view.View.getDisplayList(View.java:13432)
at android.view.View.draw(View.java:14210)
at android.view.ViewGroup.drawChild(ViewGroup.java:3103)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2940)
at android.view.View.getDisplayList(View.java:13385)
at android.view.View.getDisplayList(View.java:13432)
at android.view.View.draw(View.java:14210)
at android.view.ViewGroup.drawChild(ViewGroup.java:3103)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2940)
at android.view.View.getDisplayList(View.java:13385)
at android.view.View.getDisplayList(View.java:13432)
at android.view.View.draw(View.java:14210)
at android.view.ViewGroup.drawChild(ViewGroup.java:3103)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2940)
at android.view.View.getDisplayList(View.java:13385)
at android.view.View.getDisplayList(View.java:13432)
at android.view.View.draw(View.java:14210)
at android.view.ViewGroup.drawChild(ViewGroup.java:3103)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2940)
at android.view.View.draw(View.java:14496)
at android.widget.FrameLayout.draw(FrameLayout.java:472)
at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:2326)
at android.view.View.getDisplayList(View.java:13390)
at android.view.View.getDisplayList(View.java:13432)
at android.view.HardwareRenderer$GlRenderer.buildDisplayList(HardwareRenderer.java:1577)
at android.view.HardwareRenderer$GlRenderer.draw(HardwareRenderer.java:1451)
at android.view.ViewRootImpl.draw(ViewRootImpl.java:2420)
at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2292)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1922)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1039)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5648)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
at android.view.Choreographer.doCallbacks(Choreographer.java:574)
at android.view.Choreographer.doFrame(Choreographer.java:544)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5111)
at java.lang.reflect.Method.invokeNative(Method.java)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:806)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:622)
at dalvik.system.NativeStart.main(NativeStart.java)


Code:

public class SubListView extends ListView {

private int count = 0;

public SubListView(Context context, AttributeSet attrs) {
super(context, attrs);
}

@Override
protected void onDraw(Canvas canvas) {
if (getCount() != count) {
count = getCount();
android.view.ViewGroup.LayoutParams params = getLayoutParams();
if(params!=null) {
params.height = getCount() * (count > 0 ? getChildAt(0).getHeight() : 0);
setLayoutParams(params);

}
}

super.onDraw(canvas);
}

}

Answer

The problem is your View is not drawn yet when calling getHeight().

You should call super.onDraw() before that:

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    if (getCount() != count) {
        count = getCount();
        android.view.ViewGroup.LayoutParams params = getLayoutParams();
        if(params!=null) {
            params.height = getCount() * (count > 0 ? getChildAt(0).getHeight() : 0);
            setLayoutParams(params);
        }
    }
}