Sheik Sajidvalirehman
Sheik Sajidvalirehman

Reputation: 21

java.lang.NullPointerException: Attempt to invoke virtual method 'int android.view.View.getBottom()' on a null object reference

It's working fine when I build and run it. But I'm getting this error when I relaunch the app remotely.

this is the only place where I used views. It's in ListAdapter.

I looked around on both Google and StackOverflow and I have found some information. However, I have not got it to work. Now I hope that I can get help with my code.

java.lang.NullPointerException: Attempt to invoke virtual method 'int android.view.View.getBottom()' on a null object reference
        at android.widget.ListView.fillDown(ListView.java:829)
        at android.widget.ListView.fillFromTop(ListView.java:888)
        at android.widget.ListView.layoutChildren(ListView.java:1958)
        at android.widget.AbsListView.onLayout(AbsListView.java:2991)
        at android.view.View.layout(View.java:22406)
        at android.view.ViewGroup.layout(ViewGroup.java:6594)
        at androidx.constraintlayout.widget.ConstraintLayout.onLayout(ConstraintLayout.java:1915)
        at android.view.View.layout(View.java:22406)
        at android.view.ViewGroup.layout(ViewGroup.java:6594)
        at com.google.android.material.appbar.HeaderScrollingViewBehavior.layoutChild(HeaderScrollingViewBehavior.java:142)
        at com.google.android.material.appbar.ViewOffsetBehavior.onLayoutChild(ViewOffsetBehavior.java:41)
        at com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior.onLayoutChild(AppBarLayout.java:1556)
        at androidx.coordinatorlayout.widget.CoordinatorLayout.onLayout(CoordinatorLayout.java:888)
        at android.view.View.layout(View.java:22406)
        at android.view.ViewGroup.layout(ViewGroup.java:6594)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
        at android.view.View.layout(View.java:22406)
        at android.view.ViewGroup.layout(ViewGroup.java:6594)
        at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1812)
        at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1656)
        at android.widget.LinearLayout.onLayout(LinearLayout.java:1565)
        at android.view.View.layout(View.java:22406)
        at android.view.ViewGroup.layout(ViewGroup.java:6594)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
        at android.view.View.layout(View.java:22406)
        at android.view.ViewGroup.layout(ViewGroup.java:6594)
        at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1812)
        at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1656)
        at android.widget.LinearLayout.onLayout(LinearLayout.java:1565)
        at android.view.View.layout(View.java:22406)
        at android.view.ViewGroup.layout(ViewGroup.java:6594)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
        at com.android.internal.policy.DecorView.onLayout(DecorView.java:1075)
        at android.view.View.layout(View.java:22406)
        at android.view.ViewGroup.layout(ViewGroup.java:6594)
        at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:3394)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2863)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1911)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:8526)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:949)
        at android.view.Choreographer.doCallbacks(Choreographer.java:761)
        at android.view.Choreographer.doFrame(Choreographer.java:696)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:935)
        at android.os.Handler.handleCallback(Handler.java:873)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7078)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:974)

public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        if(getItem(position)!= null) {
            title = getItem(position).getText();
            date = getItem(position).getDate();
            hour = getItem(position).getHour();
            minute = getItem(position).getMinute();
            SimpleDateFormat formatter = new SimpleDateFormat("dd MMMM yyyy");
            LayoutInflater inflater = LayoutInflater.from(context);
            convertView = inflater.inflate(resource, parent, false);
            TextView titleView = (TextView) convertView.findViewById(R.id.title);
            TextView descpView = (TextView) convertView.findViewById(R.id.description);
            TextView dateView = (TextView) convertView.findViewById(R.id.date);
            titleView.setText(title);
            descpView.setText(hour + ":" + minute);
            dateView.setText(formatter.format(date));
        }

        return convertView;
    }

Upvotes: 2

Views: 2772

Answers (1)

Zain
Zain

Reputation: 40878

java.lang.NullPointerException: Attempt to invoke virtual method 'int android.view.View.getBottom()' on a null object reference

Some view is getting null causing NullPointerException when calling getBottom() on that view.

So, as the app runs locally without this issue; but not remotely, so the answer will be a kind of a guess.

Assumption 1: getView returns a null value.

Reason: convertView is marked as @Nullable, so it can be null from the beginning, and can remain null if the condition if(getItem(position)!= null) is not met; and therefore getView() can return a null View.

Recommendation: you need to make sure that getView can't return null value by setting the convertView value outside the if condition like the below snippet.

public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
    if (converView == null) {
        convertView = inflater.inflate(resource, parent, false);
    }

Assumption 2: convertView is null from this expression convertView = inflater.inflate(resource, parent, false);

Reason: You didn't provide from where you got the resource argument you pass to inflate(); maybe it's a variable value that can change in multiple values; one of which can be null; and therefore making convertView null as well.

Recommendation:

If the resource is not changeable, you can hardcode it by setting its value to say R.layout.list_row; where list_row is the xml layout of your list item.

And If it's changeable, then debug its value to make sure it can't be null by any means.

Upvotes: 2

Related Questions