Issue
in this Controller when 'stdin.save(std);' is removed code works finr but when I add this 'stdin.save(std);' line i'm getting 'java.lang.IllegalArgumentException: Target object must not be null' this exception
package com.access6.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import com.access6.dao.StudentInt;
import com.access6.model.Student;
@RestController
public class StudentContrlr {
@Autowired
StudentInt stdin;
Student std;
@RequestMapping("/")
public ModelAndView home() {
return new ModelAndView("home");
}
@RequestMapping("/addStd")
public ModelAndView addStd(@RequestParam ("id") String id, @RequestParam ("name") String name,
@RequestParam ("location") String location,ModelMap mp ) {
mp.put("id", id);
mp.put("name", name);
mp.put("location", location);
stdin.save(std);
return new ModelAndView("home");
}
}
model: @entity cannot create table in database but connection is working fine
package com.access6.model;
import javax.persistence.Entity;
import javax.persistence.Id;
@Entity
public class Student {
@Id
int id;
String name;
String location;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
@Override
public String toString() {
return "Student [id=" + id + ", name=" + name + ", location=" + location + "]";
}
}
interface
package com.access6.dao;
import org.springframework.data.repository.CrudRepository;
import com.access6.model.Student;
public interface StudentInt extends CrudRepository<Student, Integer> {
}
Spring boot app
package com.access6;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DbConnect2Application {
public static void main(String[] args) {
SpringApplication.run(DbConnect2Application.class, args);
}
}
application.properties
spring.mvc.view.prefix= /WEB-INF/pages/
spring.mvc.view.suffix= .jsp
spring.h2.console.enabled=true
spring.datasource.platform=h2
spring.datasource.url=jdbc:h2:mem:vinanth
Solution
please find the below code.
@RequestMapping("/addStd")
public ModelAndView addStd(@RequestParam ("id") String id, @RequestParam ("name") String name,
@RequestParam ("location") String location,ModelMap mp ) {
std = new Student() ;
std.setId(id);
std.setName(name);
std.setLocation(location);
mp.put("id", id);
mp.put("name", name);
mp.put("location", location);
stdin.save(std);
return new ModelAndView("home");
}
OR
Use modelAttribute to set request params into dto automatically,and here request parameter name and variable name from dto should be same.
@RequestMapping("/addStd")
public ModelAndView addStd(@ModelAttribute Student std,ModelMap mp ) {
mp.put("id", std.getId());
mp.put("name", std.getName());
mp.put("location", std.getLocation());
stdin.save(std);
return new ModelAndView("home");
}
Answered By - Ranjith Bokkala
Answer Checked By - Marie Seifert (JavaFixing Admin)