I am a beginner-intermediate java programmer and I am getting a null pointer exception from my arraylist. I am writing a bookstore program for APCS and when i add the book, it is supposed to add to the arraylist in the inventory class. But when i call a method to search for a book (e.g. by title), it shows that there isn't anything in the arraylist.
//Here is my inventory class -- it has all methods for adding the book or searching for one The searching methods are in getBookByTitle, getBookByAuthor, and getBookByISBN and the method for adding a book is addBook
package webbazonab;
//Inventory Class
//Bharath Senthil
//Ansh Sikka
import java.util.ArrayList;
public class Inventory{
private ArrayList<Book> allBooks = new ArrayList<Book>();
private String bookTitles;
private String bookAuthors;
private String bookPrices;
private String bookCopies;
private String ISBNs;
public Inventory()
{
}
//@param double price, int copies, String bookTitle, String Author, String isbnNumber
public void addBooks(Book addedBook){
allBooks.add(addedBook);
}
public boolean isAvailable(){
for(Book myBook : allBooks){
if(myBook.copiesLeft() == 0)
return false;
}
return true;
}
public String populateTitle(){
for (Book titleBooks : allBooks){
bookTitles = titleBooks.getTitle() + "\n";
return bookTitles;
}
return bookTitles;
}
public String populateAuthor(){
for(Book authorBooks : allBooks){
bookAuthors = authorBooks.getAuthor() + "\n";
return bookAuthors;
}
return bookAuthors;
}
public String populatePrice(){
for (Book pricedBooks : allBooks){
bookPrices = String.valueOf(pricedBooks.getPrice()) + "\n";
}
return "$" + bookPrices;
}
/**
*
* @return
*/
public String populateCopies(){
for (Book amtBooks : allBooks){
bookCopies = String.valueOf(amtBooks.copiesLeft()) + "\n";
return bookCopies;
}
return bookCopies;
}
public String populateISBN(){
for (Book isbnNums : allBooks){
ISBNs = isbnNums.getIsbn() + "\n";
return ISBNs;
}
return ISBNs;
}
@SuppressWarnings("empty-statement")
public Book getBookByTitle(String titleSearch) {
for(Book titleBook : allBooks) {
if (titleBook.getTitle().equals(titleSearch)) {
return titleBook;
}
}
return null;
}
public Book getBookByISBN(String isbnSearch){
for(Book isbnBookSearches : allBooks){
if(isbnBookSearches.getIsbn().equals(isbnSearch)){
return isbnBookSearches;
}
}
return null;
}
public Book getBookByAuthor(String authorSearch){
for(Book authorBookSearches : allBooks){
if(authorBookSearches.getAuthor().equals(authorSearch)){
return authorBookSearches;
}
}
return null;
}
public void sort(){
for(int i = 0; i < allBooks.size(); i++)
{
for(int k = 0; k < allBooks.size(); k++)
{
if(((Book) allBooks.get(i)).getIsbn().compareTo(((Book) allBooks.get(k)).getIsbn()) < 1)
{
Book temp = (Book) allBooks.get(k);
allBooks.set(k, allBooks.get(i));
allBooks.set(i, temp);
}
else if(((Book) allBooks.get(i)).getIsbn().compareTo(((Book) allBooks.get(k)).getIsbn()) > 1)
{
Book temp = (Book) allBooks.get(i);
allBooks.set(i, allBooks.get(k));
allBooks.set(k, temp);
}
}
}
}
public ArrayList<Book> getBooks(){
return allBooks;
}
}
//The exception occurs when i call the method here (in another class):
Inventory lib = new Inventory();
jTextField12.setText(lib.getBookByAuthor(authorSearch).getTitle());
Here is my book class if you need it
package webbazonab;
//Webbazon AB
//Project By: Ansh Sikka and Bharath Senthil
public class Book
{
private double myPrice;
private String myTitle;
private String bookAuthor;
private String isbn;
private int myCopies;
public Book(double price, int copies, String bookTitle, String Author, String isbnNumber)
{
myPrice = price;
myCopies = copies;
myTitle = bookTitle;
bookAuthor = Author;
isbn = isbnNumber;
}
public double getPrice()
{
return myPrice;
}
public String getIsbn()
{
return isbn;
}
public String getTitle()
{
return myTitle;
}
public String getAuthor()
{
return bookAuthor;
}
public int copiesLeft(){
return myCopies;
}
public String notFound(){
return "The book you searched for could not be found!";
}
public String toString()
{
return "Title: " + getTitle() + "\nAuthor: " + getAuthor() + "\nNumber of Available Books: " + copiesLeft() + "\nPrice: $" + getPrice();
}
}
Thanks!