空指针异常怎么处理
public abstract class Property {
protected int HP; //血量
protected int MP; //蓝量
protected int ATK; //攻击力
protected int DEF; //防御力
protected int range; //射程
}
public abstract class Hero {
protected Property p; //属性
protected Wallet w; //钱包
protected Weapon t; //武器
protected Clothes c; //衣服
protected Jewelry j; //首饰
public Hero(){
}
public Hero(int HP,int MP,int ATK,int DEF,int range) {
p.HP=HP;
p.MP=MP;
p.ATK=ATK;
p.DEF=DEF;
p.range=range;
}
}
public class Rabbi extends Hero{
public Rabbi() {
super(4000,2500,200,80,550); //设置法师初始属性
}
public void info() {
File file1 = new File("D:\\法师基础属性.txt");
BufferedWriter bw = null;
try {
FileWriter fw = new FileWriter(file1);
bw = new BufferedWriter(fw);
bw.write("法师:远程英雄,伤害较高,持续输出能力强,一般充当队伍输出角色,"
+ "缺点是血量较低,技能冷却时间较长。" +"\r\n"
+ "法师的初始属性为:" + "\r\n"
+ "血量为:"+ p.HP + "\r\n"
+ "蓝量为:" + p.MP + "\r\n"
+ "攻击力为:" + p.ATK + "\r\n"
+ "防御力为:" + p.DEF + "\r\n"
+ "射程为:" + p.range);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (bw != null) {
try {
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
运行info方法时出现空指针异常了,为什么会这样,该怎么解决,求大神帮帮忙,谢谢了!