0
我试图制作一个小型POS系统。而且我需要在'进行销售'前将不同的项目添加到列表中(我将在后面插入到数据库中作为单独的INSERT语句)在按下按钮时向textarea添加值的行
现在我遇到了添加按钮的问题销售到文字区域。当我只有一次销售进入时它工作得很好。但是,开始给一个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];
编译精细的代码,但我得到了例外。任何线索为什么? 任何帮助或暗示,我做错了赞赏:)
还有什么我可以初始化它?无法将其初始化为“”.. – Tsar 2014-11-03 18:14:54
@ShifaTsar假设您知道它需要的大小,您可以像这样初始化它:'String [] itemNo = new String [size];''。如果你不这样做,我会建议寻找'ArrayList',它可以随着你的使用而增长。 – resueman 2014-11-03 18:17:08
啊,谢谢。我一直忘记ArrayList。 :) – Tsar 2014-11-03 18:21:38