Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

data binding null pointer exception on textView

I know what is this exception is and I know that there are a lot of similiar questions. But I can't apply those solutions to my problem. Everything works fine when I am using findById() but when I am trying to use data binding I got stuck. I am new in data binding.

public class HomePieChartMarkerViewModel extends MarkerView {
private TvContentBinding binding;

/**
 * Constructor. Sets up the MarkerView with a custom layout resource.
 *
 * @param context
 * @param layoutResource the layout resource to use for the MarkerView
 */
public HomePieChartMarkerViewModel(Context context, int layoutResource) {
    super(context, layoutResource);
}


@Override
public void refreshContent(Entry e, Highlight highlight) {
    binding.markerValue.setText("$ " + e.getY());// <<<< HERE I GOT AN ERROR
    super.refreshContent(e,highlight);
}
private MPPointF mOffset;

@Override
public MPPointF getOffset() {
    if(mOffset == null) {
        // center the marker horizontally and vertically
        mOffset = new MPPointF(-(getWidth() / 2), -getHeight());
    }

    return mOffset;
}

}

and I am calling this class in my fragment:

IMarker marker = new HomePieChartMarkerViewModel(context,R.layout.tv_content);
    binding.myMonthlyExpenditureSingleLineChart.setMarker(marker);

my xml tv_content

<layout>

<data>

    <variable
        name="chart_marker"
        type="com.nswd.successplan.model.HomePieChartMarkerViewModel" />
</data>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:paddingStart="8dp"
    android:paddingEnd="8dp"
android:background="@drawable/tv_content_rounded"
    android:gravity="center">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:src="@drawable/ic_bulet_blue"
        android:paddingEnd="8dp"/>
    <TextView
        android:id="@+id/marker_value"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="985"
        android:textSize="12sp"
        android:textColor="@color/background"/>
</LinearLayout>

EDIT: my error

java.lang.NullPointerException: Attempt to read from field 'android.widget.TextView com.nswd.successplan.databinding.TvContentBinding.markerValue' on a null object reference
    at com.nswd.successplan.model.HomePieChartMarkerViewModel.refreshContent(HomePieChartMarkerViewModel.java:32)
    at com.github.mikephil.charting.charts.Chart.drawMarkers(Chart.java:741)
    at com.github.mikephil.charting.charts.BarLineChartBase.onDraw(BarLineChartBase.java:285)
    at android.view.View.draw(View.java:23191)
    at android.view.View.updateDisplayListIfDirty(View.java:22066)
    at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:5214)
    at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:5186)
    at android.view.View.updateDisplayListIfDirty(View.java:22021)
    at android.view.View.draw(View.java:22921)
    at android.view.ViewGroup.drawChild(ViewGroup.java:5230)
    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4987)
    at android.view.View.draw(View.java:23194)
    at androidx.core.widget.NestedScrollView.draw(NestedScrollView.java:1931)
    at android.view.View.updateDisplayListIfDirty(View.java:22066)
    at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:5214)
    at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:5186)
    at android.view.View.updateDisplayListIfDirty(View.java:22021)
    at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:5214)
    at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:5186)
    at android.view.View.updateDisplayListIfDirty(View.java:22021)
    at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:5214)
    at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:5186)
    at android.view.View.updateDisplayListIfDirty(View.java:22021)
    at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:5214)
    at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:5186)
    at android.view.View.updateDisplayListIfDirty(View.java:22021)
    at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:5214)
    at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:5186)
    at android.view.View.updateDisplayListIfDirty(View.java:22021)
    at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:5214)
    at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:5186)
    at android.view.View.updateDisplayListIfDirty(View.java:22021)
    at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:5214)
    at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:5186)
    at android.view.View.updateDisplayListIfDirty(View.java:22021)
    at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:5214)
    at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:5186)
    at android.view.View.updateDisplayListIfDirty(View.java:22021)
    at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:5214)
    at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:5186)
    at android.view.View.updateDisplayListIfDirty(View.java:22021)
    at android.view.ThreadedRenderer.updateViewTreeDisplayList(ThreadedRenderer.java:588)
    at android.view.ThreadedRenderer.updateRootDisplayList(ThreadedRenderer.java:594)
    at android.view.ThreadedRenderer.draw(ThreadedRenderer.java:667)
    at android.view.ViewRootImpl.draw(ViewRootImpl.java:4263)
    at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:4047)
    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:3320)
    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:2200)
    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:8999)
    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:996)
    at android.view.Choreographer.doCallbacks(Choreographer.java:794)
    at android.view.Choreographer.doFrame(Choreographer.java:729)
    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:981)
    at android.os.Handler.handleCallback(Handler.java:883)
like image 427
gojic Avatar asked Nov 15 '22 08:11

gojic


1 Answers

Try using this

  ((HomePieChartMarkerViewModel) context).binding.myMonthlyExpenditureSingleLineChart.setMarker(marker);
  • I encountered the same error when I was trying to access content of textview from MainActivity in my recycler view adapter class. Therefore by using the context of the class, the null pointer was gone. This helped in my case. I called it inside the viewBinder function where I had the context of previous class too.

I hope I answered your question.

like image 114
Special_octo20 Avatar answered Dec 15 '22 17:12

Special_octo20