skydoves / PowerSpinner

🌀 A lightweight dropdown popup spinner, fully customizable with an arrow and animations for Android.
Apache License 2.0
1.22k stars 116 forks source link

GetText.toString on a null object reference #54

Closed ghost closed 3 years ago

ghost commented 3 years ago

Please complete the following information:

Describe the Bug: when i am trying to get the text value from the powerspinner it is giving below error... "binding.personal.spinPersonal.getText().toString()" java.lang.NullPointerException: Attempt to read from field 'com.android.project.databinding.LayoutPersonalBinding com.android.project.databinding.FragmentBorrowerPersonalBinding.personal' on a null object reference

Expected Behavior: It should return the text value which is set on the spinner , but it is giving null object error....

skydoves commented 3 years ago

Could you attach some example codes? I think you tried to access a null object..?

Attempt to read from field 'com.android.project.databinding.LayoutPersonalBinding 
com.android.project.databinding.FragmentBorrowerPersonalBinding.personal' on a null object reference
ghost commented 3 years ago

Xml part

i am using databinding

<androidx.appcompat.widget.LinearLayoutCompat android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="@dimen/dp_5" android:orientation="vertical">

            <androidx.appcompat.widget.AppCompatTextView
                android:textSize="12sp"
                android:textStyle="bold"
                android:layout_marginLeft="@dimen/dp_4"
                android:textColor="@color/globalBlack"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:fontFamily="@font/mmedium"
                android:text="@string/personalText" />

            <com.skydoves.powerspinner.PowerSpinnerView
                android:layout_width="match_parent"
                android:layout_height="@dimen/dp_40"
                android:id="@+id/spinAppPersonal"
                android:hint="Choose Monthly Income"
                android:textSize="13sp"
                android:textColor="@color/globalBlack"
                android:textColorHint="@color/globalBlack"
                android:gravity="center|left"
                android:padding="@dimen/dp_10"
                android:layout_margin="@dimen/dp_5"
                app:spinner_divider_color="@color/globalBlack"
                app:spinner_divider_show="true"
                app:spinner_divider_size="0.4dp"
                app:spinner_arrow_gravity="end"
                app:spinner_arrow_show="true"
                app:spinner_popup_animation="dropdown"
                app:spinner_popup_background="@color/globaltext"
                app:spinner_popup_elevation="@dimen/dp_20"
                app:spinner_arrow_drawable="@drawable/ic_bottom_arrow"
                app:spinner_arrow_tint="@color/globalBlack"
                app:spinner_arrow_animate="true"
                android:background="@drawable/spinnerbackground" />

        </androidx.appcompat.widget.LinearLayoutCompat>

JAVA PART

String check = ""; check = binding.personal.spinAppPersonal.getText().toString();

this personal is the included layout....

java.lang.NullPointerException: Attempt to read from field 'com.android.project.databinding.LayoutPersonalBinding com.android.project.databinding.FragmentBorrowerPersonalBinding.personal' on a null object reference

skydoves commented 3 years ago

Hi, I need more information about your Java codes in your project, FragmentBorrowerPersonal. Also, is it work correctly if you remove the PowerSpinnerView on your XML?

ghost commented 3 years ago

yes!!