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 4 of 4

Thread: using the return of a method to initialize an ArrayList

  1. #1
    Member
    Join Date
    Mar 2009
    Posts
    91
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default using the return of a method to initialize an ArrayList

    how could I use the return of extractCustomers and initialze ArrayList<Customer> in main method?

    import java.util.*;
     
    public class Main {
     
     
    	public static void main(String[] args) {
     
    		ArrayList<BankAccount> allAccounts = new ArrayList<BankAccount>();
     
    		Customer john = new Customer();
    		john.firstName = "John";
    		john.lastName = "Doe";
     
    		BankAccount johnBa = new BankAccount();
    		johnBa.accNumber = "111-222-333";
    		johnBa.balance = 200;
    		johnBa.myCustomer = john;
     
    		Customer nick = new Customer();
    		nick.firstName = "Nick";
    		nick.lastName = "James";
     
    		BankAccount nickBa = new BankAccount();
    		nickBa.accNumber = "222-333-444";
    		nickBa.balance = 100;
     
    		allAccounts.add(johnBa);
    		allAccounts.add(nickBa);
     
    		ArrayList<Customer> allCust;
     
    	}
     
    	static ArrayList<Customer> extractCustomers(ArrayList<BankAccount> ba) {
    		ArrayList<Customer> cu = new ArrayList<Customer>();
     
    		for(BankAccount b: ba) {
    			cu.add(b.myCustomer);
    		}
     
    		return cu;
    	}
     
    }


  2. #2
    Member
    Join Date
    Mar 2009
    Posts
    91
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: using the return of a method to initialize an ArrayList

    ok I got it, but now I'm trying to output the content of allCust ArrayList and I'm getting nullPointerException.... could someone give me a hint? Thanks

    import java.util.*;
     
    public class Main {
     
     
    	public static void main(String[] args) {
     
    		ArrayList<BankAccount> allAccounts = new ArrayList<BankAccount>();
     
    		Customer john = new Customer();
    		john.firstName = "John";
    		john.lastName = "Doe";
     
    		BankAccount johnBa = new BankAccount();
    		johnBa.accNumber = "111-222-333";
    		johnBa.balance = 200;
    		johnBa.myCustomer = john;
     
    		Customer nick = new Customer();
    		nick.firstName = "Nick";
    		nick.lastName = "James";
     
    		BankAccount nickBa = new BankAccount();
    		nickBa.accNumber = "222-333-444";
    		nickBa.balance = 100;
     
    		allAccounts.add(johnBa);
    		allAccounts.add(nickBa);
     
    		ArrayList<Customer> allCust = new ArrayList<Customer>();
    		allCust = extractCustomers(allAccounts);
     
    		for(Customer c : allCust) {
    			System.out.println(c.firstName+" "+c.lastName);
    		}
     
    	}
     
    	static ArrayList<Customer> extractCustomers(ArrayList<BankAccount> ba) {
    		ArrayList<Customer> cu = new ArrayList<Customer>();
     
    		for(BankAccount b: ba) {
    			cu.add(b.myCustomer);
    		}
     
    		return cu;
    	}
     
    }

    public class BankAccount {
     
    	String accNumber;
    	double balance; 
     
    	Customer myCustomer;
     
    }

    public class Customer {
     
    	String firstName;
    	String lastName;
     
    }
    Last edited by mia_tech; October 25th, 2012 at 03:08 PM.

  3. #3
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: using the return of a method to initialize an ArrayList

    Do you know what a NullPointerException is? Do you understand what the error message is telling you? The line number* to the error is included in the message.

    The best advice I have for this is to understand how to read the error message. Simple mistakes like this are common and understanding the error message details will be a useful skill.
    If you can not figure it out, copy paste the error message for help.

    *The number is not always exactly the line the error is on, but this is not the place for that topic.

  4. #4
    Member
    Join Date
    Mar 2009
    Posts
    91
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: using the return of a method to initialize an ArrayList

    Quote Originally Posted by jps View Post
    Do you know what a NullPointerException is? Do you understand what the error message is telling you? The line number* to the error is included in the message.

    The best advice I have for this is to understand how to read the error message. Simple mistakes like this are common and understanding the error message details will be a useful skill.
    If you can not figure it out, copy paste the error message for help.

    *The number is not always exactly the line the error is on, but this is not the place for that topic.
    I just figure out what I was missing!

Similar Threads

  1. [SOLVED] Return all values in an ArrayList from a method
    By IanSawyer in forum Collections and Generics
    Replies: 1
    Last Post: March 28th, 2012, 09:53 AM
  2. ArrayList recursion return help
    By Twoacross in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 21st, 2012, 04:51 PM
  3. Method return value(s)
    By mwr76 in forum Loops & Control Statements
    Replies: 3
    Last Post: October 2nd, 2011, 02:38 PM
  4. [SOLVED] How to get the return of a method inside an object in an ArrayList?
    By Hallowed in forum Java Theory & Questions
    Replies: 7
    Last Post: May 1st, 2011, 10:44 PM
  5. ArrayList Unexpected Return
    By Cammack in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 31st, 2010, 09:23 PM