material-components/material-components-android · error · IllegalArgumentException

Album %d does not exist.

Error message

Album %d does not exist.

What it means

Thrown by MusicData.getAlbumById(long) in the catalog's musicplayer demo. It scans the static ALBUMS list and throws IllegalArgumentException when no Album matches the requested id. Sample-data helper, not part of the Material library API.

Source

Thrown at catalog/java/io/material/catalog/musicplayer/MusicData.java:129

              "Annie Chiu",
              R.drawable.album_ellen_qin_unsplash,
              TRACKS,
              "46 mins"),
          new Album(
              9L,
              "On Dry Land",
              "Alfonso Gonzalez",
              R.drawable.album_david_clode_unsplash,
              TRACKS,
              "55 mins"));

  static Album getAlbumById(long albumId) {
    for (Album album : MusicData.ALBUMS) {
      if (album.id == albumId) {
        return album;
      }
    }
    throw new IllegalArgumentException(
        String.format(Locale.US, "Album %d does not exist.", albumId));
  }

  /** A data class to hold information about a music album. */
  public static class Album {

    final long id;
    final String title;
    final String artist;
    @DrawableRes final int cover;
    final List<Track> tracks;
    final String duration;

    static final ItemCallback<Album> DIFF_CALLBACK =
        new ItemCallback<Album>() {
          @Override
          public boolean areItemsTheSame(@NonNull Album newItem, @NonNull Album oldItem) {
            return newItem.title.equals(oldItem.title);

View on GitHub (pinned to ac7e18efee)

Solutions

  1. Pass an album id that exists in MusicData.ALBUMS (inspect the ALBUMS initializer for valid ids).
  2. If you replaced ALBUMS with your own data, keep the ids you pass to getAlbumById consistent with that data.
  3. Check the id against the ALBUMS list before calling, or wrap in try/catch IllegalArgumentException when a missing album is recoverable.

Example fix

// before
Album album = MusicData.getAlbumById(100); // not in ALBUMS

// after
long id = MusicData.ALBUMS.get(0).id; // or a known valid id
Album album = MusicData.getAlbumById(id);
Defensive patterns

Strategy: validation

Validate before calling

boolean exists = false;
for (MusicData.Album a : MusicData.ALBUMS) { if (a.id == albumId) { exists = true; break; } }
if (exists) { MusicData.getAlbumById(albumId); }

Try / catch

Catch IllegalArgumentException where an unselected/unset id (e.g. 0) may reach the lookup; map it to a 'no album selected' UI state.

Prevention

When it happens

Trigger: Calling MusicData.getAlbumById(albumId) with an id not present in MusicData.ALBUMS (e.g. 0, a negative value, or an id beyond the number of hardcoded albums).

Common situations: Using the musicplayer demo as a template and passing ids from your own data source that do not match the sample ids; serializing/passing an album id that defaults to 0 before selection; off-by-one when generating ids.

Related errors


AI-assisted analysis of material-components/material-components-android@ac7e18efee (2026-08-14). Data as JSON: /api/errors/7dd964645bb20b4e. Report an issue: GitHub.