Skip to content

Conversation

Jason-Whitmore
Copy link
Contributor

Description (required)

Fixes #6436

What changes did you make and why?

The getString() method was changed to allow returning a null value upon failure. This prevents the NPE.

Then, usages of getString() were modified to handle potential null value returns. If getString() returned null and a non null String was required, the empty string was used instead (this can be changed to throw an exception if needed).

Tests performed (required)

Tested ProdDebug on Android Studio Emulator with API level 36.

Reproducing the original issue is tricky, but this is probably the simplest way:

  1. Start with no bookmarks or clear all bookmarks
  2. Go to Explore
  3. Using the search function, find an item which has no images associated with it. I used "Q1687285".
  4. Bookmark the item
  5. Go to Bookmarks
  6. Tap "Categories"

On main, the issue will reproduce with an NPE. On this branch, there will be no crash.

If step 1 is skipped, the app may crash with #6464, but not #6436 according to the logs.

Jason-Whitmore and others added 3 commits October 9, 2025 16:16
Before this change, a call to getString() would assume that the specified column
name actually exists. A bad String input would cause a null value to be returned
to getString(), which would then throw a NPE because getString() can only return
non null Strings.

This change expands the getString() method to check if the column name exists.
If it does exist, the String is retrieved normally. Else, a null value is
returned. The method signature is changed to allow null return values.
Before this change, the getString() method in DatabaseUtils.kt was changed
to allow returning a null value upon method failure. All usages of getString()
were not changed.

This change updates all usages of getString() which require non null return
values. If null is returned, an empty string is used instead.
Copy link

✅ Generated APK variants!

@nicolas-raoul
Copy link
Member

Following the steps above, I indeed get no crash.

By the way, Bookmarks>Items is empty, any idea why? Locations also empty despite bookmarking a location in Nearby.

@nicolas-raoul
Copy link
Member

When skipping step 1 and tapping Bookmarks>Items I get this crash, I guess it is expected too.

APP_VERSION_NAME=6.0.2-debug-Jason-Whitmore-issue_6436
ANDROID_VERSION=16
STACK_TRACE=java.lang.IndexOutOfBoundsException: Empty list doesn't contain element at index 0.
at kotlin.collections.EmptyList.get(Collections.kt:37)
at kotlin.collections.EmptyList.get(Collections.kt:25)
at fr.free.nrw.commons.bookmarks.items.BookmarkItemsDao.convertToCategoryItems(BookmarkItemsDao.kt:182)
at fr.free.nrw.commons.bookmarks.items.BookmarkItemsDao.fromCursor(BookmarkItemsDao.kt:162)
at fr.free.nrw.commons.bookmarks.items.BookmarkItemsDao.getAllBookmarksItems(BookmarkItemsDao.kt:53)
at fr.free.nrw.commons.bookmarks.items.BookmarkItemsController.loadFavoritesItems(BookmarkItemsController.kt:21)
at fr.free.nrw.commons.bookmarks.items.BookmarkItemsFragment.initList(BookmarkItemsFragment.kt:47)
at fr.free.nrw.commons.bookmarks.items.BookmarkItemsFragment.onViewCreated(BookmarkItemsFragment.kt:34)
at androidx.fragment.app.Fragment.performViewCreated(Fragment.java:2987)

@nicolas-raoul nicolas-raoul merged commit 0a4b179 into commons-app:main Oct 11, 2025
1 check passed
@Jason-Whitmore
Copy link
Contributor Author

Following the steps above, I indeed get no crash.

By the way, Bookmarks>Items is empty, any idea why? Locations also empty despite bookmarking a location in Nearby.

I noticed this too when I was debugging. Interestingly, the method BookmarkItemsFragment.initList will correctly retrieve the list of favorite items as the depictItems list, and hide the You haven't added any bookmarks text, but will not actually display the items to the user.

I'm not sure when the bug first appeared. One possibility is that the bug first appeared when the old Java code was rewritten to Kotlin a few months ago.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

getString(...) must not be null

2 participants