Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 20 of 20

Thread: ArrayList<Class> is returning null

  1. #1
    Member
    Join Date
    Mar 2012
    Posts
    42
    My Mood
    Confused
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default 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:

    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.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Mar 2012
    Posts
    42
    My Mood
    Confused
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: ArrayList<Class> is returning null

    ok, so I should have done the ArrayList like:
    ArrayList<Customer> mycustomer = new ArrayList<Customer>();

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Mar 2012
    Posts
    42
    My Mood
    Confused
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default 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.
    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?

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    Mar 2012
    Posts
    42
    My Mood
    Confused
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: ArrayList<Class> is returning null

    ok, so I have done that. In my CustomerListener method, am I correct in creating:
    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.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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?
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Member
    Join Date
    Mar 2012
    Posts
    42
    My Mood
    Confused
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: ArrayList<Class> is returning null

    Here is what my program is doing:
    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
    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
    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.

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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 [
    Last edited by Norm; April 14th, 2012 at 03:02 PM.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Member
    Join Date
    Mar 2012
    Posts
    42
    My Mood
    Confused
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: ArrayList<Class> is returning null

    hmm....ok. From the Person class I have a toString()
    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
    mycustomer.toArrar();
    I have a feeling though I should be calling the toString(), is that right?

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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?
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Member
    Join Date
    Mar 2012
    Posts
    42
    My Mood
    Confused
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: ArrayList<Class> is returning null

    another quick question...
    do I need the following in the GUI builder method
    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.

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: ArrayList<Class> is returning null

    Why are you adding a null entry to the list of listeners?

    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.
    Last edited by Norm; April 14th, 2012 at 04:34 PM.
    If you don't understand my answer, don't ignore it, ask a question.

  15. #15
    Member
    Join Date
    Mar 2012
    Posts
    42
    My Mood
    Confused
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default 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
    new DisplayAllListener()
    which is my button that will list all my entries in the console.

  16. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: ArrayList<Class> is returning null

    sorry, I have no idea what you are talking about.
    If you don't understand my answer, don't ignore it, ask a question.

  17. #17
    Member
    Join Date
    Mar 2012
    Posts
    42
    My Mood
    Confused
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default 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?

  18. #18
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: ArrayList<Class> is returning null

    Then where is the ending ]
         ArrayList<String> al = new ArrayList<String>();
         al.add("SSS");
         System.out.println(al);  // [SSS]
    If you don't understand my answer, don't ignore it, ask a question.

  19. #19
    Member
    Join Date
    Mar 2012
    Posts
    42
    My Mood
    Confused
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default 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???

  20. #20
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. [SOLVED] Returning ArrayList via user input
    By IanSawyer in forum Collections and Generics
    Replies: 4
    Last Post: March 27th, 2012, 05:40 PM
  2. null pointer exception on extending a server class
    By u-will-neva-no in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 24th, 2012, 08:46 AM
  3. Array with null (same for Arraylist)
    By Alex L in forum What's Wrong With My Code?
    Replies: 5
    Last Post: December 5th, 2011, 12:51 AM
  4. Using enum, returning null
    By 9erNumber16 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 13th, 2011, 05:40 PM
  5. Returning Null
    By Woody619 in forum What's Wrong With My Code?
    Replies: 11
    Last Post: July 22nd, 2010, 12:53 PM