线程“main”java.lang.nullpointerexception中的spring异常:“this.playerrepository”为null

bttbmeg0  于 2021-07-13  发布在  Java
关注(0)|答案(2)|浏览(414)

这个问题在这里已经有答案了

为什么我的spring@autowired字段为空(22个答案)
12天前关门了。
所有人。我试图建立一个游戏应用程序,但遇到了一个问题。我创建了一个spring存储库、实体和服务,但每当调用后者时,我都会遇到一个异常,说明:

Exception in thread "main" java.lang.NullPointerException: Cannot invoke "com.example.demo.repo.PlayerRepository.findById(Object)" because "this.playerRepository" is null
    at com.example.demo.service.PlayerService.findPlayerById(PlayerService.java:16)
    at com.example.demo.DemoApplication.main(DemoApplication.java:16)

以下是我提供的课程,展示了我在课程中包括的内容:
游戏性:

package com.example.demo.model;

import javax.persistence.*;
import java.util.Objects;

@Entity
@Table(name = "player", schema = "public", catalog = "gamestats")
public class PlayerEntity {
    private long id;
    private int health;
    private int damage;
    private int absorb;
    private int regen;
    private int fire;

    @Id
    @Column(name = "id")
    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    @Basic
    @Column(name = "health")
    public int getHealth() {
        return health;
    }

    public void setHealth(int health) {
        this.health = health;
    }

    @Basic
    @Column(name = "damage")
    public int getDamage() {return this.damage; }

    public void setDamage(int damage) {
        this.damage = damage;
    }

    @Basic
    @Column(name = "absorb")
    public int getAbsorb() {
        return absorb;
    }

    public void setAbsorb(int absorb) {
        this.absorb = absorb;
    }

    @Basic
    @Column(name = "regen")
    public int getRegen() {
        return regen;
    }

    public void setRegen(int regen) {
        this.regen = regen;
    }

    @Basic
    @Column(name = "fire")
    public int getFire() {
        return fire;
    }

    public void setFire(int fire) {
        this.fire = fire;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        PlayerEntity that = (PlayerEntity) o;
        return id == that.id && health == that.health && damage == that.damage && absorb == that.absorb && regen == that.regen && fire == that.fire;
    }

    @Override
    public int hashCode() {
        return Objects.hash(id, health, damage, absorb, regen, fire);
    }
}

播放器存储库:

package com.example.demo.repo;

import com.example.demo.model.PlayerEntity;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface  PlayerRepository extends JpaRepository<PlayerEntity, Long> {
}

播放器服务:

package com.example.demo.service;

import com.example.demo.model.PlayerEntity;
import com.example.demo.repo.PlayerRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class PlayerService{
    @Autowired
    PlayerRepository playerRepository;

    private PlayerEntity playerEntity;

    public PlayerEntity findPlayerById(long id) {
        return playerRepository.findById(id).get();
    }

    public void safePlayer(PlayerEntity test){ playerRepository.save(test); }

    public int getPlayerStrength(long id) {
        return playerRepository.findById(id).get().getHealth();
    }
}

演示应用程序:

package com.example.demo;

import com.example.demo.service.PlayerService;
import org.json.JSONException;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import java.io.IOException;

@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) throws IOException, InterruptedException, JSONException {
        SpringApplication.run(DemoApplication.class, args);
        PlayerService playerService = new PlayerService();
        playerService.findPlayerById(1);
    }

我真的很感激你的帮助,因为我真的被卡住了。

hjzp0vay

hjzp0vay1#

服务需要自动连接存储库;只有在由spring上下文创建时才会发生这种情况。通过在main via中创建 new ,Spring没有机会向它注入任何东西,所以它的能量场仍然存在 null .
如果您想使用服务,您需要从一个方法访问它,以确保上下文已经正确创建。你可以通过使用 @PostConstruct 方法。

@SpringBootApplication
public class DemoApplication {
    // usually not held in an application class, but will work
    @Autowired
    private PlayerService service;

    public static void main(String[] args) throws IOException, InterruptedException, JSONException {
        SpringApplication.run(DemoApplication.class, args);
    }

  @PostConstruct
  public void doStuff() {
    // the service will have been initialized and wired into the field by now
    service.findPlayerById(1);
  }
ffvjumwh

ffvjumwh2#

您必须使用optional来确定是否有一个空值对应于任何玩家id。您可以遵循以下语法。

public PlayerEntity findPlayerById(long id) {
        Optional<PlayerEntity> result=playerRepository.findById(id);

        PlayerEntity  player=null;
        if(result.isPresent()){
            player=result.get();
        }else{
            throw new RuntimeException("Did not find player id"+id);
        }
        return player;

    }

相关问题