简体   繁体   中英

Null pointer exception spring boot jpa

I am trying to pass values to the persistence layer in a springboot application using jpa. However, each time I get a null pointer exception despite that I see that the object to be persisted is well formed. I have my code snippets below.

AlertRepository

public interface AlertRepository extends JpaRepository<Alert, Integer>{
 }

AlertController

@Controller
public class AlertController {

@Autowired
private AlertRepository alertRepository;    
@GetMapping(path="/add") // Map ONLY GET Requests
public @ResponseBody String addNewAlert (@RequestParam Alert cAlert) {
    Alert alert = new Alert();
    alert.setAlert(cAlert.getAlert());
    alert.setAttack(cAlert.getAttack());
    alertRepository.save(alert);

    .....
    .....
    return "Saved";
}

Get_Save_Alert

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);
            }

Here is the exception stacktrace:

Exception : null
java.lang.NullPointerException
at de.cavas.repository.AlertController.addNewAlert(AlertController.java:26)
at de.cavas.SecurityTest.preRegistrationTest(SecurityTest.java:281)
at 
de.cavas.InstanceRegistry.handleTempRegistration(InstanceRegistry.java:250)
at de.cavas.InstanceRegistry.register(InstanceRegistry.java:155)

Line 26 in the trace refers to the line alertRepository.save(alert) in the AlertController Class .

Update I have also provided the application.yml file incase there is something "fishy" I cannot see !

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

update 2 - the Alert entity class

@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
 }

Can you try doing these two points

  1. Have you added @EnableJpaRepositories annotation or xml configuration to enable Spring-data repository support. Xml configuration like - <jpa:repositories base-package="com.acme.repositories"/> .

2 I think you should remove static keyword from Repository Autowiring and make it private if it is used in only this clas, for instance

@Autowired
private AlertRepository alertRepository; 
@Autowired
private AlertRepository alertRepository;

and On your main application class add @EnableAutoConfiguration

@SpringBootApplication
@EnableAutoConfiguration
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

Also if you are not using @Repository then spring will never create bean for your repo, In that case it will throw nullPointer exception

  @Repository 
    public interface AlertRepository extends
     JpaRepository<Alert, Integer>{  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM