2017-11-30 3 views
-1

Wenn ich die addBorrower Methode aufrufen ein Fehler ausgelöst wird:Hinzufügen zu einem vom Benutzer angegebenen Array werfen Fehler

java.lang.NullPointerException bei Membership.addBorrower (Membership.java:24)

traurig, wenn Das ist eine wirklich blöde Frage, ich bin nur wirklich dran, ich habe überall nach einer Lösung gesucht und soweit ich weiß, habe ich das umgesetzt, aber ich mache immer noch etwas falsch. Jede Hilfe wird sehr geschätzt.

/** 
* 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(); 
     } 
    } 

} 

Andere Klassen (nur für den Fall müssen Sie sie):

/** 
 
* 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()); 
 
    } 
 
}

Antwort

0

Diese Linie Borrower[] borrowers = new Borrower[maxNoOfBorrowers]; bis 012 geändert werden mussim Konstruktor

0

Das Problem ist, dass Sie im Konstruktor wie folgt vor:

public Membership(int maxNoOfBorrowers) 
    { 
     Borrower[] borrowers = new Borrower[maxNoOfBorrowers]; 
     currentIndex = 0;   
    } 

, was Sie ist eine neue Variable mit dem Namen Kreditnehmer initialisieren getan haben, nicht derjenige das ist eine Membervariable der Klasse.

Sie folgende Voraussetzungen erfüllt sein sollten:

public Membership(int maxNoOfBorrowers) 
     { 
      borrowers = new Borrower[maxNoOfBorrowers]; 
      currentIndex = 0;   
     } 
+0

Oh, ich danke Ihnen so sehr, ich wusste, dass es etwas dumm war. sorry für verschwenden Sie Ihre Zeit :) –

+0

Nie eine Verschwendung von Zeit, viel Glück, es ist eine knifflige zu erkennen, wenn Sie es nicht wissen. –

Verwandte Themen