我正在尝试使用cid
在学生班级中设置值SubjectMark->private String cid;
如何在我的控制器中设置和获取价值。
实体和控制器的方法如下:
@Entity
public class Student implements Serializable {
private static final long serialVersionUID = 1L;
@EmbeddedId
private SubjectMark id;
private String fullName;
private Integer totalMarks;
private Double percentage;
private String grade;
//Setters and getters
}
//Composit class
@Embeddable
public class SubjectMark implements Serializable {
//Composit key
private String cid;
//Setters and getters
}
在我的控制器中,我尝试像这样设置值:
@RequestMapping(value="getstdata",method=RequestMethod.GET)
@ResponseBody
public String getstdata(@RequestParam(value="cid")String cid){
//Some code
try{
Student st=new Student();
st.getId().setCid(cid);//Set value like this but it is getting null pointer exception
//some code
//retuen some value
}
请帮助我!
答案 0 :(得分:0)
我想您需要在您的类中编写一个get set方法。 尝试写类似的东西
私有字符串cid;
public String Cid {get => cid;设置=> CID =值; }
答案 1 :(得分:0)
问题的第一部分:
我正在尝试使用SubjectMark-> private String cid在学生课堂中设置值cid;如何在我的控制器中设置和获取价值。
@Entity
@Table
public class Student implements Serializable {
private static final long serialVersionUID = 1L;
@EmbeddedId
private SubjectMark subjectMarkId;
private String otherField;
// setters, getters
}
//Composite class
@Embeddable
public class SubjectMark implements Serializable {
private String cId;
// setter, getter
}
//Controller
@GetMapping(value = "getstdata")
public String getStData(@RequestParam(value="cid") String cid) {
Student student = new Student();
student.setSubjectMark(new SubjectMark());
student.getSubjectMark().setCId(cid);//cid value dynamic
// some other code
return "";
}
问题的第二部分:
现在,当您尝试从null对象调用方法(setter或getter)时,发生null异常的原因之一。