rubenlagus / TelegramBots

Java library to create bots using Telegram Bots API
https://telegram.me/JavaBotsApi
MIT License
4.78k stars 1.22k forks source link

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

Open D0nn1 opened 1 month ago

D0nn1 commented 1 month ago

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:

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

smallufo commented 1 month ago

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 commented 1 month ago

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 commented 2 weeks ago

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