【问题标题】:Adding to an array declared by user throwing error [duplicate]添加到用户抛出错误声明的数组中[重复]
【发布时间】:2018-05-14 12:26:44
【问题描述】:

当我调用 addBorrower 方法时会抛出错误:

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

对不起,如果这是一个非常愚蠢的问题,我真的很坚持,我到处寻找解决方案,据我所知,这就是我已经实现的,但我仍在做某事错误的。非常感谢任何帮助。

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

}

其他类(以备不时之需):

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

【问题讨论】:

    标签: java arrays bluej user-defined


    【解决方案1】:

    问题是在构造函数中你做了以下事情:

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

    您所做的是初始化一个名为 borrowers 的新变量,而不是该类的成员变量。

    您应该具备以下条件:

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

    【讨论】:

    • 哦,非常感谢,我知道这很愚蠢。抱歉浪费你的时间:)
    • 永远不要浪费时间,祝你好运,如果你不知道,这很难发现。
    【解决方案2】:

    这行Borrower[] borrowers = new Borrower[maxNoOfBorrowers];需要在构造函数中改为this.borrowers = new Borrower[maxNoOfBorrowers];

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-12-03
      • 1970-01-01
      • 1970-01-01
      • 2015-07-14
      • 1970-01-01
      • 2022-09-29
      相关资源
      最近更新 更多