0

I am getting a NullPointerException when I use to format date with my StringUtils class. If I use it without StringUtils, it just works fine.

I have added the import statement for StringUtils

I have this:

<TextView
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="@{StringUtils.getFormattedDate(user.date)}" />

This gives me an error in my StringUtils method:

public static String getFormattedDate(String unformattedDate) {
        // unformattedDate will be in format of yyyy-mm-dd
        // Convert it to d mmm, yyyy
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        String date = null;
        try {
            Date d = df.parse(unformattedDate);   // <<--------- Here
            SimpleDateFormat dateFormat = new SimpleDateFormat("d MMM, yyyy");
            date = dateFormat.format(d.getTime());
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

When I checked with debugger, unformattedDate is null from the beginning. The correct method is called but the value passed is null. This is strange.

When I use it like this in the layout file:

<TextView
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="@{user.date}" />

It gives me no error, and date is displayed on screen!

I have tried cleaning the project and re-run it. But no success.

2
  • where and when you are setting value for user? Commented Sep 6, 2016 at 6:45
  • It is fetched async using firebase database. Is that causing issue? Commented Sep 6, 2016 at 6:45

2 Answers 2

1

Data bindings are null-safe itself, but this doesn't hold for using the values as parameter. Since the getFormattedDate() is yours to change, just make sure it's null-safe as well. You're already returning null if there's a ParseException anyway.

public static String getFormattedDate(String unformattedDate) {
    try {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        Date d = df.parse(unformattedDate);
        SimpleDateFormat dateFormat = new SimpleDateFormat("d MMM, yyyy");
        return dateFormat.format(d.getTime());
    } catch (Exception e) {
        Log.w("StringUtils", "getFormattedDate", e);
        return null;
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

This is more preferable i think. Thanks!
1

When you run it for first time user is not having any value untill you get it from Firebase database, so user.date will also be null.

Put ternary :

android:text="@{user.date==null ? `` : StringUtils.getFormattedDate(user.date)}"

1 Comment

It is strange that it just doesn't give any specific error, and shows all my automatically generated binding classes are not found. I had this type of error when you put android:text="@{user.amount}" where amount is of type double. Putting android:text="@{String.valueOf(user.amount)}" compiles successfully. This is the case when you just don't get any specific error. I think Android Studio 2.1.3 still lakes when it comes to errors in data-binding

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.