I am writing a web app with Spring Boot, Hibernate and PostgreSQL. I want to learn how to save things in DB, but now I can`t resolve my problem. I am getting an error caused by my controller by line:
silniaRepository.save(silniaDB); error is just:
java.lang.NullPointerException
there is my pom:
<name>silnia</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.2.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF- 8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.postgresql</groupId> <artifactId>postgresql</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.mortbay.jetty</groupId> <artifactId>jetty-maven-plugin</artifactId> <version>8.1.5.v20120716</version> </plugin> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project> part of my controller:
@Controller public class SilniaController { @Autowired public SilniaService silniaService; public SilniaRepository silniaRepository; public SilniaDB silniaDB; @RequestMapping("/db") @ResponseBody public String testMethod() { StringBuilder response = new StringBuilder(); SilniaDB silniaDB = new SilniaDB() .setNumber1(23); silniaRepository.save(silniaDB); for (SilniaDB i : silniaRepository.findAll()) { response.append(i).append("<br>"); } return response.toString(); } my DB model:
@Entity public class SilniaDB { @Id @GeneratedValue(strategy = GenerationType.AUTO) private Long id; @Column private int number; public int getNumber() { return number; } public void setNumber (int number){this.number=number;} public SilniaDB setNumber1(int number) { this.number = number; return this; } public SilniaDB withNumber(final int number) { this.number = number; return this; } public Long getId() { return id; } public void setId(Long id) { this.id = id; } @Override public String toString() { return "TaskEntity{" + "id=" + id + "number=" + number + '}'; } } and typical repository interface:
@Repository public interface SilniaRepository extends CrudRepository<SilniaDB, Long> { public SilniaDB findByNumber(Integer number); } I really spent a lot of time on that issue. Thanks in advance for every comment or answer.