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

bot.execute(SendDocument) - NullPointerException #1112

Closed Mark1708 closed 2 years ago

Mark1708 commented 2 years ago

Hi! Please tell me what the problem is.

When trying to send a document (not empty, 250 bytes), I catch a NullPointerException. As you can see, I have done validation checks on both InputFile and SendDocument. At the same time, there is no problem with sending text messages using the execute method.

I use telegrambots-spring-boot-starter 6.1.0 and Java 8.

public BotApiMethod<?> handle(SendMessage sendMessage, Client client) {
        try {
            InputFile document = fileService.generateClientDataCsv();
            document.validate();
            log.info("Success validate document");
            System.out.println(document.toString());
            SendDocument sendDocument = SendDocument.builder()
                    .chatId(client.getChatId())
                    .document(document)
                    .build();
            sendDocument.validate();
            log.info("Success validate sendDocument");
            System.out.println(sendDocument.toString());
            telegramBot.execute(sendDocument);
            sendMessage.setText("Файл успешно сгенерирован");
        } catch (TelegramApiException | NullPointerException e) {
            sendMessage.setText("При генерации файла произошла ошибка");
            e.printStackTrace();
        }
        return sendMessage;
    }

StackTrace

java.lang.NullPointerException
    at org.telegram.telegrambots.bots.DefaultAbsSender.execute(DefaultAbsSender.java:190)
    at legal.com.telegrambot.bot.FileRequestHandler.handle(FileRequestHandler.java:51)
    at legal.com.telegrambot.bot.MessageHandler.handle(MessageHandler.java:101)
    at legal.com.telegrambot.bot.TelegramFacade.processInputMessage(TelegramFacade.java:108)
    at legal.com.telegrambot.bot.TelegramFacade.handleUpdate(TelegramFacade.java:49)
    at legal.com.telegrambot.bot.model.TelegramBot.onWebhookUpdateReceived(TelegramBot.java:43)...
Mark1708 commented 2 years ago

Solved the problem by making the sendDocument method in the TelegramBot class

FileRequestHandler.java

public BotApiMethod<?> handle(SendMessage sendMessage, Client client) {
        try {
            InputFile document = fileService.generateClientDataCsv();

            SendDocument sendDocument = SendDocument.builder()
                    .chatId(client.getChatId())
                    .document(document)
                    .caption("Список пользователей с подписками")
                    .parseMode(ParseMode.MARKDOWN)
                    .build();
            telegramBot.sendDocument(sendDocument);
            sendMessage.setText("Файл успешно сгенерирован");
        } catch (Exception e) {
            sendMessage.setText("При генерации файла произошла ошибка");
            e.printStackTrace();
        }
        return sendMessage;
    }

TelegramBot.java

public void sendDocument(SendDocument sendDocument) {
        try {
            execute(sendDocument);
        } catch (TelegramApiException e) {
            e.printStackTrace();
        }
    }