0

Model.java:

import lombok.Data;
@Entity
@Table(name = "CUSTOMER")
@Data
public class Model{
@Id
@Column(name = "UNIQ_ID")
private String uniqId;
@Column(name = "CUSTOMER_ID",insertable = false,updatable=false)
private String customerId;
@Column(name = "STATUS")
private String status;
@ManyToOne
@JoinColumn(name = "CUSTOMER_ID")
private Customer model;
}

ModelRepository.java

public interface ModelRepository extends JpaRepository<Model, 
String>,JpaSpecificationExecutor<Model> {
}

ModelService.java

@Inject
private ModelRepository modelRepo;
@Transactional
public void registerCustomermodel(Model modeldata) {
    modelRepo.save(modeldata);
}   
public static void main(String[] args)
{
    ModelService cs=new ModelService();
    Model model=new Model();
    model.setCustomerId("111000000373");
    model.setUniqId("117");
    model.setStatus("null");
    cs.registerCustomermodel(model);
}

All my data source configuration files are well figured, while executing this im getting null pointer exception. Can anyone explain what are the possible reasons?

Neil Stockton
  • 11,383
  • 3
  • 34
  • 29
harish
  • 143
  • 3
  • 10
  • 3
    First step: tell us _where exactly_ you get that NPE or best post the stacktrace and mark the relevant lines in your posted code. – Thomas May 04 '17 at 13:40
  • Exception in thread "main" java.lang.NullPointerException at modelRepo.save(modeldata) and cs.registerCustomermodel(model); – harish May 04 '17 at 13:52
  • 2
    You notice that this doesn't match the term "exactly", don't you? We'd need to know which line hence the request to post the stacktrace. However, I suspect the problem is `new ModelService()`. If you create instances like this the injection system normally doesn't notice the object creation and hence injection is not triggered. Try getting the instance from Spring (assuming you're using that for injection) instead. – Thomas May 04 '17 at 13:55
  • In the main method, you are not setting the field private Customer model; Maybe that throws you NPE? – developer_hatch May 04 '17 at 14:01
  • I m getting the same error and unable to fix it out .So i switched to prepared statement instead. – Amit Gujarathi May 04 '17 at 14:08
  • Exception in thread "main" java.lang.NullPointerException at ModelService.registerCustomermodel(ModelService.java:163) at ModelService.main(ModelService.java:187) this is my stacktrace. i dont think instance creation is the problem because i printed some variables in regsiterCustomerModel method. @Thomas – harish May 04 '17 at 14:14
  • The injection of the ModelRepository failed. Check if you include @EnableJpaRepositories so that it is able to scan your repository interface. – Chris May 04 '17 at 14:53
  • [What does your step debugger tell you?](http://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) –  May 04 '17 at 16:38

0 Answers0