日期:2014-05-20  浏览次数:20725 次

关于空指向异常错误,大家帮忙解决。。
Java code
package org.ts.demo;

class Emp {
    private int empno;
    private String ename;
    private String job;
    private double sal;
    private double comm;
    private Emp mgr;
    private Dept dept;

    public Emp(int empno, String ename, String job, double sal, double comm) {
        this.empno = empno;
        this.ename = ename;
        this.job = job;
        this.sal = sal;
        this.comm = comm;
    }

    public void setMgr(Emp mgr) {
        this.mgr = mgr;
    }

    public Emp getMgr() {
        return this.mgr;
    }

    public void setDept(Dept dept) {
        this.dept = dept;
    }

    public Dept getDept() {
        return this.dept;
    }

    public String getEmpInfo() {
        return "雇员信息:" + "\n\t|- 编号:" + this.empno + "\n\t|- 姓名:" + this.ename
                + "\n\t|- 职务:" + this.job + "\n\t|- 工资:" + this.sal + "\n\t|- "
                + SalGrade.getInstance(this.sal).getSalGradeInfo()  // 注释本行运行没有问题,但有这行却出现错误
                + "\n\t|- 奖金:" + this.comm;
    }
}

class Dept {
    private int deptno;
    private String dname;
    private String loc;
    private Emp[] emps;

    public Dept(int deptno, String dname, String loc) {
        this.deptno = deptno;
        this.dname = dname;
        this.loc = loc;
    }

    public void setEmps(Emp[] emps) {
        this.emps = emps;
    }

    public Emp[] getEmps() {
        return this.emps;
    }

    public String getDeptInfo() {
        return "部门信息:" + "\n\t|- 编号:" + this.deptno + "\n\t|- 名称:" + this.dname
                + "\n\t|- 位置:" + this.loc;
    }
}

class SalGrade {
    private int grade;
    private double losal;
    private double hisal;
    private static final SalGrade salgrades[] = {
            new SalGrade(1, 700.0, 1200.0), new SalGrade(2, 1201.0, 1400.0),
            new SalGrade(3, 1401.0, 2000.0), new SalGrade(4, 2001.0, 3000.0),
            new SalGrade(5, 3001.0, 9999.0) };

    private SalGrade(int grade, double losal, double hisal) {
        this.grade = grade;
        this.losal = losal;
        this.hisal = hisal;
    }

    public static SalGrade getInstance(double sal) {
        for (int i = 0; i < salgrades.length; i++) {
            if (sal > salgrades[i].losal && sal < salgrades[i].hisal) {
                return salgrades[i];
            }
        }
        return null;
    }

    public String getSalGradeInfo() {
        return "工资等级:" + this.grade + "(" + this.losal + "~" + this.hisal + ")";
    }
}

public class OODemo {
    public static void main(String args[]) {
        Dept dept = new Dept(10, "销售部", "上海");
        Emp[] allEmp = { new Emp(1122, "张三", "干事", 1500.0, 50.0),
                new Emp(2233, "李四", "销售", 2000.0, 100.0),
                new Emp(3344, "王五", "经理", 3000.0, 100.0),
                new Emp(4455, "赵六", "董事", 5000.0, 0.0) };
        dept.setEmps(allEmp);
        for (int i = 0; i < allEmp.length - 1; i++) {
            allEmp[i].setMgr(allEmp[i + 1]);
        }
        for (int i = 0; i < allEmp.length; i++) {
            allEmp[i].setDept(dept);
        }
        System.out.println(dept.getDeptInfo());
        System.out.println("部门人数:" + dept.getEmps().length + "人");
        System.out.println("-------------------------");
        for (int i = 0; i < dept.getEmps().length; i++) {
            Emp emp = dept.getEmps()[i];
            System.out.println(emp.getEmpInfo());
            if (emp.getMgr() != null) {
                System.out.println(emp.getMgr().getEmpInfo());
            }
            System.out.println("-------------------------");
        }
    }
}

小弟不才,初学java,遇到些问题,大家帮忙看看,感谢!!

------解决方案--------------------