我想通过调用另一个类(库)的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;
}
}
`
是你的'库'不'空'? –
你试过调试器吗?另外,请看看代码在你的问题中的组织结构如何,并使其更好。最后,尝试创建一个[SSCCE](http://sscce.org/) –
你确定t,y和q不为空吗?使用调试器怎么样? – elyashiv