Reputation: 103
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
Upvotes: 2
Views: 99
Reputation: 234795
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);
}
}
Upvotes: 2
Reputation: 1874
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 );
Upvotes: 2