黑马程序员技术交流社区
标题:
多对多关系,空指向异常???
[打印本页]
作者:
心?=忐§忑]
时间:
2014-4-17 08:53
标题:
多对多关系,空指向异常???
package 多对多关系;
import java.util.ArrayList;
import java.util.List;
public class Student {
private String name ;
private int age ;
private List<Count> allCount ;
public Student(){
this.allCount = new ArrayList<Count>() ;
}
public Student(String name , int age ){
this.name = name ;
this.age = age ;
}
public String getName(){
return name ;
}
public void setName(String name){
this.name = name ;
}
public int getAge(){
return age ;
}
public void setAge(int age){
this.age = age ;
}
public List<Count> getCount(){
return allCount ;
}
public void setSchool(List<Count> allCount){
this.allCount = allCount ;
}
public String toString(){
return "姓名:" + this.name + " 年龄:" + this.age ;
}
}
package 多对多关系;
import java.util.ArrayList;
import java.util.List;
public class Count {
private String name ;
private float credit ;
private List<Student> allStudent ; //一门课程有多个学生
public Count(){
this.allStudent = new ArrayList<Student>() ;
}
public Count(String name , float credit){
this() ;
this.name = name ;
this.credit = credit ;
}
public String getName(){
return name ;
}
public void setName(String name){
this.name = name ;
}
public float getCredit(){
return credit ;
}
public void setCredit(float credit){
this.credit = credit ;
}
public List<Student> getAllStudent(){
return allStudent ;
}
public void setAllStudent(List<Student> allStudent){
this.allStudent = allStudent ;
}
public String toString(){
return "课程名称:" + this.name + " 课程成绩:" + this.credit ;
}
}
package 多对多关系;
import java.util.Iterator;
import 多对多关系.Student;
import 多对多关系.Count;
public class TextDemo {
public static void main(String args[]){
Count c1 = new Count("java基础" , 30 ) ;
Count c2 = new Count("计算机基础" , 25 ) ;
Student stu1 = new Student("张三" , 21) ; //实例化学生类
Student stu2 = new Student("李四" , 22) ; //实例化学生类
Student stu3 = new Student("阿菜" , 20) ; //实例化学生类
c1.getAllStudent().add(stu1) ;
c1.getAllStudent().add(stu2) ;
c1.getAllStudent().add(stu3) ;
c2.getAllStudent().add(stu1) ;
c2.getAllStudent().add(stu2) ;
c2.getAllStudent().add(stu3) ;
stu1.getCount().add(c1) ;
stu1.getCount().add(c2) ;
stu2.getCount().add(c1) ;
stu2.getCount().add(c2) ;
stu3.getCount().add(c1) ;
stu3.getCount().add(c2) ;
System.out.println(c1);
Iterator<Student> iter = c1.getAllStudent().iterator() ;
while(iter.hasNext()){
Student s = iter.next() ;
System.out.println(s) ;
}
}
}
复制代码
运行时出错:Exception in thread "main" java.lang.NullPointerException
at 多对多关系.TextDemo.main(TextDemo.java:23)
作者:
呆呆沙师妹
时间:
2014-4-17 10:31
应该是Student类中有参数的构造函数引用了Object类中默认的构造器,导致的allCount = null空指针异常;
public Students(String name , int age ){
this();
this.name = name ;
this.age = age ;
}
复制代码
作者:
清风有意
时间:
2014-4-17 10:56
可以明确 你的问题出在student上面
public class Student {
private String name ;
private int age ;
private List<Count> allCount ;
public Student(){
//这句没有用到,所以在new Student(name,age)的时候,allCount指向null;
this.allCount = new ArrayList<Count>() ;
}
public Student(String name , int age ){
而在主函数中你直接getAllcount(),返回的是null;不能add
所以应该将空构造方法的 this.allCount= new ArrayList<Count>() ;放到这个构造方法中
this.name = name ;
this.age = age ;
this.allCount= new ArrayList<Count>() ;
}
public String getName(){
return name ;
}
public void setName(String name){
this.name = name ;
}
public int getAge(){
return age ;
}
public void setAge(int age){
this.age = age ;
}
public List<Count> getCount(){
return allCount ;
}
public void setSchool(List<Count> allCount){
this.allCount = allCount ;
}
public String toString(){
return "姓名:" + this.name + " 年龄:" + this.age ;
}
}
复制代码
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2