-3

i know that my question is a duplicate of some question here before, i tried every solution that i see from that, but no solution work for me

i have a class named FilingModel and a method name getReason, i always get a null value to my tvReason TextView

public class FilingAdapter extends RecyclerView.Adapter<FilingAdapter.FilingHolder> {
  List<FilingModel> lists;
    @Override
    public void onBindViewHolder(@NonNull FilingHolder holder, int position) {
     FilingModel model = lists.get(position);
     holder.tvReason.setText(model.getReason());
   }

}
public class FilingModel {

    private String reason;

    public FilingModel(String reason) {
        this.reason = reason;
    }

    public String getReason() {
        if ( reason.isEmpty() || TextUtils.isEmpty(reason) || reason.equals(null)) {
           reason = "-";
        }
        return reason;
    }
}


Marlon
  • 69
  • 7

2 Answers2

0

String checks for null are rather straight forward (once you have done it):

First you want to check if the instance is null (otherwise all further manipulation might fail):

 if(reason == null)

Then you want to check if it is empty as you already did it but maybe you want to trim it first (to remove all whitespaces):

if(reason == null || reason.trim().isEmpty())

as you can see you can chain the conditions with or since Java will stop evaluating the conditions once one of them was found true . So if your string is null, Java will not evaluate if it is empty.

And that's all you need, null and isEmpty (and optionally with a trim())

public String getReason() {
    if(reason==null || reason.trim().isEmpty()){
       reason = "-";
    }
    return reason;
}
GameDroids
  • 5,584
  • 6
  • 40
  • 59
  • i tried it, but it still not working for me – Marlon Jan 14 '20 at 09:28
  • FYI, the short version (without trim) is simply `if(reason == null || reason.isEmpty())` no need for `TextUtils` or other external tools. Just keep in mind that you first check for null before you check anything else. – GameDroids Jan 14 '20 at 09:30
  • what exactly is not working? are you getting an Exception or is the result different from what you expect? – GameDroids Jan 14 '20 at 09:31
  • @GameDriods i don't get any exception – Marlon Jan 14 '20 at 09:44
-1

Give type to your variable reason. For example String.

private String reason;

+

if(reason == null)
Bonum Ursus
  • 46
  • 1
  • 3