-1

I have read through some of the NullPointerException threads on here, and have formatted my code to use one of the simple checks I found, but I'm still getting a NullPointerException.

Full Code: note the **** for where the error is

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;
}
}

I can't figure out how why I'm getting a nullpointerexception on my nullpoint checker making sure things aren't null, been staring at the code for an hour and can't seem to find where I'm going wrong with this one. Any and all help is much appreciated!

FYI: I know it's frowned upon to post you're entire code, but not having a damn clue where/what is going wrong kinda forced me too on this one.

Bob
  • 1,344
  • 3
  • 29
  • 63
  • 1
    You get a NPE because either `e` is null or `e.getaBook()` returns null. You have to check these two values as well. – MalaKa Nov 07 '15 at 00:28

3 Answers3

1

Either e is null (an ArrayList can contain null) or the call to getaBook() is returning null hence when you call e.getaBook().getBookName() aNullPointerException is thrown.

kstandell
  • 775
  • 6
  • 16
1

e might be null. e.getaBook() might be null as well.

Grogi
  • 2,099
  • 17
  • 13
1

please do null check on e and e.getaBook(). FYI,I updated the code to fit all the conditions in single statement.

 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+ ".");
   }

}
bakki
  • 314
  • 3
  • 10