sujithkanna/SmileyRating

fails on nullpointerexception when setting a smiley for the first time

Closed this issue · 9 comments

Your work is awsome! but now I need to make it work :) I follow the 3 steps in the usage part and I see the smilies in the designer preview. I did the following in an adapter:
RatingViewHolder(View itemView) {
super(itemView);
srRating = (SmileRating) itemView.findViewById(R.id.smile_rating);
srRating.setIndicator(true); //<< srRating is not null and this line works
}

@OverRide
public void onBindViewHolder(final RatingViewHolder holder, int position) {
Rating theRating = RatingList.get(position);
holder.srRating.setSelectedSmile(5,true); << fails*
}

  • SmileRating.java, line 194
    moveSmile(mTouchPoints.get(mSelectedSmile).x); <<< fails mTouchPoints size is 0
    I put a breakpoint in line 255: createTouchPoints() but it never reaches there before the above code so it fails on nullpointerexception because there is no position 5

Please advice, it seems that this is working for everyone so I wonder if I miss something
Thanks

Are you sure it is null pointer? or IndexOutOfBounds exception

Can you post the stackTrace of the error too?

I guess mTouchPoints should be 5 (smilies) but it contains 0 because they were never created. I think.
Here is the stacktrace
--------- beginning of crash
09-16 08:35:36.497 3992-3992/com.update.update E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.update.update, PID: 3992
java.lang.NullPointerException: Attempt to read from field 'float com.hsalf.smilerating.BaseRating$Point.x' on a null object reference
at com.hsalf.smilerating.SmileRating$2.onAnimationStart(SmileRating.java:194)
at android.animation.ValueAnimator.notifyStartListeners(ValueAnimator.java:1048)
at android.animation.ValueAnimator.start(ValueAnimator.java:1110)
at android.animation.ValueAnimator.start(ValueAnimator.java:1117)
at com.hsalf.smilerating.SmileRating.setSelectedSmile(SmileRating.java:565)
at com.hsalf.smilerating.SmileRating.setSelectedSmile(SmileRating.java:545)
at com.update.update.DateRatingAdapter.onBindViewHolder(DateRatingAdapter.java:49) -> holder.srRating.setSelectedSmile(5,true);

I hope the above will be enough but if you need more info from me, let me know

I got the same error

@LeooonH It seems that we're the only ones to get this error... I wonder what the problem is. Waiting for @sujithkanna response.

Hi all,
I want to ask why when I set true for the animation it gives me java.lang.NullPointerException: Attempt to read from field 'float com.hsalf.smilerating.BaseRating$Point.x' on a null object reference

My code (simplyfied):

public class scoreActivity extends AppCompatActivity{

    SmileRating smileRating;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.score_layout);
        smileRating = (SmileRating) findViewById(R.id.smile_rating);
        smileRating.setIndicator(true);

// this works
        smileRating.setSelectedSmile(BaseRating.TERRIBLE);

// this one doesn't works
//      smileRating.setSelectedSmile(BaseRating.TERRIBLE,true);
    }

What am I doing wrong?

Thank you for your AWESOME work! :D

It is because the view is not inflated yet fully, so try posting it using runnable.

smileyRating.post(new Runnable() {
            @Override
            public void run() {
                smileRating.setSelectedSmile(BaseRating.TERRIBLE,true);
            }
        });

Can you try this?

Thank you :D

solution by @sujithkanna works! same problem with animate and the snippet works fine