When I call the addBorrower method an error is thrown:
java.lang.NullPointerException at Membership.addBorrower(Membership.java:24)
sorry if this is a really stupid question, I'm just really stuck with it, I've looked everywhere for a solution and as far as I can tell that's what I've implemented, but I'm still doing something wrong. Any help is greatly appreciated.
/**
* Write a description of class Membership here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Membership
{
// instance variables - replace the example below with your own
private Borrower[] borrowers;
private int currentIndex;
/**
* Constructor for objects of class Membership
*/
public Membership(int maxNoOfBorrowers)
{
Borrower[] borrowers = new Borrower[maxNoOfBorrowers];
currentIndex = 0;
}
public void addBorrower(Borrower borrower)
{
if (currentIndex < borrowers.length)
{
borrowers[currentIndex] = borrower;
currentIndex++;
}
else
System.out.println("Membership full. Could not add borrower!");
}
public int getCapacity()
{
return borrowers.length;
}
public int getNumberOfBorrowers()
{
return currentIndex;
}
public void printAllBorrowers()
{
for (Borrower borrower:borrowers)
{
borrower.printBorrowerDetails();
}
}
}
Other classes(just in case you need them):
/**
* Write a description of class Borrower here.
*
* @author (A M)
* @version (a version number or a date)
*/
public class Borrower
{
private String firstName;
private String lastName;
private String libraryNumber;
private int noOfBooks;
private Address address;
/**
* Constructor for objects of class Borrower.
* The number of books should be set to 1.
*
* @param firstName The Borrower's first name
* @param lastName The Borrower's last name
* @param lNumber The Borrower's library number
* @param street The Borrower's street
* @param town The Borrower's town
* @param postcode The Borrower's postcode
*/
public Borrower(String fName, String lName, String lNumber,
String street, String town, String postcode)
{
firstName = fName;
lastName = lName;
libraryNumber = lNumber;
noOfBooks = 1;
address = new Address(street, town, postcode);
}
/**
* Constructor for objects of class Borrower.
* The number of books on loan should should be set to
* the supplied vale.
*
* @param fName The Borrower's first name
* @param lName The Borrower's last name
* @param lNumber The Borrower's library number
* @param numberOfBooks The initial book borrow
* @param street The Borrower's street
* @param town The Borrower's town
* @param postcode The Borrower's postcode
*/
public Borrower(String fName, String lName, String lNumber, int numberOfBooks,
String street, String town, String postcode)
{
firstName = fName;
lastName = lName;
libraryNumber = lNumber;
noOfBooks = numberOfBooks;
address = new Address(street, town, postcode);
}
// accessors
/**
* Get the Borrower's first name
*
* @return the Borrower's first name
*/
public String getFirstName()
{
return firstName;
}
/**
* Get the Borrower's last name
*
* @return the Borrower's last name
*/
public String getLastName()
{
return lastName;
}
/**
* Get the Borrower's library Number
*
* @return the Borrower's library number
*/
public String getLibraryNumber()
{
return libraryNumber;
}
/**
* Get the number of books on loan
*
* @return the number of books on loan
*/
public int getNoOfBooks()
{
return noOfBooks;
}
/**
* Return details of a borrower in a readable format
*
* @return the borrower's details
*/
public String toString()
{
String output = "";
output = firstName + " " + lastName
+ "\n" + address.toString()
+ "\nLibrary Number: " + libraryNumber
+ "\nNumber of loans: " + noOfBooks + "\n";
return output;
}
/**
* Print out the Borrower's details to the console window
*
*/
public void printBorrowerDetails()
{
System.out.println( toString());
}
// mutators
/**
* Increase the bumber of books on loan by 1
*
*/
public void borrowBook()
{
noOfBooks = noOfBooks + 1;
System.out.println("Books on loan: " + noOfBooks);
}
/**
* Increase the bumber of books on loan by a given number
*
* @param number of new loans to add to total
*/
public void borrowBooks(int number)
{
noOfBooks = noOfBooks + number;
System.out.println("Books on loan: " + noOfBooks);
}
/**
* Return a book
*
*/
public void returnBook ()
{
noOfBooks = noOfBooks - 1 ;
System.out.println("Books on loan: " + noOfBooks);
}
/**
* Return the Borrower's address
*
* @return the Borrower's address
*/
public String getAddress()
{
return address.toString();
}
/**
* Change the Borrower's address
*
* @param street the street
* @param town the town
* @param postcode the postcode
*/
public void setAddress(String street, String town, String postcode)
{
address.setFullAddress(street, town, postcode);
}
/**
* Print out Borrower's address
*/
public void printAddress()
{
address.printAddress();
}
} // end class
/**
* Address class for homework 2
*
* @author Alan Maughan
* @version 01
*/
public class Address
{
private String street;
private String town;
private String postcode;
/**
* Constructor for objects of class Address
*
* @param street the street
* @param town the town
* @param postcode the postcode
*/
public Address(String street, String town, String postcode)
{
this.street = street;
this.town = town;
this.postcode = postcode;
}
/**
* returns the street
*
* @return the street
*/
public String getStreet()
{
return street;
}
/**
* Returns the town
*
* @return the town
*/
public String getTown()
{
return town;
}
/**
* Returns the postcode
*
* @return the postcode
*/
public String getPostcode()
{
return postcode;
}
/**
* Returns the formatted address
* one element to a line
*
* @return the formatted address
*/
public String toString()
{
String output = "";
output = street + "\n" + town + "\n" + postcode;
return output;
}
/**
* Set the street
*
* @param Street the street
*/
public void setStreet(String street)
{
this.street = street;
}
/**
* Set the town
*
* @param town the town
*/
public void setTown(String town)
{
this.town = town;
}
/**
* Set the postcode
*
* @param postcode the postcode
*/
public void setPostcode(String postcode)
{
this.postcode = postcode;
}
/**
* Set the full address
*
* @param street the street
* @param town the town
* @param postcode the postcode
*/
public void setFullAddress(String street, String town, String postcode)
{
this.street = street;
this.town = town;
this.postcode = postcode;
}
/**
* print formatted address to console window
*/
public void printAddress()
{
System.out.println(toString());
}
}