ArrayOutOfBound和null值

时间:2014-11-10 15:50:09

标签: java arraylist null indexoutofboundsexception

这部分让我很困惑。当我点击计算按钮两次时,我无法从memType获取值,因为它始终为null,而arrayIndexOutOfBounds。当我第二次点击时它也会清空列。有人可以帮我这个吗?已编码2天几乎不停。

用户界面:

 //Calculate
                else if(e.getSource() == jbtCalculate)
                 {  memControl = new MemberControl();
                    Member member = new Member();
                    JPanel confirmPanel = new JPanel();
                    JPanel cp1 = new JPanel();
                    JPanel cp2 = new JPanel();
                    int RowCount = table.getRowCount();
                    double discount = 0;
                    double transaction;
                    double discountPromo = 0;
                    double totaltrans = 0 ;
                    double totaltranswithDisc =0;
                    String pID = jTF3.getText();
                    ArrayList<Purchase> purchaseList = purcControl.selectRecord(pID);

                    String memTypeCheck = jTF4.getText();
                    memControl.selectRecord(memTypeCheck);
                    String memType = String.valueOf(member.getMemType());
                    System.out.println(memType);
                    if(memType.equals("Gold"))
                    {
                        discount = 0.8;
                    }
                    else if(memType.equals("Silver"))
                    {
                        discount = 0.9;
                    }
                    else if(memType.equals("Normal"))
                    {
                        discount = 1;
                    }
                    try{
                        tableModel.setColumnCount(4);
                      tableModel.addColumn("Total");
                    for (Purchase purchase : purchaseList) {
                      transaction = purchase.getQuantity()*purchase.getPrice();
                      tableModel.setValueAt(transaction,purchaseCount , 4);
                        purchaseCount += 1; 
                           totaltrans += transaction;
                    }
                     JOptionPane.showMessageDialog(null, "Discount on member :" +discount +"0\nDiscount on promotion :"+ discountPromo+"0\nCost before discount :"+totaltrans +"0\nTotal transaction:" + totaltranswithDisc+"0");

                    }
                    catch(ArrayIndexOutOfBoundsException a){
                        JOptionPane.showMessageDialog(null, "Index Out Of Bound");

                    }

控制:

    public Member selectRecord(String id) {
    return memDA.getRecord(id);
}

域名:

  public Member(MembershipType memType){
    this.memType = memType;

}
public Member(String memID,String memName, MembershipType memType, String IC, char gender, String address, String ContactNo, String eMail){
    this.memID = memID;
    this.memName = memName;
    this.memType = memType;
    this.IC = IC;
    this.gender = gender;
    this.address = address;
    this.ContactNo = ContactNo;
    this.eMail = eMail;


    counterID++;
}

public String getMemID(){
    return memID;
}
public MembershipType getMemType(){
    return memType;
}

public String getMemName(){
    return memName;
}

public String getIC(){
    return IC;
}

public char getGender(){
    return gender;
}
public String getAddress(){
    return address;
}

public String getContactNo(){
    return ContactNo;
}

public String getEMail(){
    return eMail;
}

public String getMemberID(){
    return memID;
}

public int getCounterID(){
    return counterID;
}
public double getTotalMemberFee(){
    return totalMemberFee;
}
public void setTotalMemberFee(double totalMemberFee){
    this.totalMemberFee = totalMemberFee;
}
public void setMemID(String memID){
    this.memID = memID;
}
public void setMemType(MembershipType memType){
    this.memType = memType;
}

public void setMemName(String memName){
    this.memName = memName;
}

public void setIC(String IC){
    this.IC = IC;
}

public void setGender(char gender){
    this.gender = gender;
}

public void setAddress(String address){
    this.address = address;
}

public void setContactNo(String ContactNo){
    this.ContactNo = ContactNo;
}

public void setEMail(String eMail){
    this.eMail = eMail;
}

public void setMemberID(String memberID){
    this.memID = memID;
}

public void setCounterID(int counterID){
    this.counterID = counterID;
}
}

DA:

public Member getRecord(String id) {
    String queryStr = "SELECT * FROM " + tableName + " WHERE memberID = ?";
    Member member = null;

    try {
        stmt = conn.prepareStatement(queryStr);
        stmt.setString(1, id);
        ResultSet rs = stmt.executeQuery();
        if (rs.next()) {
            MembershipType membershipType = memTypeDA.getRecord(rs.getString("memberType"));
            member = new Member(id,rs.getString("memberName"),membershipType,rs.getString("memberIC"),rs.getString("gender").charAt(0),rs.getString("memberAddress"), rs.getString("ContactNo"),rs.getString("email"));
        }
    } catch (SQLException ex) {
        JOptionPane.showMessageDialog(null, ex.getMessage(), "ERROR", JOptionPane.ERROR_MESSAGE);
    }
    return member;
}

编辑:这是我得到arrayOutOfBound错误的部分。 (来自UI)

 try{
                tableModel.setColumnCount(4);
              tableModel.addColumn("Total");
            for (Purchase purchase : purchaseList) {
              transaction = purchase.getQuantity()*purchase.getPrice();
              tableModel.setValueAt(transaction,purchaseCount , 4);
                purchaseCount += 1; 
                   totaltrans += transaction;
            }
             JOptionPane.showMessageDialog(null, "Discount on member :" +discount +"0\nDiscount on promotion :"+ discountPromo+"0\nCost before discount :"+totaltrans +"0\nTotal transaction:" + totaltranswithDisc+"0");

            }
            catch(ArrayIndexOutOfBoundsException a){
                JOptionPane.showMessageDialog(null, "Index Out Of Bound");

            }
            jTF5.setText(String.valueOf(totaltrans) + "0"); //EDIT
            jTF5.setEditable(false); //EDIT

空值已经解决。我添加了两个声明。

Member mem = memControl.selectRecord(memTypeCheck);
            String memType = mem.getMemType().getMemberType();

所以实际上我的错误就是空值。

1 个答案:

答案 0 :(得分:1)

首先,您要使用空构造函数创建新成员:

Member member = new Member();

所以我假设在这个对象中,memType为null。

然后你用:

创建一个String
String memType = String.valueOf(member.getMemType());

显然应该是null。