我的一个表叫score,里面的属性为course_no(这个是外键,我的course表中的一个项),student_no(这个也是外键,我的student表中的一个项)。我现在想往这个score表中插入数据:
  Course course = new Course();
course = courseService.findByCourseNo(courseNo);
Student student = new Student();
student = studentService.findByStudentId(studentId).get(0);
score.setCourse(course);
score.setStudent(student);
courseinfoService.add(score);
这是我在函数中写的,但是出现错我,说是空指针异常!
我把score.hbm.xml贴上<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--  
  Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
  <class name="grade.entity.Score" table="score">
  <id name="id" type="java.lang.Integer">
  <column name="id" />
  <generator class="native" />
  </id>
  <many-to-one name="student" class="baseInfo.entity.Student" fetch="select">
  <column name="student_no" not-null="true" />
  </many-to-one>
  <many-to-one name="course" class="baseInfo.entity.Course" fetch="select">
  <column name="course_no" not-null="true" />
  </many-to-one>
  <property name="credit" type="java.lang.Float">
  <column name="credit" precision="53" scale="0" />
  </property>
  <property name="studentScore" type="java.lang.Float">
  <column name="student_score" precision="53" scale="0" not-null="true" />
  </property>
  <property name="scoreflag" type="java.lang.Integer">
  <column name="scoreflag" not-null="true"/>
  </property>
  </class>
</hibernate-mapping>
package grade.entity;import baseInfo.entity.Course;
import baseInfo.entity.Student;/**
 * Score entity.
 *  
 * @author MyEclipse Persistence Tools
 */public class Score{// Fieldsprivate Integer id;
private Student student;
private Course course;
private Float credit;
private Float studentScore;
private int scoreflag;
// Property accessorspublic Integer getId() {
return this.id;
}public void setId(Integer id) {
this.id = id;
}public Student getStudent() {
return this.student;
}public void setStudent(Student student) {
this.student = student;
}public Course getCourse() {
return this.course;
}public void setCourse(Course course) {
this.course = course;
}public Float getCredit() {
return this.credit;
}public void setCredit(Float credit) {
this.credit = credit;
}public Float getStudentScore() {
return this.studentScore;
}public void setStudentScore(Float studentScore) {
this.studentScore = studentScore;
}public int getScoreflag() {
return this.scoreflag;
}public void setScoreflag(int scoreflag) {
this.scoreflag = scoreflag;
}}
这个是我写的action
package student.action;import grade.entity.Score;
import grade.service.CourseInfoService;import java.util.ArrayList;
import java.util.List;
import java.util.Map;import login.entity.Login;
import baseInfo.entity.Course;
import baseInfo.entity.Student;
import baseInfo.service.CollegeService;
import baseInfo.service.CourseService;
import baseInfo.service.StudentService;import com.opensymphony.xwork2.ActionContext;public class SelectCourseAction {private CourseInfoService courseinfoService;
private StudentService studentService;
private CollegeService collegeService;
private CourseService courseService;
private Student student;
private Score score;
private Course course;
private String courseNo;
private List<Score> listScore = new ArrayList<Score>();
private List<Course> listNoSelectedCourse = new ArrayList<Course>();
private List<Course> listCourse = new ArrayList<Course>();public Course getCourse() {
return course;
}
public void setCourse(Course course) {
this.course = course;
}
public String getCourseNo() {
return courseNo;
}
public void setCourseNo(String courseNo) {
this.courseNo = courseNo;
}
public CollegeService getCollegeService() {
return collegeService;
}
public void setCollegeService(CollegeService collegeService) {
this.collegeService = collegeService;
}
public CourseService getCourseService() {
return courseService;
}
public void setCourseService(CourseService courseService) {
this.courseService = courseService;
}
public List<Course> getListNoSelectedCourse() {
return listNoSelectedCourse;
}
public void setListNoSelectedCourse(List<Course> listNoSelectedCourse) {
this.listNoSelectedCourse = listNoSelectedCourse;
}
public List<Course> getListCourse() {
return listCourse;
}
public void setListCourse(List<Course> listCourse) {
this.listCourse = listCourse;
}
public CourseInfoService getCourseinfoService() {
return courseinfoService;
}
public void setCourseinfoService(CourseInfoService courseinfoService) {
this.courseinfoService = courseinfoService;
}
public StudentService getStudentService() {
return studentService;
}
public void setStudentService(StudentService studentService) {
this.studentService = studentService;
}
public Student getStudent() {
return student;
}
public void setStudent(Student student) {
this.student = student;
}
public Score getScore() {
return score;
}
public void setScore(Score score) {
this.score = score;
}
public List<Score> getListScore() {
return listScore;
}
public void setListScore(List<Score> listScore) {
this.listScore = listScore;
}
@SuppressWarnings("unchecked")
public String getLogin()

ActionContext context = ActionContext.getContext();
Map session = context.getSession();
Login role = (Login)session.get("role");
return role.getUsername().trim();
}
public String queryAll()
{
String studentId = getLogin();
// System.out.println("登录名:++++++++++++++"+studentId);
student = studentService.findByStudentId(studentId).get(0);
listScore = courseinfoService.findByStudentId(student.getId());//学生已经选的课程
//System.out.println("listScore==========="+listScore);
//listScore可以实现queryAll页面的上半部分的功能
//System.out.println("student.getCollege().getId()=========="+student.getCollege().getId());
listCourse = courseService.findByCollegeId(student.getCollege().getId());//获得学生所在的学院所有的课程
//System.out.println("listCourse==========="+listCourse);
//下面的for循环是找出这个学院的这个学生没有选的课程
listNoSelectedCourse.clear();
for (Course c : listCourse)

int temp = 0;
for (Score s : listScore)
{
if (c.getCourseNo().equals(s.getCourse().getCourseNo()))
temp++;
}
//System.out.println("temp++++++++++"+temp);
if (temp == 0)
listNoSelectedCourse.add(c);//
//System.out.println("c============="+c.getCyear());
}
//System.out.println("listNoSelectedCourse======"+listNoSelectedCourse.size());
return "queryAll";
}
public String addSelect()
{
//获取courseNo和studentId,可以找到相应的表中的id,然后写到score表中,作为选课的一条记录
String studentId = getLogin();
System.out.println("studentId======"+studentId);
System.out.println("courseNo========="+courseNo);
Course course = new Course();
course = courseService.findByCourseNo(courseNo);
System.out.println("course++++++++"+course);
Student student = new Student();
student = studentService.findByStudentId(studentId).get(0);
System.out.println("student++++++++"+student);
score.setCourse(course);
score.setStudent(student);
courseinfoService.add(score);
return "tzqueryAll";
}
}
这是我的course表
3 id int 4 0
0 course_no varchar 50 0
0 course_name varchar 50 0
0 college_name int 4 0
0 ctype char 10 0
0 score float 8 0
0 estyle char 10 0
0 cyear varchar 50 0这是我的score表
3 id int 4 0
0 course_no int 4 0
0 student_no int 4 0
0 credit float 8 1
0 student_score float 8 0
0 scoreflag int 4 0这是我的student表
3 id int 4 0
0 student_id varchar 20 0
0 student_name varchar 50 0
0 student_sex char 2 1
0 born_date datetime 8 1
0 collegeName int 4 0
0 majorName int 4 0
0 tele_num varchar 20 1
0 address varchar 200 1
0 className int 4 0
0 age int 4 1
0 oldschool varchar 100 1
0 [position] varchar 20 1
0 status int 4 1
0 comments varchar 400 1希望大家多多帮忙,谢谢了.
我的分数不太多,我自己留个零头,整数给你们,谢谢!

