我试图通过从另一个类(库)调用printlibrary方法在动作侦听器方法中显示我的ArrayList的元素。我一直在这一行library.addItem(libraryItem);
得到一个空指针异常我不明白如何解决这个问题
我已经以if语句的形式使用验证来查看library是否为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));
}
}
}
这是我的图书馆项目课程
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;
}
}
`
答案 0 :(得分:0)
在您尝试使用库之前,您似乎无法将库分配给任何内容。