M
M
MeeLeSh2021-11-12 17:56:59
Java
MeeLeSh, 2021-11-12 17:56:59

NullPointerException on injection?

Writes that java.lang.NullPointerException: Cannot invoke "Bot.services.DefaultTemplates.sendWelcomePage(org.telegram.telegrambots.meta.api.methods.send.SendMessage)" because "this.defaultTemplates" is null
although it seems like it should be injected Anyway, I don't understand what I'm doing wrong. I tried to do it manually through the configuration class, it didn’t help either, I tried to make all the injections, through the constructors, also to no avail. What am I doing wrong? Thanks

package Bot.сontrollers;

import Bot.services.MyBot;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.telegram.telegrambots.meta.api.methods.BotApiMethod;
import org.telegram.telegrambots.meta.api.objects.Update;

@RestController
public class WebHookController {

    private MyBot myBot;

    @Autowired
    public WebHookController(MyBot myBot) {
        this.myBot = myBot;
    }

    @PostMapping("/")
    public BotApiMethod<?> WebHookController(@RequestBody Update update, MyBot myBot) {
        return myBot.onWebhookUpdateReceived(update);
    }

    @GetMapping
    public ResponseEntity get() {
        return ResponseEntity.ok().build();
    }
}


package Bot.services;

import lombok.Getter;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.telegram.telegrambots.bots.TelegramWebhookBot;
import org.telegram.telegrambots.meta.api.methods.BotApiMethod;
import org.telegram.telegrambots.meta.api.methods.send.SendMessage;
import org.telegram.telegrambots.meta.api.objects.Message;
import org.telegram.telegrambots.meta.api.objects.Update;

@Getter
@Setter
@Service
public class MyBot extends TelegramWebhookBot {

    @Autowired
    private DefaultTemplates defaultTemplates;

    @Value("${bot.webHookPath}")
    private String botPath;

    @Value("${bot.name}")
    private String botUsername;

    @Value("${bot.token}")
    private String botToken;

    @Override
    public String getBotUsername() {
        return botUsername;
    }

    @Override
    public String getBotToken() { return botToken; }

    @Override
    public String getBotPath() {
        return botPath;
    }

    @Override
    public BotApiMethod<?> onWebhookUpdateReceived(Update update) {
        if (update.hasCallbackQuery()) {

            return null;
        } else {

            Message message = update.getMessage();

            SendMessage sendMessage = new SendMessage();
            sendMessage.setChatId(String.valueOf(message.getChatId()));

            if (message.hasText()) {
                return defaultTemplates.sendWelcomePage(sendMessage);
            }
        }
        return null;
    }
}


package Bot.services;


import org.springframework.stereotype.Component;
import org.telegram.telegrambots.meta.api.methods.send.SendMessage;
import org.telegram.telegrambots.meta.api.objects.replykeyboard.ReplyKeyboardMarkup;
import org.telegram.telegrambots.meta.api.objects.replykeyboard.buttons.KeyboardRow;

import java.util.ArrayList;

@Component
public class DefaultTemplates {
    public SendMessage sendWelcomePage(SendMessage sendMessage) {
        ReplyKeyboardMarkup replyKeyboardMarkup = new ReplyKeyboardMarkup();
        ArrayList<KeyboardRow> keyboard = new ArrayList<>();
        replyKeyboardMarkup.setSelective(true);
        replyKeyboardMarkup.setResizeKeyboard(true);
        replyKeyboardMarkup.setOneTimeKeyboard(false);
        KeyboardRow keyboardRow = new KeyboardRow();
        keyboardRow.add("hello world");
        keyboard.add(keyboardRow);
        replyKeyboardMarkup.setKeyboard(keyboard);
        sendMessage.setReplyMarkup(replyKeyboardMarkup);
        sendMessage.setText("hello world");
        return sendMessage;
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
BorLaze, 2021-11-13
@BorLaze

Is Bot.services written in @ComponentScan?
Another thing that confuses is that there is no DefaultTemplates in MyBot imports. How does it even compile?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question