Java:Null检查的NullPointerException?

时间:2015-11-07 00:22:19

标签: java object for-loop arraylist nullpointerexception

我已经阅读了这里的一些NullPointerException线程,并且已经将我的代码格式化为使用我找到的一个简单检查,但我仍然得到NullPointerException。

完整代码:请注意错误所在的****

public class Assignment11 {

public static void main(String[] args) {
    ArrayList<Employee> employeeArray = new ArrayList<Employee>();
    ArrayList<Book> booksToCirculate = new ArrayList<Book>();
    ArrayList<Book> archivedbooks = new ArrayList<Book>();
    int passAndCirc = 0;
    int totalEmployees = 0;

    addBook("Software Engineering", booksToCirculate);
    addBook("Chemistry", booksToCirculate);


    addEmployee("Adam", booksToCirculate, employeeArray);
    addEmployee("Sam", booksToCirculate, employeeArray);
    addEmployee("Ann",booksToCirculate, employeeArray);



    circulateBook("Chemistry","1999 03 20",booksToCirculate,employeeArray);
    //circulateBook("Software Engineering","1998 02 20",booksToCirculate,employeeArray);



    passOn("Chemistry", "1999 03 25",employeeArray,archivedbooks,booksToCirculate);

    passOn("Chemistry", "1999 03 29",employeeArray,archivedbooks,booksToCirculate);

    passOn("Chemistry", "1999 03 30",employeeArray,archivedbooks,booksToCirculate);

    passOn("Software Engineering","1998 02 21",employeeArray,archivedbooks,booksToCirculate);

//  passOn("Software Engineering","1998 02 29",employeeArray,archivedbooks,booksToCirculate);




}

public static void addEmployee(String aName, ArrayList<Book> booksToCirculate, ArrayList<Employee> employeeArray)
{
    Employee anEmployee = new Employee();
    anEmployee.setName(aName);
    employeeArray.add(anEmployee);
    for (Book b : booksToCirculate)
    {
        b.getQueue().add(anEmployee); //adds employee to each queue
    }
    System.out.println(anEmployee.getName() + " has been added to each queue, and list of employees.");


}

public static void addBook(String aName, ArrayList<Book> booksToCirculate)
{
    Book aBook = new Book();
    aBook.setBookName(aName);
    aBook.setArchived(false);
    //aBook.setStartDate(LocalDate.now()); //lookin good
    booksToCirculate.add(aBook);
    System.out.println(aBook.getBookName() + " has been added to the library.");

}

public static void circulateBook(String aName, String aDate, ArrayList<Book> booksToCirculate, ArrayList<Employee> employeeArray)
{
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern ( "yyyy MM dd" );
    LocalDate dateStarted = formatter.parse ( aDate , LocalDate :: from ); //have the date in 2015 11 05 format
    Employee anEmployee = null;
    Book aBook = null;
    Queue<Employee> Employees = new LinkedList<>();


    for (Book b : booksToCirculate)
    {
        if (b.getBookName().equals(aName))
        {
            b.setStartDate(dateStarted);
            anEmployee = b.getQueue().poll();
            aBook = b;

            Employees = b.getQueue();
            b.setQueue(Employees);              
        }


    }
    for (Book b: booksToCirculate)
    {
        if (!b.getBookName().equals(aName))
        {
            b.setQueue(Employees);
            b.getQueue().add(anEmployee);
        }
    }

    for (Employee e : employeeArray)
    {
        if (e.getName().equals(anEmployee.getName()))
        {
            //System.out.println(e.getName());
            e.setaBook(aBook);
            e.setRetainingTime(0);
            System.out.println(e.getName()+" Has the book " + e.getaBook().getBookName()+" Given to him on: "+e.getaBook().getStartDate() + " setting his/her waiting time to " + e.getRetainingTime()+ ".");
        }
    }


}

public static void passOn(String aName, String aDate, ArrayList<Employee> employeeArray, ArrayList<Book> archivedBooks, ArrayList<Book> booksToCirculate)
{
    Employee anEmployee = null;
    Book aBook = null;
    Queue<Employee> Employees = new LinkedList<>();
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern ( "yyyy MM dd" );
    LocalDate datePassed = formatter.parse ( aDate , LocalDate :: from );
    LocalDate dateReceived;
    LocalDate startDate;
    int daysElapsed = 0;
    int daysElapsed2 = 0;

    for (Employee e : employeeArray)
    {
***************************************************************************     
        if (e.getaBook().getBookName() != null)  //Where the error is
            if (e.getaBook().getBookName().equals(aName)){ 
            {
                startDate = e.getaBook().getStartDate();
                Period period = Period.between ( startDate, datePassed );
                daysElapsed2 = period.getDays();
                e.setRetainingTime(daysElapsed2);
                System.out.println(e.getName() + " has passed on "+ e.getaBook().getBookName()+" changing his/her's retaining time to " + daysElapsed2+ ".");

            }
        }

    }
//**************************************************************************
    for (Book b : booksToCirculate)
    {
        outerloop:
        if (b.getBookName().equals(aName))
        {
            if (b.getQueue().isEmpty())
            {
                b.setEndDate(datePassed);
                archivedBooks.add(b);
                b.setEndDate(datePassed);
                b.setArchived(true);
                System.out.println("The book " + b.getBookName() + " has been archived.");
                break outerloop;
            }
            dateReceived = b.getStartDate();
            Period period = Period.between ( dateReceived, datePassed );
            daysElapsed = period.getDays();
            anEmployee = b.getQueue().poll();
            anEmployee.setWaitingTime(daysElapsed);
            anEmployee.setaBook(b);
            aBook = b;

            Employees = b.getQueue();
            b.setQueue(Employees);              
        }
    }

    for (Book b: booksToCirculate)
    {
        if (!b.getBookName().equals(aName))
        {
            b.setQueue(Employees);

            b.getQueue().add(anEmployee);
        }
    }

    for (Employee e : employeeArray)
    {
        if (e.getName().equals(anEmployee.getName()))
        {
            e.setaBook(aBook);
            e.setRetainingTime(daysElapsed);
            System.out.println(e.getName()+" Has the book " + e.getaBook().getBookName()+" passed to him on: "+datePassed + " setting his/her waiting time to " + e.getRetainingTime()+ ".");
        }
    }

    //break maybe

}
}

