I used Spring to store data in database (when I start to write data to the database correctly)
I send JSON POST to my Spring server request like this:
{
"productName": "name",
"restaurantId": 2,
"categoryId": 3,
"description": "des",
"price": 33.23
}
and receiving function
@RestController
public class NewProductController{
@RequestMapping(value = "/addNewProduct", method = RequestMethod.POST)
public ResponseEntity<NewProduct> update(@RequestBody NewProduct newProduct){
productName = newProduct.getProductName();
categoryId = newProduct.getCategoryId();
restaurantId = newProduct.getRestaurantId();
description = newProduct.getDescription();
price = newProduct.getPrice();
System.out.println(productName);
System.out.println(categoryId);
System.out.println(restaurantId);
System.out.println(description);
System.out.println(price);
NewProductService newProductService = new NewProductService();
newProductService.setData();
return new ResponseEntity<NewProduct>(newProduct, HttpStatus.OK);
}
Data from the println display correctly.
And set setData function from NewProductService
public class NewProductService {
@Autowired
CategoryRepository categoryRepository;
private String productName;
private Long categoryId;
private Long restaurantId;
private String description;
private Double price;
//[...]
@Transactional
public void setData(){
Category customer = categoryRepository.findOne(1L);
System.out.println("Customer found with findOne(1L):");
System.out.println("--------------------------------");
System.out.println(customer);
System.out.println();
}
In Category customer = categoryRepository.findOne(1L);
I get a error:
2015-08-18 23:12:27.716 ERROR 8092 --- [nio-8080-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause
java.lang.NullPointerException: null
at com.drinkandfood.server.Services.NewProductService.setData(NewProductService.java:46)
When I put this code in
@Override
public void run(String... strings) throws Exception {
Category customer = categoryRepository.findOne(1L);
System.out.println("Customer found with findOne(1L):");
System.out.println("--------------------------------");
System.out.println(customer);
System.out.println();
}
I do not get an error, but the expected answer. Where is the problem?