1

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
 }
15
  • 2
    Would be nice to tell us in which line you get the NPE.. By the way, it is called exception, not exemption Commented Apr 15, 2018 at 13:30
  • 1
    The stacktrace is still quite useless unless you tell us which line is line 26... Commented Apr 15, 2018 at 13:31
  • 4
    Spring will never autowire static fields. That's why it's null, and that's why you get a NullPointerException. Commented Apr 15, 2018 at 13:33
  • @user3237736 line 26 is in the AlertController Class the line " alertRepository.save(alert);" Commented Apr 15, 2018 at 13:36
  • 1
    So that means alertRepository is null (autowiring failed). How did you autowire it? Btw why is it static? Commented Apr 15, 2018 at 13:37

2 Answers 2

1
@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>{  }
Sign up to request clarification or add additional context in comments.

9 Comments

Why don't I see @Repository used on to of your repository.
Also can you also add Alert model here ?
I used the annotation @EnableJpaRepositories, do I need to use Repository also ? I will update with the Alert pojo asap
yea @Repository public interface AlertRepository extends JpaRepository<Alert, Integer>{ }
You do have this table created correct, under your schema of db? I don't see any hibernate mapping there.
|
0

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; 

1 Comment

I have done added @EnableJpaRepositories and taken out the static keyword ...but no joy yet !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.