Java 空指针异常
我试图通过从另一个类库调用printlibrary方法,在操作侦听器方法中显示ArrayList的元素。我在该行库中不断收到空指针异常。addItemlibraryItem;我不知道如何解决这个问题 我已经使用了if语句形式的验证来查看library是否为null,以及system.out来查看按钮操作列表器是否实际工作,以及按钮是否工作Java 空指针异常,java,arrays,exception,null,Java,Arrays,Exception,Null,我试图通过从另一个类库调用printlibrary方法,在操作侦听器方法中显示ArrayList的元素。我在该行库中不断收到空指针异常。addItemlibraryItem;我不知道如何解决这个问题 我已经使用了if语句形式的验证来查看library是否为null,以及system.out来查看按钮操作列表器是否实际工作,以及按钮是否工作 { {System.out.println("hello world"); String t = title.ge
{
{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();
}
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));
}
}
这是我图书馆框架的其余部分班
{
{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();
}
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));
}
}
这是我的图书馆课
{
{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();
}
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));
}
}
这是我的会员班`
{
{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();
}
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 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;
}
}
`在尝试使用库之前,您似乎没有将库分配给任何对象。库是否不为空?您是否尝试过调试器?另外,请查看您的问题中代码的无序程度,并使其更好。最后,尝试创建一个值,您确定t、y和q不为null吗?使用调试器怎么样?请只发布格式良好的代码,因为代码越容易阅读,我们就越容易理解并帮助您。请阅读“帮助”部分,了解如何使发布的代码保留其格式。问问题时,你的目标应该是让这里的志愿者尽可能容易地帮助你。@aoeu英语不是每个人的母语这一事实怎么办?谢谢,伙计,我不敢相信我没有看到你是救命恩人!没问题。我不久前写了这篇文章,因为我们在irc上不断看到类似的问题:
{
{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();
}
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));
}
}