-2

This parts is really making me confused. I can't get values from memType as it is always in null and the arrayIndexOutOfBounds when I click on the calculate button twice. It also empty the column when i clicked the second time. Anybody can help me with this please ? Have been coding for 2 days almost non-stop.

UI:

 //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");

                    }

Control :

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

Domain :

  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;
}

Edit: This is the part where I got my error of arrayOutOfBound. (from 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

Null value has been solved. I added two statement.

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

So actually is my silly mistake for null value.

user3900009
  • 113
  • 2
  • 4
  • 12
  • Where is the error happening point to specific line(s) in your code...it will help us help you – brso05 Nov 10 '14 at 15:52
  • Okay,wait I will do the editing – user3900009 Nov 10 '14 at 15:56
  • What is the tableModel variable do you have a class for that? – brso05 Nov 10 '14 at 16:08
  • String[] columnNames = { "Purchase Details ID", "Stock ID", "Price per unit", "Quantity"}; Object[][] dataValues = {}; public DefaultTableModel tableModel = new DefaultTableModel(dataValues, columnNames); – user3900009 Nov 10 '14 at 16:09
  • I am guessing the error is in the line `tableModel.setValueAt(transaction, purchaseCount, 4);` is that correct? – brso05 Nov 10 '14 at 16:11
  • Yeah,thats right. btw sorry for that copy and paste without making it like yours, I'm still learning how to use this forum. – user3900009 Nov 10 '14 at 16:14
  • It's alright you just use ` around your code to do code formatting in comments – brso05 Nov 10 '14 at 16:15
  • 1
    The problem probably is that you are trying to access a row that has not been created. `purchaseCount` in that line is probably larger than the size of the array holding the values. It's hard for me to really know the cause without seeing the DefaultTableModel class. What do you initialize `purchaseCount` to? – brso05 Nov 10 '14 at 16:17
  • Also I notice you initialize `tableModel` with an empty array `{}` unless you are adding elements to `tableModel` somewhere else in code that I can't see then your `tableModel` will not have any elements so when you try to access an element at `purchaseCount` it will not exist... – brso05 Nov 10 '14 at 16:19
  • The way you are using it the size of `purchaseList` and `tableModel` should be the same or you will get indexOutOfBounds exception if `purchaseList` is larger than `tableModel`. – brso05 Nov 10 '14 at 16:20
  • I initialized it so that it will keep going from the first to the second one. That's why. – user3900009 Nov 10 '14 at 16:21
  • I initialized purchaseCount so that I can have the row value automatically. But it seems that it caused the error. – user3900009 Nov 10 '14 at 16:25
  • yeah, I just initialize purchaseCount to 0 again after the for loop so that I can just do multiple times of calculating without error. – user3900009 Nov 10 '14 at 16:30
  • Cool glad you figured it out... – brso05 Nov 10 '14 at 16:30

1 Answers1

1

Firstly you're creating a new Member with an empty constructor :

Member member = new Member();

So I assume within this object, memType is null.

Then you're creating a String with :

String memType = String.valueOf(member.getMemType());

Which obviously is supposed to be null.

NSimon
  • 5,212
  • 2
  • 22
  • 36