正如它所述,当我尝试通过我的JDialog弹出窗口将一个项目插入一个对象数组时,我得到一个NullPointerException
。我重新编写了一个现有应用程序来创建JDialog
,它从另一个当前名为Project6()
的应用程序类打开。名为JDialog
的{{1}}类在作为独立GUI运行时(在我将其设为ProcessRec()
之前)运行良好。
在这种情况下,我的堆栈跟踪相当无用,因为它只指向尝试将数据插入数组的方法(就像我之前说的那样,之前工作正常)和与它们对应的GUI按钮。
JDialog
的构造函数接受一个ProcessRec()
对象,它与创建我的对象数组的类(这是我定义的另一个类的对象数组ToolWarehouse()
)相匹配。
ToolItem()
当它自己运行时,public ProcessRec(ToolWarehouse tools)
构造函数params为空(默认)。
当ProcessRec()
作为独立的GUI运行以将数据输入到数组中时,我会使用默认构造函数创建一个对象,如下所示:
ProcessRec()
然后我会使用它的方法ToolWareHouse tools = new ToolWarehouse();
将数据输入到数组中。
现在它是insert()
,但我被指示改为:
JDialog
然而,这会导致ToolWarehouse tools;
因为,我猜,编译器指向的是不存在的对象。当我在NullPointerException
中创建一个新对象时,正如我在独立时所做的那样,我不再得到ProcessRec()
。但是,当我尝试使用exception
将数据存储到数组中时,它不起作用并且无法找到数据(尽管我收到提示说插入成功)。
我对类的实例知之甚少,但我假设我的数据未显示的原因是因为它使用的是insert()
类的完全不同的实例?格式是:
ToolWarehouse()
接受之前使用的类的相同实例?我可能会离开这里,所以我希望有人可以帮助我理解可能发生的事情和为什么。
如果需要更多信息我道歉,我会添加任何必要的信息。代码有点冗长,我不想占用空间。万分感谢。
编辑:这是public ProcessRec(ToolWarehouse tools)
类,它创建了ProcessRec()
框,以及与其对应的一些方法。
JDialog
创建数组的ToolWarehouse类:
public class ProcessRec extends JDialog
{
//global declarations
private JButton insertBtn;
private JButton deleteBtn;
private JButton displayBtn;
private JButton hideBtn;
private JButton clearBtn;
private JTextField toolFld;
private JTextField idFld;
private JTextField priceFld;
private JTextField qualityFld;
private JTextField numInStockFld;
private JTextField messageFld;
private JLabel messageLbl;
private Font f1 = new Font("serif", Font.BOLD, 24);
private Font f2 = new Font("serif", Font.PLAIN, 18);
private Container c = getContentPane();
private String input = "";
private String toolInput = "";
private int responseCode = 0;
private int idInput = 0;
private int qualityInput = 0;
private int numInStockInput = 0;
private double priceInput = 0.0;
private ToolWarehouse tools = new ToolWarehouse();
//constructor for GUI elements and event listeners
public ProcessRec(ToolWarehouse tools)
{
setTitle("Project1");
setSize(520,450);
c.setLayout(new FlowLayout());
JLabel title = new JLabel("Tiny Tim's Tool Warehouse, Inc.");
title.setFont(f1);
c.add(title);
JTextField toolLbl = new JTextField("Enter tool name:", 15);
toolLbl.setEditable(false);
c.add(toolLbl);
toolFld = new JTextField(25);
c.add(toolFld);
JTextField idLbl = new JTextField("Enter ID:", 15);
idLbl.setEditable(false);
c.add(idLbl);
idFld = new JTextField(25);
c.add(idFld);
JTextField priceLbl = new JTextField("Base Price:", 15);
priceLbl.setEditable(false);
c.add(priceLbl);
priceFld = new JTextField(25);
c.add(priceFld);
JTextField qualityLbl = new JTextField("Enter Quality:", 15);
qualityLbl.setEditable(false);
c.add(qualityLbl);
qualityFld = new JTextField(25);
c.add(qualityFld);
JTextField numInStockLbl = new JTextField("Enter Number in Stock:", 15);
numInStockLbl.setEditable(false);
c.add(numInStockLbl);
numInStockFld = new JTextField(25);
c.add(numInStockFld);
insertBtn = new JButton("Insert");
c.add(insertBtn);
deleteBtn = new JButton("Delete");
c.add(deleteBtn);
displayBtn = new JButton("Display");
c.add(displayBtn);
hideBtn = new JButton("Hide");
c.add(hideBtn);
clearBtn = new JButton("Clear");
c.add(clearBtn);
JLabel messageLbl = new JLabel("Messages:");
messageLbl.setFont(f2);
c.add(messageLbl);
messageFld = new JTextField(30);
c.add(messageFld);
//button listeners
insertBtn.addActionListener(new EventListener());
deleteBtn.addActionListener(new EventListener());
displayBtn.addActionListener(new EventListener());
hideBtn.addActionListener(new EventListener());
clearBtn.addActionListener(new EventListener());
} //end constructor
public String getToolRecords(int index)
{
return tools.getRecord(index);
}
public int getNumberOfItems()
{
return tools.getNumberOfItems();
}
//Action Listener
private class EventListener implements ActionListener
{
public void actionPerformed(ActionEvent ev)
{
if (ev.getSource() == insertBtn)
{
toolInput = toolFld.getText();
input = idFld.getText();
idInput = Integer.parseInt(input);
input = priceFld.getText();
priceInput = Double.parseDouble(input);
input = qualityFld.getText();
qualityInput = Integer.parseInt(input);
input = numInStockFld.getText();
numInStockInput = Integer.parseInt(input);
responseCode = tools.insert(qualityInput, toolInput, idInput, numInStockInput,
priceInput);
if (responseCode == 1)
{
messageFld.setText(idInput + " - Successful insertion");
}
else if (responseCode == 0)
{
messageFld.setText(idInput + " - Array is full");
}
else if (responseCode == -1)
{
messageFld.setText(idInput + " - Duplicate/Invalid ID");
}
if (tools.getNumberOfItems() < 10)
{
JOptionPane.showMessageDialog(null, "Tool Name: " + toolInput
+ "\nTool ID: " + idInput + "\nTool Base Price: " + "$" + priceInput
+ "\nQuality: " + qualityInput + "\nNumber In Stock: "
+ numInStockInput, "Insert Review", JOptionPane.INFORMATION_MESSAGE);
}
else if (tools.getNumberOfItems() == 10)
{
JOptionPane.showMessageDialog(null, "The array is full, please delete an entry",
"Insert Review", JOptionPane.INFORMATION_MESSAGE);
}
}//end insert button
else if (ev.getSource() == deleteBtn)
{
input = idFld.getText();
idInput = Integer.parseInt(input);
responseCode = tools.delete(idInput);
if (responseCode == 1)
{
messageFld.setText(idInput + " - Successful deletion");
}
else if (responseCode == -1)
{
messageFld.setText(idInput + " - ID not found");
}
}//end delete button
else if (ev.getSource() == displayBtn)
{
tools.display();
}
else if (ev.getSource() == hideBtn)
{
//setState(JFrame.ICONIFIED);
}
else if (ev.getSource() == clearBtn)
{
qualityFld.setText(null);
toolFld.setText(null);
idFld.setText(null);
numInStockFld.setText(null);
priceFld.setText(null);
messageFld.setText(null);
repaint();
}//end clear button
}//end actionPerformed
}//end ActionListener
}//end Project1
我正在处理的当前部分,它从public class ToolWarehouse
{
//Global declarations
private int numberOfItems = 0;
private int index = 0;
private int returnCode = 0;
protected ToolItem[] toolArray = new ToolItem[10];
public ToolWarehouse()
{
numberOfItems = 0;
//creating the array of ToolItems
for (int i = 0; i < toolArray.length; i++)
{
toolArray[i] = new ToolItem();
System.out.println(toolArray[i]);
}
}//end constructor
public int searchArray(int id)
{
for (index = 0; index < toolArray.length; index++)
{
if (toolArray[index].getToolID() == id)
{
System.out.println("ID found at location " + index);
return index;
}
}
return -1;
}//end searchArray
public int insert(int quality, String name, int id, int numInStock, double price)
{
returnCode = searchArray(id);
if (numberOfItems == 10)
{
System.out.println("Array is full");
return 0;
}
else if (returnCode == -1)
{
boolean oK = toolArray[numberOfItems].assign(quality, name, id, numInStock,
price);
if (oK == true)
{
System.out.println("Successful insertion");
numberOfItems++;
return 1;
}
}
return -1;
}//end insert
public int delete(int ID)
{
returnCode = searchArray(ID);
if (returnCode != -1)
{
toolArray[returnCode] = new ToolItem();
for (index = returnCode; index < numberOfItems; index++)
{
toolArray[index] = toolArray[index + 1];
}
numberOfItems--;
System.out.println("Successful deletion");
return 1;
}
else
System.out.println("ID not found");
return -1;
}//end delete
public void display()
{
for (index = 0; index < numberOfItems; index++)
{
toolArray[index].calcAdjustedPrice();
toolArray[index].display();
}
}//end display
public String getRecord(int index)
{
return "Tool Name: " + toolArray[index].getName()
+ "| Tool ID: " + toolArray[index].getToolID()
+ "| Tool Quality: " + toolArray[index].getQuality()
+ "| Number in Stock: " + toolArray[index].getNumberInStock()
+ "| Tool Price: " + toolArray[index].getPrice();
}//end getRecord
public int getNumberOfItems()
{
return numberOfItems;
}
}//end ToolWarehouse
类调用insert()
,但不正确插入到我正在处理的数组中使用默认构造函数创建ToolWarehouse()
类的对象时(即使我的println调试语句指示它):
ToolWarehouse()
答案 0 :(得分:1)
实际上,在尝试使用对象之前,必须确保对象已初始化。你把
ToolWarehouse tools;
作为类变量,在对话框中你可以说
tools = new ToolWareHouse();
创建对象并开始使用它。但也许更好的方法是直接将其初始化为类变量。例如写
ToolWarehouse tools = new ToolWareHouse();
在班级的顶部。 (所以它是一个类变量,并且在整个班级中都是已知的)
您没有向我们展示太多代码,因此很难说出确切的问题是什么。但正如你所说,你没有初始化你的对象,所以当你试图将它传递给另一个方法&amp;时,你必须得到一个nullPointerException。用它。