Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 零指针异常spring boot jpa_Java_Spring Boot_Spring Data_Spring Data Jpa - Fatal编程技术网

Java 零指针异常spring boot jpa

Java 零指针异常spring boot jpa,java,spring-boot,spring-data,spring-data-jpa,Java,Spring Boot,Spring Data,Spring Data Jpa,我试图使用jpa将值传递到springboot应用程序中的持久层。然而,每次我得到一个空指针异常,尽管我看到要持久化的对象是格式良好的。下面是我的代码片段 AlertRepository public interface AlertRepository extends JpaRepository<Alert, Integer>{ } 获取保存警报 ApiResponse hh = zapClient.core.numberOfAlerts(target); Lis

我试图使用jpa将值传递到springboot应用程序中的持久层。然而,每次我得到一个空指针异常,尽管我看到要持久化的对象是格式良好的。下面是我的代码片段

AlertRepository

public interface AlertRepository extends JpaRepository<Alert, Integer>{
 }
获取保存警报

ApiResponse hh = zapClient.core.numberOfAlerts(target);
        List<Alert> alertList = zapClient.getAlerts(target, 0, 0);
        // zapClient.core.alerts(target, start, count);
        System.out.println("the number of alerts is : " + hh);
        de.cavas.model.Alert cavasAlert = new de.cavas.model.Alert();
        for (Alert alert : alertList) {
            cavasAlert.setRisk(alert.getRisk().toString());
            cavasAlert.setConfidence(alert.getConfidence().toString());
            cavasAlert.setUrl((alert.getUrl().toString()));
            cavasAlert.setParam(alert.getParam().toString());
            cavasAlert.setSolution(alert.getSolution());
            cavasAlert.setCweid(String.valueOf(alert.getCweId()));
            cavasAlert.setWascid(String.valueOf(alert.getWascId()));
            cavasAlert.setAttack(alert.getAttack());
            cavasAlert.setDescription(alert.getDescription());
            cavasAlert.setName(alert.getName());
            cavasAlert.setPluginId(alert.getPluginId());
            cavasAlert.setReference(alert.getReference());


            controller.addNewAlert(cavasAlert);
            }
跟踪中的第26行指的是
alertRepository.save(alert)
行,位于
AlertController类中

更新 我还提供了application.yml文件,以防有我看不到的“可疑”信息

server:
port: 8761
eureka:
    client:
          registerWithEureka: false
         fetchRegistry: false
server:
      waitTimeInMsWhenSyncEmpty: 0    
spring:
   datasource:
       url: jdbc:mysql://sssss:500/vulncorrelate?useSSL=false
   username: ss
   password: ss
   platform: mysql
   initialize: false
 jpa:
   database-platform: org.hibernate.dialect.MySQLDialect
   generate-ddl: true
   spring.jpa.show-sql: true
   hibernate.ddl-auto: update
更新2-
警报
实体类

@Entity
@JsonInclude(JsonInclude.Include.NON_NULL)

public class Alert {

String microserviceName;
String microservicePort;
String microserviceIpAddress;
String microserviceId;
String timeStamp;

@JsonProperty("sourceid")
private String sourceid;


@JsonIgnore
@JsonProperty("other")
private String other;

@JsonProperty("method")
private String method;

@Lob
@JsonProperty("evidence")
private String evidence;

@JsonProperty("pluginId")
private String pluginId;

@JsonProperty("cweid")
private String cweid;

@JsonProperty("confidence")
private String confidence;

@JsonProperty("wascid")
private String wascid;

@JsonProperty("description")
private String description;

@JsonProperty("messageId")
private String messageId;

@Lob
@JsonProperty("url")
private String url;

@Lob
@JsonProperty("reference")
private String reference;

@JsonProperty("solution")
private String solution;

@Lob
@JsonProperty("alert")
private String alert;

@Lob
@JsonProperty("param")
private String param;

@Lob
@JsonProperty("attack")
private String attack;
@JsonProperty("name")
private String name;
@JsonProperty("risk")
private String risk;
@JsonProperty("id")
private int id;

@JsonProperty("sourceid")
public String getSourceid() {
    return sourceid;
}

public Alert(String microserviceName, String microservicePort, String 
microserviceIpAddress, String microserviceId,
        String timeStamp, String sourceid, String other, String method, 
String evidence, String pluginId,
        String cweid, String confidence, String wascid, String description, 
String messageId, String url,
        String reference, String solution, String alert, String param, 
String attack, String name, String risk,
        int id) {
    super();
    this.microserviceName = microserviceName;
    .....
}

public Alert() {
    // TODO Auto-generated constructor stub
}

...... //setters and getters
 }

你能试着做到这两点吗

  • 您是否添加了@EnableJpaRepositories注释或xml配置以启用Spring数据存储库支持。 Xml配置,如-
  • 2我认为您应该从存储库自动连接中删除static关键字,并将其设置为私有的,例如,如果它仅用于此clas中

    @Autowired
    private AlertRepository alertRepository; 
    
    及 在你的主应用程序类上 添加 @EnableAutoConfiguration

    @SpringBootApplication
    @EnableAutoConfiguration
    public class Application {
        public static void main(String[] args) {
            SpringApplication.run(Application.class, args);
        }
    }
    
    另外,若您并没有使用@Repository,那个么spring将永远不会为您的repo创建bean,在这种情况下,它将抛出nullPointer异常

      @Repository 
        public interface AlertRepository extends
         JpaRepository<Alert, Integer>{  }
    
    @存储库
    公共接口AlertRepository扩展
    JpaRepository{}
    
    请告诉我们您在哪一行获得NPE。。顺便说一句,它被称为exception,而不是exception stacktrace仍然非常无用,除非你告诉我们哪一行是第26行……Spring永远不会自动连接静态字段。这就是为什么它是空的,这就是为什么您会得到一个NullPointerException。@user3237736第26行在AlertController类的“alertRepository.save(alert);”行中,这意味着
    alertRepository
    null
    (自动连线失败)。你是怎么自动连线的?顺便问一句,为什么它是静态的?我已经添加了
    @EnableJpaRepositories
    并去掉了
    静态的
    关键字…但是还没有快乐!为什么我看不到@Repository在您的存储库的上使用。您也可以在这里添加警报模型吗?我使用了注释
    @EnableJpaRepositories
    ,我还需要使用
    存储库吗?我将使用
    Alert
    pojo asapyea@Repository公共接口AlertRepository extends JpaRepository{}更新。您是否在db模式下正确创建了此表?我没有看到任何hibernate映射。
    @Autowired
    private AlertRepository alertRepository;
    
    @SpringBootApplication
    @EnableAutoConfiguration
    public class Application {
        public static void main(String[] args) {
            SpringApplication.run(Application.class, args);
        }
    }
    
      @Repository 
        public interface AlertRepository extends
         JpaRepository<Alert, Integer>{  }