Skip to content

DinerMenuIterator.hasNext() does not handle null menu entries #44

Description

@prashantpiyush1111

hasNext() only checks whether position is within the array length and does not check whether the current menu entry is null.

DinerMenu uses a fixed-size array, so the menu can be partially populated. When there are unused slots, hasNext() can still return true for those slots, and next() can return null.

If the caller then tries to use the returned MenuItem, for example by calling getName(), this can result in a NullPointerException.

Please restore the null check in hasNext():

if (position >= items.length || items[position] == null) {
return false;
}

This ensures that unused array slots are not treated as valid menu items.

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions