ArrayList<Class> is returning null
I am having some issues once again with programming. I want to learn this stuff and have read and reread the materials but I am misfiring somewhere. Here is the code that I have written so far:
Code java:
public class Week01 {
ArrayList<Customer> mycustomer;
final ArrayList<Employee> myemployee = new ArrayList<Employee>();
private JFrame frame;
private JTextField tfgiven;
private JTextField tflast;
private JTextField tfpersonID;
private JTextField tfphone;
private JTextField tfaddress;
private JTextField tfcity;
private JTextField tfstate;
private JTextField tfzip;
private JTextField tfzip4;
private JButton btnAddCustomer;
private JButton btnAddEmployee;
private JButton btnDisplayAll;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Week01 window = new Week01();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public Week01() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
There is a bunch of code in here....
btnAddCustomer.addActionListener(new CustomerListener());
btnAddEmployee.addActionListener(new EmployeeListener());
btnDisplayAll.addActionListener(new DisplayAllListener());
tfgiven.addActionListener(null);
tflast.addActionListener(null);
tfpersonID.addActionListener(null);
tfaddress.addActionListener(null);
tfcity.addActionListener(null);
tfstate.addActionListener(null);
tfzip.addActionListener(null);
tfzip4.addActionListener(null);
}
private class CustomerListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
if(e.getSource() == btnAddCustomer) {
String givenName = tfgiven.getText();
tfgiven.setText("");
String surname = tflast.getText();
tflast.setText("");
String customerID = tfpersonID.getText();
tfpersonID.setText("");
if(customerID.trim().equals("")) {
JOptionPane.showMessageDialog(null, "Customer ID required.");
} else {
Integer.parseInt(customerID);
String phoneNumber = tfphone.getText();
tfphone.setText("");
String streetAddress = tfaddress.getText();
tfaddress.setText("");
String city = tfcity.getText();
tfcity.setText("");
String state = tfstate.getText();
tfstate.setText(state);
String zip = tfzip.getText();
tfzip.setText("");
if(zip.trim().equals("")) {
return;
} else {
Integer.parseInt(zip);
}
String zipPlus4 = tfzip4.getText();
tfzip4.setText("");
if(zipPlus4.trim().equals("")) {
return;
} else {
Integer.parseInt(zipPlus4);
}
}
return;
}
}
}
private class EmployeeListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
if(e.getSource() == btnAddEmployee) {
String givenName = tfgiven.getText();
String surname = tflast.getText();
String employeeID = tfpersonID.getText();
if(employeeID.trim().equals("")) {
JOptionPane.showMessageDialog(null, "Employee ID required.");
} else {
Integer.parseInt(employeeID);
String phoneNumber = tfphone.getText();
String streetAddress = tfaddress.getText();
String city = tfcity.getText();
String state = tfstate.getText();
String zip = tfzip.getText();
if(zip.trim().equals("")) {
} else {
Integer.parseInt(zip);
}
String zipPlus4 = tfzip4.getText();
if(zipPlus4.trim().equals("")) {
} else {
Integer.parseInt(zipPlus4);
}
Employee employees = new Employee(null, null, false, 0);
myemployee.add(employees);
}
}
}
}
private class DisplayAllListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
if(e.getSource() == btnDisplayAll) {
for(int i = 0; i < mycustomer.size(); i++) {
mycustomer.get(i).getGivenName();
mycustomer.get(i).getsurname();
mycustomer.get(i).getCustomerID();
mycustomer.get(i).getPhoneNumber();
mycustomer.get(i).getStreetAddress();
mycustomer.get(i).getState();
mycustomer.get(i).getZip();
mycustomer.get(i).getZipPlus4();
}
ArrayList<ArrayList<Customer>> mycustomer = new ArrayList<ArrayList<Customer>>();
mycustomer.add(new ArrayList<Customer>());
mycustomer.get(0).add(null);
for(int j = 0; j < myemployee.size(); j++) {
myemployee.get(j).getGivenName();
myemployee.get(j).getsurname();
myemployee.get(j).getEmployeeID();
myemployee.get(j).getPhoneNumber();
myemployee.get(j).getStreetAddress();
myemployee.get(j).getCity();
myemployee.get(j).getState();
myemployee.get(j).getZip();
myemployee.get(j).getZipPlus4();
}
myemployee.toArray();
}
System.out.println("Customers: " + "\n" +
mycustomer + "\nEmployees: " +
"\n" + myemployee);
}
}
}
So I have a Person class that holds the first and last name, phone number, address, city, state, zip; I have a Customer class (first name, last name, customer ID) and an Employee class (first name, last name, employee ID). When I press the ADD CUSTOMER button and then the DISPLAY ALL button I am not getting what I input in the textfields. Yes I am new to this, yes I ask stupid questions, no I am not understanding all this.
Re: ArrayList<Class> is returning null
One problem I see is that the myCustomer variable is defined and given a value inside of a method.
When the method exits, that variable and its values are gone.
Re: ArrayList<Class> is returning null
ok, so I should have done the ArrayList like:
Code java:
ArrayList<Customer> mycustomer = new ArrayList<Customer>();
Re: ArrayList<Class> is returning null
If you want different methods in the class to be able to get the data in the arraylist, you should define it at the class level and assign it a value.
You should NOT define a variable in a method with the same name as a class variable. That shadows the class variable/ hides it in the method.
Re: ArrayList<Class> is returning null
hmm....ok I am following a little. I want the Customer class which extends the Person class in the ArrayList which will allow me to pull all the information I need into the arraylist.
Code java:
public class Week01 {
ArrayList<Customer> mycustomer = new ArrayList<Customer>();
ArrayList<Employee> myemployee = new ArrayList<Employee>();
will this allow me to get the data i need in the CustomerListener method?
Re: ArrayList<Class> is returning null
Yes, Making the arraylists class members (and not shadowing them in methods) will allow all the methods in the class to access them to add and get data.
Re: ArrayList<Class> is returning null
ok, so I have done that. In my CustomerListener method, am I correct in creating:
Code java:
private class CustomerListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
if(e.getSource() == btnAddCustomer) {
String givenName = tfgiven.getText();
tfgiven.setText("");
String surname = tflast.getText();
tflast.setText("");
String customerID = tfpersonID.getText();
tfpersonID.setText("");
if(customerID.trim().equals("")) {
JOptionPane.showMessageDialog(null, "Customer ID required.");
} else {
Integer.parseInt(customerID);
String phoneNumber = tfphone.getText();
tfphone.setText("");
String streetAddress = tfaddress.getText();
tfaddress.setText("");
String city = tfcity.getText();
tfcity.setText("");
String state = tfstate.getText();
tfstate.setText("");
String zip = tfzip.getText();
tfzip.setText("");
if(zip.trim().equals("")) {
return;
} else {
Integer.parseInt(zip);
}
String zipPlus4 = tfzip4.getText();
tfzip4.setText("");
if(zipPlus4.trim().equals("")) {
return;
} else {
Integer.parseInt(zipPlus4);
}
the customerID, zip and zipPlus4 are all ints, if I create the Strings again in this method, they are not being used. I am stuck at this point, I want to get the Text from the textfield and store it in the arraylist mycustomers and then when i am ready to list them all out, I press the Display All button, but I keep getting null.
Re: ArrayList<Class> is returning null
Where does the null value come from? What variable is getting set to null? Where do you originally have the data? What is the path of the data from its source to where you want to get it for display but are getting null instead?
Can you post a small complete program that compiles, executes and shows the problem?
Re: ArrayList<Class> is returning null
Here is what my program is doing:
Code java:
Customers:
[null null
unknown
unknown
unknown, unknown, 99999-9999]
it should come out with givenName surname, customerID, phoneNumber, streetAddress, city, state, zip-zipPlus4 from the Customer class
Code java:
public class Customer extends Person {
private int customerID;
double creditLimit;
double balanceOwed;
public Customer(String givenName, String surname,
int customerID) {
super(givenName, surname);
this.customerID = customerID;
this.balanceOwed = 0.00;
The person class
Code java:
public class Person {
private String givenName;
private String surname;
private String phoneNumber;
private String streetAddress;
private String city;
private String state;
private int zip;
private int zipPlus4;
public Person (String givenName, String surname) {
this.givenName = givenName;
this.surname = surname;
this.phoneNumber = "unknown";
this.streetAddress = "unknown";
this.city = "unknown";
this.state = "unknown";
this.zip = 99999;
this.zipPlus4 = 9999;
}
public Person(String givenName, String surname,
String phoneNumber, String streetAddress,
String city, String state, int zip,
int zipPlus4) {
}
public String toString() {
return givenName + " " + surname + "\n" + phoneNumber + "\n" +
streetAddress + "\n" + city + ", " + state + ", " +
zip + "-" + zipPlus4;
}
There is more code at the end of each class getters and setters. so if nothing is put into the address field down to zipPlus4, the results should show unknown.
Re: ArrayList<Class> is returning null
You need to track the data from where it enters the program to where you are getting null values instead of the data.
Where does the "[null null" come from? With the leading [
Re: ArrayList<Class> is returning null
hmm....ok. From the Person class I have a toString()
Code java:
public String toString() {
return givenName + " " + surname + "\n" + phoneNumber + "\n" +
streetAddress + "\n" + city + ", " + state + ", " +
zip + "-" + zipPlus4;
}
and then in my Week01 GUI, I don't think that I am calling it correctly. I have I have a feeling though I should be calling the toString(), is that right?
Re: ArrayList<Class> is returning null
Where did the [ on the second line before the null come from?
What does the toArrar() method do?
If mycustomer is an arraylist, how do you want to get to its contents so you can display them?
Re: ArrayList<Class> is returning null
another quick question...
do I need the following in the GUI builder method
Code java:
tfgiven.addActionListener(null);
tflast.addActionListener(null);
tfpersonID.addActionListener(null);
tfaddress.addActionListener(null);
tfcity.addActionListener(null);
tfstate.addActionListener(null);
tfzip.addActionListener(null);
tfzip4.addActionListener(null)
I am going through all my code to see where the [] are coming from. the toArray() method is not needed for an arrayList, right? I should be getting the contents from the toString in the Person class right, but I am having trouble incorporating the customerID into the array.
Re: ArrayList<Class> is returning null
Why are you adding a null entry to the list of listeners?
Quote:
toArray() method is not needed
It depends on what the code is supposed to be doing. If you need the contents in an array, that would be the method to use.
Re: ArrayList<Class> is returning null
good question, if I leave it with nothing in there, Eclipse starts yelling at me. It is asking me to instantiate, the only way that I am not getting an error is to use null or which is my button that will list all my entries in the console.
Re: ArrayList<Class> is returning null
sorry, I have no idea what you are talking about.
Re: ArrayList<Class> is returning null
in order to find the [] would I put in a print statement, those would come from the arrayList wouldn't they?
Re: ArrayList<Class> is returning null
Then where is the ending ]
Code :
ArrayList<String> al = new ArrayList<String>();
al.add("SSS");
System.out.println(al); // [SSS]
Re: ArrayList<Class> is returning null
ok looks easy enough, I have tried to put this statement under my arraylist under the class method, I have tried in several places within my code and the add always stays underlined red???
Re: ArrayList<Class> is returning null
Try compiling with the javac command so you can get error messages if you can't figure out how to use the IDE.