为什么我在这个函数上获得空指针异常

时间:2016-09-12 05:10:59

标签: java nullpointerexception

我在此特定函数上遇到空指针异常问题

  public String sqlQuery(){
        String sql;
        switch(employeeCode){
        case "A":
            sql="SELECT * FROM (SELECT * FROM SalesCodeA UNION ALL SELECT * FROM SalesCodeB UNION ALL SELECT * FROM SalesCodeC ) t WHERE t.EmployeeCode='A'";
            break;
        case "B":
            sql="SELECT * FROM (SELECT * FROM SalesCodeA UNION ALL SELECT * FROM SalesCodeB UNION ALL SELECT * FROM SalesCodeC ) t WHERE t.EmployeeCode='B'";
            break;
        case "C":
            sql="SELECT * FROM (SELECT * FROM SalesCodeA UNION ALL SELECT * FROM SalesCodeB UNION ALL SELECT * FROM SalesCodeC ) t WHERE t.EmployeeCode='C'";
            break;
        default:
            sql=null;
        }

        return sql;
    }
开关(employeeCode)

我得到一个空指针异常。 但是employeeCode在我的其他函数上不为null,employeeCode在我的sqlInsert()函数上工作正常

      public String sqlInsert(){
    if(employeeCode.equals("A") && employeeSales > 2500){
        return "SalesCodeA";
    }else if(employeeCode.equals("B") && employeeSales > 2000){
        return "SalesCodeB";
    }else if(employeeCode.equals("C") && employeeSales > 1500){
        return "SalesCodeC";
    } return null;

}   

我可以在我的数据库上输入数据,但我无法检索它们以进行显示,因为它在我的sqlQuery()函数上触发了NPE。

