2

I have a bidder class for a car auction and cannot seem to pass jtextfield values into the array when the user clicks the submit button. The bidder class is:

package abc;

import java.util.ArrayList;


public class Bidder extends User{
private String firstName, lastName, street, city, postcode, tel, eMail, cardMake, cardNo, expDate;
private int houseNo, csvNo;

public Bidder(){

}

public Bidder(String UN, String UP, int UT, String FN, String LN, int HN,
        String S, String C, String P, String T, String EM, String CM, String CN, String ED, int CSV ){
super (UN, UP, UT);

firstName = FN;
lastName = LN;
houseNo = HN;
street = S;
city = C;
postcode = P;
tel = T;
eMail = EM;
cardMake = CM;
cardNo = CN;
expDate = ED;
csvNo = CSV;
}
static ArrayList<Bidder> BidderArray = new ArrayList<Bidder>();


}

The values I am trying to pass to the array are as when a submit button is pressed are as follows:

 private void submitActionPerformed(java.awt.event.ActionEvent evt) {     

    boolean validEntries = true;


    String checkUserName = userName.getText();
    if (checkUserName.equals(""))
    {
        validEntries = false;
        userName.setBackground(Color.red);
    }
    String checkPassword = password.getText();
    if (checkPassword.equals(""))
    {
        validEntries = false;
        password.setBackground(Color.red);
    }
    try{
    int checkUserType = Integer.parseInt(userType.getText());
    }
    catch (Exception error)
    {
        validEntries = false;
        userType.setBackground(Color.red);
    }
    String checkFirstName = firstName.getText();
    if (checkFirstName.equals(""))
    {
        validEntries = false;
        firstName.setBackground(Color.red);
    }
    String checkLastName = lastName.getText();
    if (checkLastName.equals(""))
    {
        validEntries = false;
        lastName.setBackground(Color.red);
    }
    try
    {
        int checkHouseNo = Integer.parseInt(houseNo.getText());
    }
    catch (Exception error)
    {
        validEntries = false;
        houseNo.setBackground(Color.red);
    }
    String checkStreet = street.getText();
    if (checkStreet.equals(""))
    {
        validEntries = false;
        street.setBackground(Color.red);

    }
    String checkCity = city.getText();
    if (checkCity.equals(""))
    {
        validEntries = false;
        city.setBackground(Color.red);

    }
    String checkPostcode = postcode.getText();
    if (checkPostcode.equals(""))
    {
        validEntries = false;
        postcode.setBackground(Color.red);

    }
    String checkTel = tel.getText();
    if (checkTel.equals(""))
    {
        validEntries = false;
        tel.setBackground(Color.red);

    }
    String checkEMail = eMail.getText();
    if (checkEMail.equals(""))
    {
        validEntries = false;
        eMail.setBackground(Color.red);

    }
    String checkCardType = cardType.getText();
    if (checkCardType.equals(""))
    {
        validEntries = false;
        cardType.setBackground(Color.red);

    }
    String checkCardNo = cardNo.getText();
    if (checkCardNo.equals(""))
    {
        validEntries = false;
        cardNo.setBackground(Color.red);

    }
    String checkExpDate = expDate.getText();
    if (checkExpDate.equals(""))
    {
        validEntries = false;
        expDate.setBackground(Color.red);

    }
    try
    {
        int checkCSV = Integer.parseInt(csv.getText());
    }
    catch (Exception error)
    {
        validEntries = false;
        csv.setBackground(Color.red);
    }

    Bidder bidder2 = new Bidder(userName, password, userType, firstName, lastName, houseNo, 
            street, city, postcode, tel, eMail, cardType, cardNo, expDate, csv);
    Bidder.BidderArray.add(bidder2);

}                                 

Regards

Lee

3
  • Your code seems to be ok, maybe the issue is some uncaught exception or when reading the items from the array. Commented Mar 20, 2014 at 18:29
  • What happens when you run this? Is there an error? Are the values blank? If you debug, are the values of userName, password, etc. correct before you pass them to the constructor? Commented Mar 20, 2014 at 18:29
  • How is submitActionPerformed being called? Or is it being called? (As an ised, inside submitActionPerformed, shouldn't you be constructing and inserting the new Bidder object only when validEntries is true?) Commented Mar 20, 2014 at 18:30

2 Answers 2

2

You're passing the csv as the last argument when you should be passing checkCSV. I strongly recommend that you get rid of that humongous constructor and use setters instead. It will make for much more maintainable code and will go a long way toward avoiding these kinds of errors.

The class might then look something like this:

public class Bidder extends User{

    private String firstName, lastName, street, city, postcode, tel, eMail, cardMake, cardNo, expDate;
    private int houseNo, csvNo;

    public Bidder(){

    }

    public void setFirstName(String name) {
        this.firstName = name;
    }

    public void setLastPassword(String password) {
        this.lastName = password;
    }

    // etc. for all the fields.

    public void setCSV(int csv) {
        this.csvNo = csv;
    }

    // Setters for userName, userPassword, and userType go in class User

    static ArrayList<Bidder> BidderArray = new ArrayList<Bidder>();

}

and you would use it like this:

private void submitActionPerformed(java.awt.event.ActionEvent evt) {     

    boolean validEntries = true;
    String sVal;
    int iVal;
    Bidder bidder2 = new Bidder();

    sVal = userName.getText();
    if (sVal.equals(""))
    {
        validEntries = false;
        userName.setBackground(Color.red);
    } else {
        bidder2.setUserName(sVal);
    }
    sVal = password.getText();
    if (sVal.equals(""))
    {
        validEntries = false;
        password.setBackground(Color.red);
    } else {
        bidder2.setUserPassword(sVal);
    }

    // for an int value:
    try{
        iVal = Integer.parseInt(csv.getText());
        bidder2.setCSV(iVal);
    }
    catch (Exception error)
    {
        validEntries = false;
        userType.setBackground(Color.red);
    }

    // etc. for all the other fields

    if (validEntries) {
        Bidder.BidderArray.add(bidder2);
    }
}
Sign up to request clarification or add additional context in comments.

5 Comments

I have tried the checkCSV, this isn't working. what what your thinking behind this? Would I not have to use checkHouseNo as well?
@LeeChant - Yes. In fact, I notice that you seem to be using the text fields instead of the values for all the arguments (you're using userName instead of checkUserName, etc.) Fix all of them and it should compile. However, you really should switch to using setters. I'll update my answer to show the beginning of how this would work.
Will give this a try @TedHopp, intrigued to see how the setters would work too
@LeeChant - I added a setter and sample usage for csvNo to show the pattern. You just have to use int types instead of String.
Thanks @TedHopp, it caused a few problems elsewhere but I've managed to resolve them and it's working
2
idder bidder2 = new Bidder(userName, password, userType, firstName, lastName, houseNo, 
            street, city, postcode, tel, eMail, cardType, cardNo, expDate, csv);

Should be like ,check the last parameter,

idder bidder2 = new Bidder(userName, password, userType, firstName, lastName, houseNo, 
            street, city, postcode, tel, eMail, cardType, cardNo, expDate, checkCSV );

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.