Skip to content

NoSuchElementException when calling getUserFromUserId method in TwitterClient #463

@CWH6

Description

@CWH6

Hi Twitter API Team,

I encountered an issue while using the TwitterClient library to fetch user details by user ID. The code snippet I used is as follows:

TwitterClient twitterClient = new TwitterClient(TwitterCredentials.builder()
    .accessToken(ACCESS_TOKEN)
    .accessTokenSecret(ACCESS_TOKEN_SECRET)
    .apiKey(API_KEY)
    .apiSecretKey(API_KEY_SECRET)
    .bearerToken(BEARER_TOKEN)
    .build());

User user = twitterClient.getUserFromUserId(userId);
return user;

However, when executing this code, I received the following exception:

java.util.NoSuchElementException: null
at java.util.Optional.orElseThrow(Optional.java:290)
at io.github.redouane59.twitter.TwitterClient.getUserFromUserId(TwitterClient.java:370)

Steps to Reproduce:

  1. Initialize the TwitterClient with valid credentials.
  2. Call the getUserFromUserId method with a valid user ID.
  3. The NoSuchElementException is thrown.

Expected Behavior:
The method should return the user details corresponding to the provided user ID.

Actual Behavior:
The method throws a NoSuchElementException when the user is not found or the API response is empty.

Additional Context:

The issue seems to occur when the Optional returned by the API call is empty, and the orElseThrow method is invoked.
It would be helpful if the method could handle cases where the user is not found or the API response is empty more gracefully, perhaps by returning null or throwing a more descriptive exception.
Proposed Solution:
Could you please consider adding error handling for cases where the user is not found or the API response is empty? For example, returning null or throwing a custom exception with a meaningful message would make debugging easier.

Thank you for your attention to this issue. I look forward to your response.

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