“线程中的异常”;“主要”;java.lang.NullPointerException:“0”;这是“playerRepository”;是空的
各位。我正在尝试构建一个游戏应用程序,但遇到了一个问题。我创建了一个Spring存储库、实体和服务,但无论何时调用后者,我都会遇到一个异常,说明:“线程中的异常”;“主要”;java.lang.NullPointerException:“0”;这是“playerRepository”;是空的,java,spring,spring-boot,rest,Java,Spring,Spring Boot,Rest,各位。我正在尝试构建一个游戏应用程序,但遇到了一个问题。我创建了一个Spring存储库、实体和服务,但无论何时调用后者,我都会遇到一个异常,说明: Exception in thread "main" java.lang.NullPointerException: Cannot invoke "com.example.demo.repo.PlayerRepository.findById(Object)" because "this.player
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);
}
播放器存储库:
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);
}
演示应用程序:
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);
}
非常感谢您的帮助,因为我真的被卡住了。您必须使用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;
}
public PlayerEntity findPlayerById(长id){
可选结果=playerRepository.findById(id);
PlayerEntity player=null;
if(result.isPresent()){
player=result.get();
}否则{
抛出新的RuntimeException(“未找到玩家id”+id);
}
返回球员;
}
服务需要自动连接存储库;只有在由Spring上下文创建时才会发生这种情况。通过在main中通过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);
}
这不会帮助存储库为空,但作为说明,您可以更简洁地将其编写为
return repo.findById(id).orelsetrow(new RuntimeException(“无此类id”+id))
。