2012-09-22 68 views
0

所以我一直在尝试运行我的LibraryTest.java程序,但它必须使用sortBooksByTitle()和sortBooksByNumPages()时崩溃。两种排序方法都可以编译,但是当我尝试运行测试类时,它会崩溃。 这些是我的三个java文件。排序方法运行时崩溃

Book.java

public class Book { 

    private String author; 
    private String title; 
    private int numPages; 

    public Book() { 
     title = "EMPTY"; 
    } 

    public Book(String titleIn, String authorIn, int numPagesIn) { 
     title = titleIn; 
     author = authorIn; 
     numPages = numPagesIn; 
    } 

    public String getAuthor() { 
     return author; 
    } 

    public String getTitle() { 
     return title; 
    } 

    public int getNumPages() { 
     return numPages; 
    } 

    public String toString() { 
     return title + " by " + author + " (" + numPages + " pages)"; 
    } 
} 

Library.java

import java.util.Random; 
import java.util.Arrays; 

public class Library { 

    private Book[] array; 
    private int count; 
    private Random randomBook = new Random(); 

    public Library(int numBooks) { 
     array = new Book[numBooks]; 
     count = 0; 
    } 

    public int getCount() { 
     return count; 
    } 

    public void addBook(Book b) { 
     //check if program can add new book 

     if (count < array.length) { 
      array[count] = b; 
      count++; 

     } //if array is full, a message is thrown 
     else { 
      System.out.println("The Library is full!"); 
     } 

    } 

    //Adds content of a library to another library. 
    public void addLibrary(Library l) { 
     for (Book b : l.array) { 
      addBook(b); 
     } 
    } 

    //Returns a book after receiving a String input. 
    public Book getBook(String book) { 
     for (int i = 0; i < array.length - 1; i++) { 
      String titleBook = array[i].getTitle(); 
      if (titleBook.equals(book)) { 
       return array[i]; 
      } 
     } 
     Book newBook = new Book(); 
     return newBook; 


    } 

    //Returns the book located in the array index given by the input. 
    public Book getBook(int index) { 
     if (index < array.length) { 
      System.out.printf("num: %d", index); 
      return array[index - 1]; 

     } 

     Book newBook = new Book(); 
     return newBook; 
    } 

    //Uses the random number generator and returns a book located in the array which 
    //index is the random number obtained. 
    public Book getBook() { 
     int num = randomBook.nextInt(array.length); 
     //System.out.printf("random num: %d", num); 
     return array[num]; 

    } 

    //Sorts books alphabetically. 
    public void sortBooksByNumPages() { 
     for (int i = 0; i < array.length - 1; i++) { 
      for (int j = i + 1; j < array.length; j++) { 
       if (array[i].getNumPages() > array[j].getNumPages()) { 
        Book temp = array[i]; 
        array[i] = array[j]; 
        array[j] = temp; 
       } 
      } 
     } 
    } 

    //Sorts books by number of pages in ascending order. 
    public void sortBooksByTitle() { 
     for (int i = 0; i < array.length - 1; i++) { 
      for (int n = i; n < array.length; n++) { 
       if (array[n].getTitle().compareToIgnoreCase(array[i].getTitle()) < 0) { 
        Book temp = array[i]; 
        array[i] = array[n]; 
        array[n] = temp; 
       } 
      } 
     } 

    } 

    //Output each book's information. 
    public String toString() { 
     String s = "Number of books: " + count + "\n"; 
     for (int i = 0; i < array.length; i++) { 
      s = s + array[i] + "\n"; 
     } 
     return s; 
    } 
} 

LibraryTest.java

public class LibraryTest { 

    public static void main(String args[]) { 
     Library lib = new Library(3); 
     Book b1 = new Book("Java: How to Program", "Deitel and Deitel", 1496); 
     lib.addBook(b1); 

     Book b2 = new Book("A Brief History of Time", "Stephen Hawking", 212); 
     lib.addBook(b2); 

     Book b3 = new Book("The Art of War", "Sun Tzu", 384); 
     lib.addBook(b3); 

     Book b4 = new Book("Ender's Game", "Orson Scott Card", 352); 
     // This addBook call should fail since the Library lib is full 
     lib.addBook(b4); 

     Book b5 = new Book("The Singularity is Near", "Ray Kurzweil", 672); 

     Library lib2 = new Library(10); 
     lib2.addBook(b4); 
     lib2.addBook(b5); 

     System.out.print("\n\nOriginal library contents\n"); 
     // This should display that there are 3 books in the library & info 
     System.out.print(lib); 

     System.out.print("\n\nAfter combining libraries\n"); 
     lib2.addLibrary(lib); 
     lib = lib2; 
     // This should display that there are 5 books in the library & info 
     System.out.print(lib); 

     System.out.print("\n\nSorted by title\n"); 
     try { 
      lib.sortBooksByTitle(); 
     } catch (Exception e) { 
      System.out.println(e.toString()); 
      System.out.println(e.getMessage()); 
     } 
     // This should display the books in alphabetical order by title 
     System.out.print(lib); 

     /* 
     * System.out.print("\n\nSorted by number of pages\n"); 
     * lib.sortBooksByNumPages(); // This should display the books in 
     * increasing order of the number of //pages each one has 
     * System.out.print(lib); 
     */ 

     // This should display Ender's Game 
     System.out.print("\n\nBook 2:\n" + lib.getBook(1)); 
     // This should display the EMPTY book 
     System.out.print("\n\nBook 20:\n" + lib.getBook(20)); 

     System.out.print("\n\nBook 'The Art of War':\n" 
       + lib.getBook("The Art of War")); 

     // This should randomly display a book from the library (potentially 
     //different each time) 
     System.out.print("\n\nRandom book:\n" + lib.getBook()); 
    } 
} 

同样3个文件编译得很好,但当我尝试运行时崩溃。请帮帮我。谢谢。

+0

你能编辑帖子并显示错误吗? – jszakmeister

+0

那么当我运行测试类,这是我得到: 线程“main”显示java.lang.NullPointerException 在Library.sortBooksByTitle(Library.java:113) 在LibraryTest.main程度排序依标题 异常(LibraryTest.java:36) Library.java:113是线 如果 而LibraryTest.java(阵列[n]的.getTitle()与compareToIgnoreCase(阵列[I] .getTitle())<0): 36是 lib.sortBooksByTitle(); –

回答

2

问题是在sortBooksByTitle()方法中抛出的NullPointerException。在抛出异常的具体线路是 if (array[n].getTitle().compareToIgnoreCase(array[i].getTitle()) < 0).

,因为当你创建lib2通过调用new Library(10),导致其初始化其数组大小10.在调用sortBooksByTitle()数组包含5时这样做这是发生书籍和5 null值。一旦循环通过了5本书,它就会打到null并在其上调用getTitle(),这会导致您的NPE。

+0

那么,您可以通过添加空检查来修复排序方法。即在第一个for循环内添加第一个语句,如'if(array [i]!= null)',并且在第二个for循环内执行另一个类似的检查:if(array [n]!=空)'。你可能需要在你正在循环的地方进行类似的检查。 –

+0

感谢您的帮助! –

+0

我修复了for循环以循环库中实际存在的书的数量。谢谢 –