【发布时间】:2014-11-03 18:08:47
【问题描述】:
我正在尝试制作一个小型 POS 系统。并且我需要在“进行销售”之前将不同的项目添加到列表中(我稍后将作为单独的 INSERT 语句插入到数据库中)
现在我遇到了将销售添加到文本区域的按钮的问题。当我只有一个销售可以进入时,它工作得很好。但是当我将值放入一个数组并循环它时,我开始给出NullPointerException ..
这是我的代码:
if (action.getSource() == btnAdd)
{
//values
String[] salesNo= null;
String[] itemNo = null;
String[] staffNo = null;
String[] customerNo = null;
Date date= new Date();
int[] quantity = null;
double[] finalprice = null;
//inserting itemno------------------------
String findItemNoCommand = "SELECT itemNo FROM `item` WHERE itemName = '" + itemList.getSelectedItem() + "'";
try
{
itemNo[count] = viewValue(conn, findItemNoCommand);
}
catch (SQLException e)
{
e.printStackTrace();
}
//inserting customerno------------------------
customerNo[count] = (String) customerList.getSelectedItem();
staffNo[count] = "null"; // to be developed
//checking and insertion of quantity------------------------
double tempquantity = Double.parseDouble(quantityText.getText());
if(tempquantity % 1 == 0)
{
quantity[count] = (int) tempquantity; //Checking if the value is an integer
}
else
{
JOptionPane.showMessageDialog(alphaPOS,
"You need to enter a whole number for Quantity",
"ERROR",
JOptionPane.ERROR_MESSAGE);
}
//calculating final price from price of item------------------------
String tempprice = null;
String findPriceCommand = "SELECT sellingprice FROM `item` WHERE itemNo = '" + itemNo + "'";
try
{
tempprice = viewValue(conn, findPriceCommand);
}
catch (SQLException e)
{
e.printStackTrace();
}
Double price = Double.parseDouble(tempprice);
finalprice[count] = quantity[count] * price;
//------------------------------------------------------------------
salesdata[count++] = new Sales(salesNo[count], itemNo[count], staffNo[count], customerNo[count], date, quantity[count], finalprice[count]);
salesTextArea.setText(" "+"\n");
int i =0;
while (salesdata[i] != null)
{
salesTextArea.insert("\t" + salesdata[i].finalprice, 2);
salesTextArea.insert("\t" + salesdata[i].quantity, 2);
salesTextArea.insert("\t" + dateFormat.format(salesdata[i].date), 2);
salesTextArea.insert("\t" + salesdata[i].customerNo, 2);
salesTextArea.insert("\t" + salesdata[i].staffNo, 2);
salesTextArea.insert("" + salesdata[i].itemNo, 2);
//salesTextArea.insert("\n", 2);
i++;
}
salesTextArea.insert("Item No \t StaffNo\t CustomerNo \t Date and Time \t\t Quantity \t Final Price", 0); }
salesdata 在这里声明:static Sales salesdata[] = new Sales[50];
代码编译得很好,但我得到了异常。任何线索为什么? 任何关于我做错的帮助或提示表示赞赏:)
【问题讨论】:
标签: java arrays nullpointerexception textarea