class Employee{
String name;
int waiting_time;
int retaining_time;
int priority;
Book aBook;
ArrayList <Book> booksRead = new ArrayList<Book>();

public Employee()
{
    this.waiting_time=0;
    this.retaining_time=0;
}

public void setName(String aName)
{
    name = aName;
}

public String getName()
{
    return name;
}

public void setWaitingTime(int waitingtime)
{
    waiting_time = waitingtime;
}

public int getWaitingTime()
{
    return waiting_time;
}

public void setRetainingTime(int retainingtime)
{
    retaining_time = retainingtime;
}

public int getRetainingTime()
{
    return retaining_time;
}

public void setPriority()
{
    priority = waiting_time - retaining_time;
}

public int getPriority()
{
    return priority;
}

public void setaBook(Book book)
{
    aBook = book;
}
public Book getaBook()
{
    return aBook;
}

public void setbooksRead(ArrayList<Book> j)
{
    booksRead = j;
}

public ArrayList<Book> getbooksRead()
{
    return booksRead;
}
}

class Book{
String name;
LocalDate start_date;
LocalDate end_date;
boolean archived;
Queue<Employee> Employees = new LinkedList<>();

public Book()
{

}

public void setBookName(String aBook)
{
    name = aBook;
}

public String getBookName()
{
    return name;
}

public void setStartDate(LocalDate starting)
{
    start_date = starting;
}

public LocalDate getStartDate()
{
    return start_date;
}

public void setEndDate(LocalDate ending)
{
    end_date = ending;
}

public LocalDate getEndDate()
{
    return end_date;
}

public void setArchived(boolean ba)
{
    archived = ba;
}

public boolean isArchived()
{
    return archived;
}

public void setQueue(Queue<Employee> qa)
{
    Employees = qa;
}

public Queue<Employee> getQueue()
{
    return Employees;
}
}

我无法弄清楚为什么我在我的无效点检查器上获得nullpointerexception以确保事情不为空,一直盯着代码一小时并且看起来不会找到我在哪里出错。非常感谢任何和所有帮助!

仅供参考:我知道不好意思发布你的整个代码,但不知道哪里出现了什么/出了什么问题,这样就迫使我这么做了。

3 个答案:

答案 0 :(得分:1)

enullArrayList可以包含null)或getaBook()的调用返回null因此当您致电时e.getaBook().getBookName()抛出NullPointerException

答案 1 :(得分:1)

e可能是nulle.getaBook()也可能是null

答案 2 :(得分:1)

请对ee.getaBook()进行空检查。仅供参考,我更新了代码以适应单一陈述中的所有条件。

 for (Employee e : employeeArray)
{

    if (e!=null && e.getaBook()!=null &&e.getaBook().getBookName() != null && e.getaBook().getBookName().equals(aName))  //modified code

           startDate = e.getaBook().getStartDate();
            Period period = Period.between ( startDate, datePassed );
            daysElapsed2 = period.getDays();
            e.setRetainingTime(daysElapsed2);
            System.out.println(e.getName() + " has passed on "+ e.getaBook().getBookName()+" changing his/her's retaining time to " + daysElapsed2+ ".");
   }

}