STK90 STK90 - 1 month ago 14
Android Question

Check null values for each item in list

I have list of News. Every news has author id, I need to get author id from news then call to Author fetch his name and set author's name to each news.

Seem to be easy, and it works, but some author's names are null, and app throws an exepcion. So I need to make a check for each item in newslist if author's name is null, replace it with "unknown" string. My variant is not working.

.flatMap(new Func1<News, Observable<News>>() {
@Override
public Observable<News> call(News news) {
return apiService.getAuthor(news.getId())
.doOnNext(new Action1<Author>() {
@Override
public void call(Author author) {

if (!author.getName().equals("null")) {
news.setAuthorName(author.getName());
} else {
news.setAuthorName("Unknown");
}
}
})
.observeOn(Schedulers.io())
.map(new Func1<Author, News>() {
@Override
public News call(Author author) {
return news;
}
})
.subscribeOn(Schedulers.newThread());
}
})

Answer

Here are some common utility functions to help you with null checking. Add these to a Utils class or something. Also notice that checking for String nulls, is different than checking for object nulls

private static final String EMPTY = "";
private static final String NULL = "null";

/**
 * Method checks if String value is empty
 *
 * @param str
 * @return string
 */
public static boolean isStringEmpty(String str) {
    return str == null || str.length() == 0 || EMPTY.equals(str.trim()) || NULL.equals(str);
}

/**
 * Method is used to check if objects are null
 *
 * @param objectToCheck
 * @param <T>
 * @return true if objectToCheck is null
 */
public static <T> boolean checkIfNull(T objectToCheck) {
    return objectToCheck == null;
}

Now update your code

.flatMap(new Func1<News, Observable<News>>() {
        @Override
        public Observable<News> call(News news) {
            return apiService.getAuthor(news.getId())
                    .doOnNext(new Action1<Author>() {
                        @Override
                        public void call(Author author) {
                            // notice how I first confirm that the object is not null 
                            // and then I check if the String value from the object is not null
                            if (!Utils.checkIfNull(author) && !Utils.isStringEmpty(author.getName()) {
                                news.setAuthorName(author.getName());
                            } else {
                                news.setAuthorName("Unknown");
                            }


                        }
                    })
                    .observeOn(Schedulers.io())
                    .map(new Func1<Author, News>() {
                        @Override
                        public News call(Author author) {
                            return news;
                        }
                    })
                    .subscribeOn(Schedulers.newThread());
        }
    })

The reason why you are having issues is because you are checking for the String literal, "null" not necessarily that the String is null.

Comments