【发布时间】:2014-02-04 11:38:03
【问题描述】:
我试图通过从另一个类(库)调用 printlibrary 方法,在动作侦听器方法中显示我的 ArrayList 的元素。我在这一行不断收到空指针异常library.addItem(libraryItem); 我不明白如何解决这个问题
我已经以 if 语句的形式使用了验证来查看库是否为空,并使用 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;
}
}
`
【问题讨论】:
-
是你的
library不是null? -
你试过调试器了吗?另外,请查看代码在您的问题中看起来如何杂乱无章,并使其变得更好。最后,尝试创建一个SSCCE
-
你确定 t,y 和 q 不为空吗?使用调试器怎么样?
-
请只发布格式良好的代码,因为您的代码越容易阅读,我们就越容易理解并帮助您。请阅读帮助部分以了解如何让您发布的代码保留其格式。您提出问题时的目标应该是让这里的志愿者尽可能轻松地为您提供帮助。
-
@aoeu 英语不是每个人的母语这一事实怎么办?
标签: java arrays exception null