这是我班上的完整代码:

 public class EmployeeBean implements EmployeeInterface {

    private String employeeName;
    private String employeeCode;
    private double employeeSales;
    private double result;
    private double commission;
    private double gross;


 public String getEmployeeName() {
    return employeeName;
}
 public void setEmployeeName(String employeeName) {
    this.employeeName = employeeName;
}

 public String getEmployeeCode() {
    return employeeCode;
}
 public void setEmployeeCode(String employeeCode) {
    if(isValidSalesCode(employeeCode)){
        this.employeeCode=employeeCode;
    }


}
 public double getEmployeeSales() {
    return employeeSales;
}
 public void setEmployeeSales(double employeeSales) {
    if(isValidSalesAmount(employeeSales)){
    this.employeeSales = employeeSales;
    }
}
 public double getResult() {
    return result;
}
 public void setResult(double result) {
    this.result = result;
}

 public double getCommission() {
    return commission;
}
 public void setCommission(double commission) {
    this.commission = commission;
}

 public double getGross() {
    return gross;
}
 public void setGross(double gross) {
    this.gross = gross;
}

 public void computeGross(){

    switch(employeeCode){
     case "A":
        setGross(grossA+(getEmployeeSales()*grossSalesA));
        break;
     case "B":
        setGross(grossB+(getEmployeeSales()*grossSalesB));  
        break;
     case "C":
        setGross(grossC+(getEmployeeSales()*grossSalesC));  
        break;
    }   

}

//compute for the commission, if sales>2500; commission=7.5% of sales
 public void computeCommission(){       

     if(employeeSales>2500)
         setCommission(getEmployeeSales()*commissionVal);
     else {
        setCommission(0);
    }



}

//compute takehome pay, takehome pay = summation of gross and commission

 public void computeTakeHomePay(){

    setResult(getGross()+getCommission());  

}

//check if code is valid

public boolean isValidSalesCode  (String salesCode){

    boolean isValid;

        if(salesCode.equals("A")||salesCode.equals("B")||salesCode.equals("C")){
        isValid=true;
    }
    else isValid=false;

    return isValid; 




}


//check if salesAmount is valid

   public boolean isValidSalesAmount(double salesAmount){
      boolean isValid;

       if(salesAmount>0){
          isValid=true;
      }
       else{
          isValid=false;
      }
     return isValid;
 }

public void SetEmployeeRecord(Connection conn){
    if(isValidSalesCode(employeeCode)&&isValidSalesAmount(employeeSales)){
    getResult();
    getCommission();
    getGross();

    if(isValidEntry(employeeCode,employeeSales)){
        insertRecord(conn);
        }
    }

}

public boolean isValidEntry(String code, double sales){
    boolean isValid;

        if(code.equals("A")&&sales>2500){
            isValid=true;
        }
        else if(code.equals("B")&&sales>2000){
            isValid=true;
        }
        else if(code.equals("C")&&sales>1500){
            isValid=true;
        }else
            isValid=false;

        return isValid;

}




public String sqlInsert(){
    if(employeeCode.equals("A") && employeeSales > 2500){
        return "SalesCodeA";
    }else if(employeeCode.equals("B") && employeeSales > 2000){
        return "SalesCodeB";
    }else if(employeeCode.equals("C") && employeeSales > 1500){
        return "SalesCodeC";
    } return null;

}       



    private void insertRecord(Connection conn){

    try{


        PreparedStatement pstmnt= conn.prepareStatement("insert into "+sqlInsert()+"(EmployeeName, EmployeeCode, EmployeeSales, EmployeeGross, EmployeeCommission, EmployeeResult)"+ "values(?,?,?,?,?,?)");
        pstmnt.setString(1, employeeName);
        pstmnt.setString(2, employeeCode);
        pstmnt.setDouble(3, employeeSales);
        pstmnt.setDouble(4, gross);
        pstmnt.setDouble(5, commission);
        pstmnt.setDouble(6, result);

        //now commit to database
        pstmnt.executeUpdate();

    }catch(SQLException sqle){
        sqle.printStackTrace();
    }

}


// get records


    private String sqlQuery(){
        String sql;
        switch(employeeCode){
        case "A":
            sql="SELECT * FROM (SELECT * FROM SalesCodeA UNION ALL SELECT * FROM SalesCodeB UNION ALL SELECT * FROM SalesCodeC ) t WHERE t.EmployeeCode='A'";
            break;
        case "B":
            sql="SELECT * FROM (SELECT * FROM SalesCodeA UNION ALL SELECT * FROM SalesCodeB UNION ALL SELECT * FROM SalesCodeC ) t WHERE t.EmployeeCode='B'";
            break;
        case "C":
            sql="SELECT * FROM (SELECT * FROM SalesCodeA UNION ALL SELECT * FROM SalesCodeB UNION ALL SELECT * FROM SalesCodeC ) t WHERE t.EmployeeCode='C'";
            break;
        default:
            sql=null;
        }

        return sql;
    }



public ResultSet getAllRecords(Connection conn){

    ResultSet records = null;

    try{

        PreparedStatement pstmnt= conn.prepareStatement(sqlQuery());    


        records= pstmnt.executeQuery();         

    }catch(SQLException sqle){
        sqle.printStackTrace();
    }
        return records;

}

这是我调用GetAllRecords函数的地方

 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    if(connection != null){

        ResultSet records= new EmployeeBean().getAllRecords(connection);

        request.setAttribute("employeeRecords", records);

        request.getRequestDispatcher("viewEmployeeHistory.jsp").forward(request, response);


    }else {
        System.err.println("Connection is NULL");
    }

2 个答案:

答案 0 :(得分:0)

我看不到你在哪里调用 setEmployeeCode()你应该做的事情

EmployeeBean employeeBeen = new EmployeeBean();
employeeBeen.setEmployeeCode("yourEmployeeCode");
ResultSet records = employeeBeen.getAllRecords(connection);

更新: 试试这个:

EmployeeBean employeeBeen = new EmployeeBean();
void yourSetterGetter(){
     employeeBeen.setEmployeeCode("yourEmployeeCode");
}
void doPost(){
     ResultSet records = employeeBeen.getAllRecords(connection);
}

答案 1 :(得分:0)

您的代码中缺少

setEmployeeCode()

EmployeeBean employee = new EmployeeBean(); employee.setEmployeeCode("yourEmployeeCode");

首先将EmployeeCode设置为bean,然后调用getAllRecords()方法。