gpt4 book ai didi

java - 尝试从 JDialog 框中将项目插入数组时出现 NullPointerException

转载 作者:太空宇宙 更新时间:2023-11-04 06:25:15 25 4
gpt4 key购买 nike

正如它所述,当我尝试通过 JDialog 弹出窗口将项目插入到对象数组中时,我收到 NullPointerException。我重新设计了一个现有应用程序来创建 JDialog,它从当前名为 Project6() 的另一个应用程序类打开。 JDialog 类称为 ProcessRec(),当它作为独立 GUI 运行时(在我将其设为 JDialog 之前),它工作得很好。

在这种情况下,我的堆栈跟踪相当无用,因为它只指向尝试将数据插入数组的方法(正如我所说,之前工作得很好)以及与它们对应的 GUI 按钮。

ProcessRec() 的构造函数接受一个 ToolWarehouse() 对象,该对象与创建我的对象数组的类相匹配(这是我定义的另一个类 ToolItem() 中的对象数组)。

public ProcessRec(ToolWarehouse tools)

当它独立运行时,ProcessRec() 构造函数参数为空(默认)。

ProcessRec() 作为独立 GUI 运行以将数据输入到数组中时,我将使用默认构造函数创建一个对象,如下所示:

ToolWareHouse tools = new ToolWarehouse();

然后我将使用它的方法 insert() 将数据输入到数组中。

现在它是一个 JDialog,但我被指示更改为:

ToolWarehouse tools;

然而,这会导致 NullPointerException 因为,我猜测,编译器指向一个不存在的对象。当我在 ProcessRec() 中创建一个新对象时(就像我在独立对象时所做的那样),我不再遇到此异常。但是,当我尝试使用 insert() 将数据存储到数组中时,它不起作用,并且找不到数据(尽管我收到提示说插入成功)。

我对类的实例知之甚少,但我假设我的数据未显示的原因是因为它使用的是 ToolWarehouse() 类的完全不同的实例?格式是:

public ProcessRec(ToolWarehouse tools)

接受之前使用的类的相同实例吗?我可能偏离了基地,所以我希望有人能帮助我理解可能发生的事情以及原因

如果需要更多信息,我深表歉意,我会添加任何必要的信息。代码有点长,不想占用空间。万分感谢。

编辑:这是 ProcessRec() 类,它创建 JDialog 框,以及与其使用相对应的一些方法。

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

创建数组的ToolWarehouse类:

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

我现在正在处理的当前部分,它从 ToolWarehouse() 类调用 insert(),但在使用默认构造函数创建 ToolWarehouse() 类的对象时,是否没有正确插入到我正在处理的数组中(即使我的 println 调试语句表明确实如此):

public void readBSAFile() throws IOException, 
FileNotFoundException

{

FileInputStream fstream2 = new FileInputStream(filename);
DataInputStream dstream2 = new DataInputStream(fstream2);

while (dstream2.available() > 0)
{
try
{
toolName = dstream2.readUTF();
id = dstream2.readInt();
quality = dstream2.readInt();
numInStock = dstream2.readInt();
price = dstream2.readDouble();
dstream2.close();

System.out.println(toolName);
System.out.println(id);
System.out.println(quality);
System.out.println(numInStock);
System.out.println(price);

//tools.insert(quality, toolName, id, numInStock, price);
}
catch (EOFException e)
{
System.out.println("End of file reached");
}
}
tools.insert(quality, toolName, id, numInStock, price);
}//end readBSAFile

最佳答案

事实上,在尝试使用该对象之前,您必须确保该对象已初始化。你把

ToolWarehouse tools; 

作为类变量,在对话框方法中你可以说

tools = new ToolWareHouse();

创建对象并开始使用它。但也许更好的方法是将其直接初始化为类变量。例如写

ToolWarehouse tools = new ToolWareHouse(); 

在类(class)中名列前茅。 (所以它是一个类变量,并且在整个类中都是已知的)

您没有向我们展示太多代码,因此很难判断确切的问题是什么。但正如你所说,你没有初始化你的对象,所以当你试图将它传递给另一个方法并使用它时,你一定会得到一个 nullPointerException 。

关于java - 尝试从 JDialog 框中将项目插入数组时出现 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26879814/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com