Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8735943
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T10:11:17+00:00 2026-06-13T10:11:17+00:00

I have the following code; however, it seems that I’m accessing an index in

  • 0

I have the following code; however, it seems that I’m accessing an index in the arraylist that doesn’t exist… here’s the code. Any help appreciated.

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;

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T10:11:19+00:00Added an answer on June 13, 2026 at 10:11 am
     BankAccount nickBa = new BankAccount();
            nickBa.accNumber = "222-333-444";
            nickBa.balance = 100;
    

    No customer assigned here for second BankAccount.

    But, you are trying to print customer details. For second BankAccount c would be null. Any operation on null reference results in NullPointerException.

     for(Customer c : allCust) {
                System.out.println(c.firstName+" "+c.lastName);
            }   
    

    Make sure c is not null before making any calls on c to avoid NullPointerException.

      for(Customer c : allCust) {
               if(c != null){
                System.out.println(c.firstName+" "+c.lastName);
                   }
            } 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code that works without using throw however when I use
I have the following code however am getting the error Uncaught TypeError: Object #<addThis>
I have the following code below however am confused as to why the div
I have the following code: UPDATE myTable SET Col1 = @Value However, I have
I have the following code which works perfectly fine, however I would like to
I have the following code which deletes a record from a database. However I
I have the following code $result = $handle->select()->from('store_details') ->where('store_details.store_id=?', $id) ->columns('store_details.store_name'); //->query(ZEND_DB::FETCH_OBJ); However, when
I have the following code that is able to create a class that has
So i have the following code on my img's width:100%; height: auto; However IE6
I have the following code, which seems to override the jQuery.error function: $.test =

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.