Implementing callback buttons in Java Telegram bot

I’m working on a Telegram bot using Java and trying to implement callback buttons. I managed to create the buttons but they’re not functioning properly. When users click the buttons, I get a NullPointerException and I can’t figure out how to handle the callback responses.

I need help understanding how to properly handle button clicks and send responses back to users. Here’s what I have so far:

public void sendMessageWithButton(String chatId, File image) {
    InlineKeyboardMarkup markup = new InlineKeyboardMarkup();
    InlineKeyboardButton btn = new InlineKeyboardButton();
    
    List<List<InlineKeyboardButton>> rowList = new ArrayList<>();
    List<InlineKeyboardButton> row = new ArrayList<>();
    btn.setText("Click Me");
    btn.setCallbackData("button_pressed");
    row.add(btn);
    rowList.add(row);
    markup.setKeyboard(rowList);
    
    SendPhoto photoMessage = new SendPhoto();
    photoMessage.setChatId(chatId);
    photoMessage.setNewPhoto(image);
    photoMessage.setReplyMarkup(markup);
    
    try {
        sendPhoto(photoMessage);
    } catch (TelegramApiException ex) {
        ex.printStackTrace();
    }
}

I’m getting this error:

SEVERE: BOTSESSION
java.lang.NullPointerException
    at mybot.SimpleBot.onUpdateReceived(SimpleBot.java:62)

Can someone provide guidance on handling callback queries properly?

you gotta separate your handling for regular messages n callback queries in onUpdateReceived. if you call getMessage() on a callback update, it returns null. so, wrap the message handling in if(update.hasMessage()) and place the callback handling next to it, not inside.

You’re missing the callback acknowledgment - that’s what’s breaking things. When you handle a callback query, you’ve got to answer it or the button stays in loading state and eventually times out.

Drop this code after you process the callback data:

AnswerCallbackQuery answerCallbackQuery = new AnswerCallbackQuery();
answerCallbackQuery.setCallbackQueryId(callbackQuery.getId());
answerCallbackQuery.setText("Button clicked successfully!");
answerCallbackQuery.setShowAlert(false);
try {
    answerCallbackQuery(answerCallbackQuery);
} catch (TelegramApiException e) {
    e.printStackTrace();
}

Telegram sits there waiting for your response if you don’t acknowledge it, and your bot just locks up. The setShowAlert part decides if users see a popup or just a quick notification. Found this out when my bot kept freezing every time someone hit a button.

The NullPointerException is happening in your onUpdateReceived method when you’re handling the callback query. Your button code looks fine - the problem’s in how you’re processing the callback data.

When someone clicks an inline button, Telegram sends a CallbackQuery object, not a regular message. You need to check for callback queries first, then grab the data.

In your onUpdateReceived method, try this:

if (update.hasCallbackQuery()) {
CallbackQuery callbackQuery = update.getCallbackQuery();
String data = callbackQuery.getData();
String chatId = callbackQuery.getMessage().getChatId().toString();

if ("button_pressed".equals(data)) {
    // Handle your button click here
    // Send response message or edit the original message
}

}

The null pointer’s probably happening because you’re trying to access message properties on a callback update without checking what type of update it is first. Make sure you’re separating regular messages from callback queries in your handler.