-2

I am trying to display the elements of my ArrayList in an action listener method by calling a printlibrary method from another class(library). I keep getting a null pointer exception at this line library.addItem(libraryItem); I dont understand how to fix this I have already used validation in the form of an if statement to see if library is null and a system.out to see if the button action listner is actually working and the button does work

        { 

    {System.out.println("hello world");
        String t = title.getText();     
        int y = Integer.parseInt(year.getText());
        int q = Integer.parseInt(quantity.getText()); 

                if(library != null)
                {
        LibraryItem libraryItem = new LibraryItem(t,y,q);
        library.addItem(libraryItem);
                }
                else
                {
                    System.out.println("its null");
                }


library.printLibrary();
    }

HERE IS THE REST OF MY LIBRARYFRAME CLASS!!!

   import javax.swing.JFrame;
   import javax.swing.JButton;
   import java.awt.GridLayout;
   import java.awt.event.ActionListener;  
   import java.awt.event.ActionEvent;
   import javax.swing.JTextField;
   import javax.swing.JLabel;



   public class LibraryFrame extends JFrame implements ActionListener
 {
     JLabel label, label2, label3;
     JTextField title, year, quantity;
     JButton button, button2;

      private Library library; 



public LibraryFrame()
{
  this.setLayout(new GridLayout(4,2,10,10)); //2nd set of co-ord (10,10) is for row and coloumn spacing.

   label = new JLabel("Title");
   title = new JTextField(20);

   label2 = new JLabel("Year");
   year = new JTextField(20);

   label3 = new JLabel("Quantity");
   quantity = new JTextField(20);

   button = new JButton("Add");
   button2 = new JButton("Search");
   button.addActionListener(this);


  this.add(label);
  this.add(title);
  this.add(label2);
  this.add(year);
  this.add(label3);
  this.add(quantity);
  this.add(button);
  this.add(button2); 

}   


public void actionPerformed(ActionEvent ev)
{ 



    {System.out.println("hello world");
        String t = title.getText();     
        int y = Integer.parseInt(year.getText());
        int q = Integer.parseInt(quantity.getText()); 

                if(library != null)
                {
        LibraryItem libraryItem = new LibraryItem(t,y,q);
        library.addItem(libraryItem);
                }
                else
                {
                    System.out.println("its null");
                }


library.printLibrary();
    }

}







public static void main (String args[])
{

    LibraryFrame frame = new LibraryFrame();
    frame.pack();
    frame.setVisible(true);




}

}   

HERE IS MY LIBRARY CLASS

 import java.util.ArrayList;


              public class Library {

         private ArrayList<LibraryItem> items;




public Library()
{
    items = new ArrayList<LibraryItem>();
}



public void addItem(LibraryItem newItem)
{
    items.add(newItem);



}

public LibraryItem searchForItem (String name)

{
    for(LibraryItem searchForItem: items)
    {
        if(searchForItem.getName().equals(name) )

    return searchForItem;


}
    return null;
}

public void printLibrary()
{


    for(int i = 0; i< items.size(); i++)
    {

        System.out.println(items.get(i));
    }


}

}

HERE IS MY LIBRARY ITEM CLASS`

public class LibraryItem 
{

    private String name;
    private int year, quantity;

       LibraryItem(String nameIn, int yearIn, int quantityIn)
       {
           name = nameIn;
           year = yearIn;
           quantity = quantityIn;



       }



    public boolean rent()
    {


        if(quantity > 0) 

        {
             quantity--;

        }
          return true;
    }






                   public String toString()
            {
                return name + " " +"has"+ " " +quantity+ "available";


                }

                  public String getName()
            {
                  return name;

            }


             public int getQuantity()
          {

            return quantity;
         }

              public int getYear()
            {

                  return year;
             }


   }

HERE IS MY MEMBER CLASS `

                   public class Member 
           {

     private String name ;
        private int dayOfBirth, monthOfBirth, yearOfBirth;



    Member(String nameIn, int dayOfBirthIn, int monthOfBirthIn, int yearOfBirthIn)
    {
       name = nameIn;
       dayOfBirth = dayOfBirthIn;
       monthOfBirth = monthOfBirthIn;
       yearOfBirth = yearOfBirthIn;



   }



          public String toString ()
    {
    return name + " " +dayOfBirth+ " " +monthOfBirth+ " " +yearOfBirth;

      }




}

`

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373

1 Answers1

0

You don't seem to assign library to anything before you try to use it.

evanchooly
  • 6,102
  • 1
  • 16
  • 23