2014-01-15 251 views
-2

我想通過調用另一個類(庫)的printlibrary方法來顯示我的ArrayList在動作偵聽器方法中的元素。我一直在這條線上得到一個空指針異常library.addItem(libraryItem);我不知道如何解決這個問題 我已經使用if語句形式的驗證來查看庫是否爲null,並且使用system.out來查看按鈕動作列表是否爲空實際上工作和按鈕確實工作空指針異常

 { 

    {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(); 
    } 

這裏是我的LIBRARYFRAME類的休息!

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




} 

} 

這是我的庫類

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


} 

}

這是我的庫項目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; 
      } 


    } 

這是我的成員類`

    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; 

     } 




} 

`

+1

是你的'庫'不'空'? –

+1

你試過調試器嗎?另外,請看看代碼在你的問題中的組織結構如何,並使其更好。最後,嘗試創建一個[SSCCE](http://sscce.org/) –

+0

你確定t,y和q不爲空嗎?使用調試器怎麼樣? – elyashiv

回答

0

在嘗試使用它之前,您似乎沒有將庫分配給任何內容。

+0

感謝隊友不敢相信我沒有看到你的救星! – user3198936

+0

沒問題。我之前寫過這篇文章,因爲我們在irc上看到過類似的問題:http://www.antwerkz.com/dealing-with-nullpointerexceptions/ – evanchooly