返回信息流代码如下:
public class Student
{
private String name;
private String sex;
private int age;
protected Date hireDate;
protected class Date
{
protected int year;
protected int month;
protected int day;
protected Date(int hY,int hM,int hD)
{
this.year = hY;
this.month = hM;
this.day = hD;
}
}
public Student(String name,int hY,int hM,int hD)
{
this.name = name;
hireDate.year=hY;
hireDate.month=hM;
hireDate.day=hD;
}
public Student(String name,String sex, int hY, int hM, int hD)
{
this(name,hY,hM,hD);
this.sex = sex;
}
public Student(String name,String sex,int age,int hY,int hM,int hD)
{
this(name,sex,hY,hM,hD);
this.age = age;
}
public void getInformation()
{
System.out.println("姓名: "+this.name+" 性别: "+this.sex+" 年龄:"+this.age+" 入学日期:"+hireDate.year+" "+hireDate.month+" "+hireDate.day);
}
public static void main(String args[])
{
Student student = new Student("张三","男",21,2008,5,6);
student.getInformation();
}
}
-------------------------------------
编译正确,但是运行时提示Exception in thread "main" java.lang.NullPointerException,
请问怎么解决?
这是一条镜像帖。来源:北邮人论坛 / java / #18536同步于 2011/5/28
该镜像源已超过 30 天没有更新,可能在源站已被删除。
Java机器人发帖
初学JAVA,小程序编译通过,但不能运行
yangrenjie
2011/5/28镜像同步8 回复
订阅后,新回复会通过你的通知中心匿名送达。
8 条回复
【 在 kearnel 的大作中提到: 】
: hireDate没有初始化
: --
谢谢,但是hireDate的初始化写在Student的第一个构造方法里了。main方法里初始化Student的时候应该也对hireDate赋值了吧?我刚才又在main方法里加了一句
Student.Date hireDate = student.new Date(2008,5,6);
但是运行的报错还是一样。。
public Student(String name,int hY,int hM,int hD)
{
this.name = name;
hireDate.year=hY;
hireDate.month=hM;
hireDate.day=hD;
}
改成
public Student(String name,int hY,int hM,int hD)
{
hireDate = new Date(hY, hM, hD);
this.name = name;
}
【 在 yangrenjie 的大作中提到: 】
: : hireDate没有初始化
: : --
: 谢谢,但是hireDate的初始化写在Student的第一个构造方法里了。main方法里初始化Student的时候应该也对hireDate赋值了吧?我刚才又在main方法里加了一句
: ...................
从C++转过来的?任何类对象使用前都得new。
【 在 wolf521 的大作中提到: 】
: public Student(String name,int hY,int hM,int hD)
: {
: this.name = name;
: ...................
谢谢了!解决了
【 在 kearnel 的大作中提到: 】
: : : hireDate没有初始化
: : : --
: : 谢谢,但是hireDate的初始化写在Student的第一个构造方法里了。main方法里初始化Student的时候应该也对hireDate赋值了吧?我刚才又在main方法里加了一句
: ...................
也就是对象在初始化之前,不能调用任何方法,使用任何成员变量对吧?
package com.lee;
public class Student {
private String name;
private String sex;
private int age;
protected Date hireDate;
{
hireDate = new Date();
}
protected class Date {
protected int year;
protected int month;
protected int day;
Date(){}
protected Date(int hY,int hM,int hD)
{
this.year = hY;
this.month = hM;
this.day = hD;
} }
public Student(String name,int hY,int hM,int hD)
{
this.name = name;
hireDate.year=hY;
hireDate.month=hM;
hireDate.day=hD;
}
public Student(String name,String sex, int hY, int hM, int hD)
{
this(name,hY,hM,hD);
this.sex = sex;
}
public Student(String name,String sex,int age,int hY,int hM,int hD)
{
this(name,sex,hY,hM,hD);
this.age = age;
}
public void getInformation() {
System.out.println("姓名: " + this.name + " 性别: " + this.sex + " 年龄:"
+ this.age + " 入学日期:" + hireDate.year + " " + hireDate.month
+ " " + hireDate.day);
}
public static void main(String []args)
{
Student student = new Student("张三","男",21,2008,5,6);
student.getInformation();
}}
static除外。
【 在 yangrenjie 的大作中提到: 】
: : : : hireDate没有初始化
: : : : --
: : : 谢谢,但是hireDate的初始化写在Student的第一个构造方法里了。main方法里初始化Student的时候应该也对hireDate赋值了吧?我刚才又在main方法里加了一句
: ...................