「:ターゲットオブジェクトがnullであってはなりませんjava.lang.IllegalArgumentExceptionが」ローカルホストでの入力を与えている間、なぜ私は例外を取得していますか?

vinanth BS:

このコントローラの 'stdin.save(STD);' 削除されたコードがfinr動作しますが、私は、この追加したときには「stdin.save(STD)を;」この例外は:i線は、「ターゲットオブジェクトがnullであってはなりませんjava.lang.IllegalArgumentExceptionが」取得しています

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");
}
}

モデル:@Entityは、データベース内のテーブルを作成することはできませんが、接続が正常に動作しています

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 + "]";
}
}

インターフェース

package com.access6.dao;
import org.springframework.data.repository.CrudRepository;
import com.access6.model.Student;
public interface StudentInt extends CrudRepository<Student, Integer> {
}

春のブートアプリ

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
ランジット:

以下のコードを見つけてください。

@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

使用DTO自動的にセットリクエストパラメータにmodelAttribute、ここDTOからのパラメータ名や変数名を要求するには、同じである必要があります。

@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");
}

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=32213&siteId=1