2

Field in my project is null and I don't have idea why. I don't initalize in project any autowired variable like new BingoGameService(); - so I don't know why I got null.

public class MessageReaction extends ListenerAdapter {
@Autowired
BingoGameService bingoGameService; //NULL

@Override
public void onMessageReceived(@Nonnull MessageReceivedEvent event) {

    if(event.getMessage().getContentRaw().equals("!createBingo")) bingoGameService.createBingoGameForUser(event.getAuthor().getIdLong());

This is my service class:

@Service
public class BingoGameService {
@Autowired
DiscordUserRepo discordUserRepo;
@Autowired
BingoBoardRepo bingoBoardRepo;
@Autowired
GameMechanics gameMechanics;

    public void createBingoGameForUser(Long id) {

if(bingoBoardRepo.findById(1L).isPresent()){
        String[][] officialBingoBoard = bingoBoardRepo.findById(1L).get().getBingoBoard();
        int[][] scoreBoard = gameMechanics.createScoreBoard(3,3);
        DiscordUser discordUserToSave = new DiscordUser();
        BingoGame bingoGame = new BingoGame(discordUserToSave, officialBingoBoard, scoreBoard);
        discordUserToSave.setUserId(id);
        discordUserToSave.setBingoGame(bingoGame);
        discordUserRepo.save(discordUserToSave);
    } 
}
Kriszu
  • 21
  • 3
  • An `@Autowired` field cannot be `null` **if** it is a proper spring managed instance. If it is `null` it isn't properly managed and you are creating an instance of `MessageReaction` yourself outside the scope of Spring. See https://stackoverflow.com/questions/19896870/why-is-my-spring-autowired-field-null and https://deinum.biz/2020-07-03-Autowired-Field-Null/ – M. Deinum Dec 01 '20 at 06:45

2 Answers2

0

you don't need use @Autowired if you create class with constructor. Add @Component over MessageReaction class

Spring Boot search for annotation like "@Component" "@Service" etc. if you don't include them over class name, it won't know where inject values/classes

@Component
public class MessageReaction extends ListenerAdapter {

private FooService fooService;

 public MessageReaction(FooService fooService){
  this.fooService = fooService
 }


}
marczin
  • 25
  • 5
0

Depends on the category of layering of your app, you can annotate classes as @Component, @Service or @Repository.

I didn't see any annotation for the class MessageReaction. If it is a generic component you have to annotate it with @Component.

This annotation helps Spring container to instantiate the instance of your class MessageReaction and autowire the injected dependencies. Since there is no default constructor for the class MessageReaction, you have to annotate the dependency BingoGameService with @Autowired annotation.

Steephen
  • 14,645
  • 7
  • 40
  • 47
  • I added @Component but it didn't change anything, still getting nullpointer. – Kriszu Dec 01 '20 at 00:39
  • Did you clean and rebuild again? Is there any error message during start up ? Can you share that.? – Steephen Dec 01 '20 at 00:43
  • During clean I got: [ERROR] Invalid packaging for parent POM org.springframework.boot:spring-boot-starter-data-jpa:2.2.6.RELEASE, must be "pom" but is "jar" @ org.springframework.boot:spring-boot-starter-data-jpa:2.2.6.RELEASE I will edit main question and add pom there. – Kriszu Dec 01 '20 at 00:52
  • @Kriszu Do you have sub modules within your project? – Steephen Dec 01 '20 at 00:59
  • No, just single module. I had wrong parent in pom. Now clean and rebuild are good, without fails, but nullpointer still appears. – Kriszu Dec 01 '20 at 01:00
  • I am assuming, you annotated the class as @Component. Is there any other exception or error before NullPointerException? Review the log – Steephen Dec 01 '20 at 01:03