Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Abilities: java.lang.NullPointerException: Cannot invoke "java.util.List.stream()" because "this.replies" is null #1436

Open
D0nn1 opened this issue Sep 26, 2024 · 3 comments

Comments

@D0nn1
Copy link

D0nn1 commented Sep 26, 2024

Describe the bug
When trying to create a Telegram bot using the Abilities framework, a NullPointerException is thrown, indicating that the replies list is null when trying to invoke the stream() method.

To Reproduce
Steps to reproduce the behavior:

  1. Create a PartyBot class extending AbilityBot
  2. Implement the creatorId() method and sayHelloWorld() Ability
  3. Create a TgBotApplication class with a main method to register the bot
  4. Run the application
  5. Observe the NullPointerException

Expected behavior
The bot should register successfully and be able to respond to the /hello command without throwing any exceptions.

Code Snippets
PartyBot class:

public class PartyBot extends AbilityBot {

    public PartyBot(TelegramClient telegramClient, String botUsername) {
        super(telegramClient, botUsername);
    }

    @Override
    public long creatorId() {
        return 12345678L;
    }

    public Ability sayHelloWorld() {
        return Ability
                .builder()
                .name("hello")
                .info("says hello world!")
                .locality(ALL)
                .privacy(PUBLIC)
                .action(ctx -> silent.send("Hello world!", ctx.chatId()))
                .build();
    }
}

TgBotApplication class:

public class TgBotApplication {
    public static void main(String... args) {
        try {
            TelegramBotsLongPollingApplication botsApplication = new TelegramBotsLongPollingApplication();
            botsApplication.registerBot(BOT_TOKEN, new PartyBot(new OkHttpTelegramClient(BOT_TOKEN), BOT_NAME));
        } catch (TelegramApiException e) {
            e.printStackTrace();
        }
    }
}

Error message

Exception in thread "pool-1-thread-1" java.lang.NullPointerException: Cannot invoke "java.util.List.stream()" because "this.replies" is null

Environment:

  • Java version: 17
  • Telegram Bot API library version: 7.10.
  • Operating System: Windows 11

Additional context
This issue seems similar to a problem reported by other users.

@smallufo
Copy link

I have the same error , with kotlin version :

@Named
class EchoAbility(
  telegramClient: TelegramClient,
  @Value("\${telegram.bot.creatorId}")
  val creatorId: Long,
  @Value("\${telegram.bot.username}")
  val name: String
) : AbilityBot(telegramClient, name) {
  override fun creatorId(): Long {
    return creatorId
  }



  fun startBot(): Ability {
    return Ability
      .builder()
      .name("start")
      .info("says hello world!")
      .locality(Locality.ALL)
      .privacy(Privacy.PUBLIC)
      .action { ctx: MessageContext ->
        logger.info { "sending hello world ... ctx = $ctx" }
        silent.send("[EchoAbility] Hello world!", ctx.chatId())
      }
      .build()
  }


  companion object {
    private val logger = KotlinLogging.logger { }
  }
}

test code

class EchoAbilityTest : AbstractChatbotTest() {

  @Inject
  private lateinit var echoAbility: AbilityBot

  @Value("\${telegram.bot.token}")
  private lateinit var botToken: String

  @Test
  fun waitForever() {
    val botsApplication = TelegramBotsLongPollingApplication()

    botsApplication.registerBot(botToken, echoAbility)

    val latch = CountDownLatch(1)
    latch.await()
  }
}

It runs OK , but when I send a message , I also encountered the same error

Exception in thread "pool-1-thread-1" java.lang.NullPointerException: Cannot invoke "java.util.List.stream()" because "this.replies" is null

I am not sure if this is related (abilities = null)

截圖 2024-09-26 22 24 45

@SimoneMarzelli
Copy link

I'm having the same issue. It seems the onRegister method from BaseAbilityBot


    public void onRegister() {
        registerAbilities();
        initStats();
    }

is not called.

Solved it by calling it explicitely in my bot's constructor and it doesn´t seem to cause any issues, Hope this helps.


    public BotService() {
        super(new OkHttpTelegramClient(System.getenv("BOT_TOKEN")), "");
        this.onRegister();
    }

@qiushuitian
Copy link

I'm having the same issue. It seems the onRegister method from BaseAbilityBot


    public void onRegister() {
        registerAbilities();
        initStats();
    }

is not called.

Solved it by calling it explicitely in my bot's constructor and it doesn´t seem to cause any issues, Hope this helps.


    public BotService() {
        super(new OkHttpTelegramClient(System.getenv("BOT_TOKEN")), "");
        this.onRegister();
    }

It's help

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

No branches or pull requests

4 participants