解决方案 »

  1.   

    严重: Servlet.service() for servlet default threw exception
    java.lang.NullPointerException
    at student.action.SelectCourseAction.addSelect(SelectCourseAction.java:148)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:404)
    at com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:267)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:229)
    at com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:123)
    at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224)
    at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223)
    at com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221)
    at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:167)
    at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:86)
    at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224)
    at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223)
    at com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221)
    at org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:83)
    at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224)
    at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223)
    at com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221)
    at com.opensymphony.xwork2.interceptor.PrepareInterceptor.doIntercept(PrepareInterceptor.java:121)
    at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:86)
    at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224)
    at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223)
    at com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221)
    at org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:170)
    at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224)
    at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223)
    at com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221)
    at com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:176)
    at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224)
    at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223)
    at com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221)
    at org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:50)
    at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:504)
    at org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:419)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.springframework.orm.hibernate3.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:174)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:263)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:584)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
    at java.lang.Thread.run(Unknown Source)
      

  2.   

    就是空指针异常,在插入外键的score.setCourse(course);
    Course course = new Course();
    course = courseService.findByCourseNo(courseNo);
      

  3.   

      你用的是spring控制吧,你看下你引用的对象是否在application-context.xml配置文件中是否配置了你使用的类吗?例如:就像你action中那些生成了get和set方法的那些对象。
    如果不是用的像studentService这些对象是需要new出来的。另外类中定义studentService这些对象最好面向接口,这样才符合规范。
      

  4.   

    我是用的spring控制。我引用的类都有,我测试了,
    System.out.println("studentId======"+studentId);
    System.out.println("courseNo========="+courseNo);
    Course course = new Course();
    course = courseService.findByCourseNo(courseNo);
    System.out.println("course++++++++"+course);
    Student student = new Student();
    student = studentService.findByStudentId(studentId).get(0);
    System.out.println("student++++++++"+student);
    打印出来都能获得值。
      

  5.   

    就是用score.setCourse(course);出错!这个setCourse中的参数就是这个类的一个实例啊!为什么会出现空指针异常啊?
      

  6.   

    已经解决,我在debug模式下单步运行,发现需要:Score score = new Score();
    谢谢大家的关注!
      

  7.   

    这些问题,一般通过debug都可以解决