flicus / vertx-telegram-bot-api

Telegam bot api for vert.x
MIT License
12 stars 6 forks source link

Null Pointer Exception using Command Handler #5

Open rifkyaziz opened 5 years ago

rifkyaziz commented 5 years ago

Hello, I have an NPE when trying to use command handler in vertx

   TelegramOptions telegramOptions = new TelegramOptions()
            .setBotToken(TELEGRAM_TOKEN);
    CommandHandler commandHandler = new CommandHandler()
            .addCommand(new StartCommand());
    bot = TelegramBot.create(vertx, telegramOptions)
            .receiver(new LongPollingReceiver().onUpdate(commandHandler))
            .start();

StartCommand class:

@BotCommand(message = "^/start")
public class StartCommand extends Command {
   @Override
   public void execute(CommandContext commandContext, Handler<Boolean> handler) {
       getBot().sendMessage(new SendMessage().setChatId(commandContext.getUpdate().getMessage().getChatId()).setText("TEST"));
    handler.handle(Boolean.TRUE);
   }
}

And this is the stacktrace 15:10:33,806 WARN vert.x-eventloop-thread-0 CommandHandler:handle:100 - onUpdate: /start, rifkyaziz 15:10:33,807 ERROR vert.x-eventloop-thread-0 LongPollingReceiver:lambda$null$0:138 - ### Exception in update handler: java.lang.NullPointerException at org.schors.vertx.telegram.bot.commands.CommandHandler.createContext(CommandHandler.java:154) at org.schors.vertx.telegram.bot.commands.CommandHandler.handle(CommandHandler.java:101) at org.schors.vertx.telegram.bot.commands.CommandHandler.handle(CommandHandler.java:36) at org.schors.vertx.telegram.bot.LongPollingReceiver$PollHandler.lambda$null$0(LongPollingReceiver.java:136) at java.util.Iterator.forEachRemaining(Iterator.java:116) at java.util.Spliterators$IteratorSpliterator.forEachRemaining(Spliterators.java:1801) at java.util.stream.ReferencePipeline$Head.forEach(ReferencePipeline.java:580) at org.schors.vertx.telegram.bot.LongPollingReceiver$PollHandler.lambda$null$1(LongPollingReceiver.java:129) at io.vertx.core.http.impl.HttpClientResponseImpl$BodyHandler.notifyHandler(HttpClientResponseImpl.java:307) at io.vertx.core.http.impl.HttpClientResponseImpl.lambda$bodyHandler$0(HttpClientResponseImpl.java:197) at io.vertx.core.http.impl.HttpClientResponseImpl.handleEnd(HttpClientResponseImpl.java:261) at io.vertx.core.http.impl.ClientConnection.handleResponseEnd(ClientConnection.java:358) at io.vertx.core.http.impl.ClientHandler.handleMessage(ClientHandler.java:100) at io.vertx.core.http.impl.ClientHandler.handleMessage(ClientHandler.java:36) at io.vertx.core.net.impl.VertxHandler.lambda$channelRead$1(VertxHandler.java:150) at io.vertx.core.impl.ContextImpl.lambda$wrapTask$2(ContextImpl.java:342) at io.vertx.core.impl.ContextImpl.executeFromIO(ContextImpl.java:200) at io.vertx.core.net.impl.VertxHandler.channelRead(VertxHandler.java:148) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:362) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:348) at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:340) at io.netty.handler.timeout.IdleStateHandler.channelRead(IdleStateHandler.java:286) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:362) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:348) at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:340) at io.netty.channel.CombinedChannelDuplexHandler$DelegatingChannelHandlerContext.fireChannelRead(CombinedChannelDuplexHandler.java:438) at io.netty.handler.codec.ByteToMessageDecoder.fireChannelRead(ByteToMessageDecoder.java:310) at io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:284) at io.netty.channel.CombinedChannelDuplexHandler.channelRead(CombinedChannelDuplexHandler.java:253) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:362) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:348) at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:340) at io.netty.handler.logging.LoggingHandler.channelRead(LoggingHandler.java:241) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:362) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:348) at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:340) at io.netty.handler.ssl.SslHandler.unwrap(SslHandler.java:1336) at io.netty.handler.ssl.SslHandler.decodeJdkCompatible(SslHandler.java:1127) at io.netty.handler.ssl.SslHandler.decode(SslHandler.java:1162) at io.netty.handler.codec.ByteToMessageDecoder.decodeRemovalReentryProtection(ByteToMessageDecoder.java:489) at io.netty.handler.codec.ByteToMessageDecoder.callDecode(ByteToMessageDecoder.java:428) at io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:265) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:362) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:348) at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:340) at io.netty.channel.DefaultChannelPipeline$HeadContext.channelRead(DefaultChannelPipeline.java:1359) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:362) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:348) at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:935) at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:134) at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:645) at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:580) at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:497) at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:459) at io.netty.util.concurrent.SingleThreadEventExecutor$5.run(SingleThreadEventExecutor.java:858) at java.lang.Thread.run(Thread.java:748)

flicus commented 5 years ago

Hello, as a workaround you can check how i use it here: https://github.com/flicus/flibot/blob/dev/bot/src/main/java/org/schors/flibot/FliBot.java

if you have a time and willing to help to improve the api, you are welcome, as i am short in time now and not sure when i will have a time to fix this issue myself