每当我尝试运行我的代码并单击其中一个组合框选项(特别是控制台)时,我都会收到错误消息。这是我的代码:
public class Bancud_Project extends JFrame implements ActionListener{
//Declare global variables
double ttlamount, inv; //Clock JLabel lbltime = new JLabel("Time");
//[L1]Fields //[L2]Declare JButton
JTextField tboxusrnm = new JTextField("",10); JButton btnlogin = new JButton("OK");
JPasswordField tboxpswd = new JPasswordField("",10); JButton btncancel = new JButton("CANCEL");
JButton btnclear = new JButton("CLEAR");
JFrame mainfrm = new JFrame();
public void login(){ //Login code
//[L1]Labels
JLabel lbltitle = new JLabel("Welcome to Tek Shak!"); JLabel lbl1 = new JLabel("Please Log In.");
JLabel lbl3 = new JLabel("Password: "); JLabel lbl2 = new JLabel("Username: ");
//[n1]Fonts
lbltitle.setFont(new Font("Agency FB",Font.BOLD, 25));
lbl1.setFont(new Font("Agency FB",Font.BOLD, 18));
lbl2.setFont(new Font("Consolas",Font.PLAIN, 18));
lbl3.setFont(new Font("Consolas",Font.PLAIN, 18));
tboxusrnm.addActionListener(this);
tboxpswd.addActionListener(this);
btncancel.addActionListener(this);
btnlogin.addActionListener(this);
btnclear.addActionListener(this);
JPanel logindisplay = new JPanel();
logindisplay.setLayout(null); //Set Applicable for bounds
//Positioning//setBounds(x,y,L,W); //Add to panel //Color //Extras
lbltitle.setBounds(20,20,200,30); logindisplay.add(lbltitle); lbltitle.setForeground(Color.white);
lbl1.setBounds(20,60,150,30); logindisplay.add(lbl1); lbl1.setForeground(Color.white);
lbl2.setBounds(20,100,150,30); logindisplay.add(lbl2); lbl2.setForeground(Color.white);
lbl3.setBounds(20,140,150,30); logindisplay.add(lbl3); lbl3.setForeground(Color.white);
tboxusrnm.setBounds(130,100,150,30); logindisplay.add(tboxusrnm);
tboxpswd.setBounds(130,140,150,30); logindisplay.add(tboxpswd); //lbl3.setToolTipText("Password");
btnlogin.setBounds(20,190,120,30); logindisplay.add(btnlogin); btnlogin.setForeground(Color.white); btnlogin.setBackground(Color.darkGray);
btncancel.setBounds(20,225,260,30); logindisplay.add(btncancel); btncancel.setForeground(Color.white); btncancel.setBackground(Color.darkGray);
btnclear.setBounds(140,190,140,30); logindisplay.add(btnclear); btnclear.setForeground(Color.white); btnclear.setBackground(Color.darkGray);
logindisplay.setBackground(Color.darkGray);
mainfrm.add(logindisplay);
mainfrm.setTitle("TekShak Login");
mainfrm.setVisible(true);
mainfrm.setBounds(700,400,310,300);
mainfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) throws FileNotFoundException { //Main
{
Bancud_Project run = new Bancud_Project();
run.login();
}
try
{
Scanner userName = new Scanner(new FileReader("employeelist.txt"));
int id = 0;
String username = " ";
while(userName.hasNext()){
username = userName.next();
id = userName.nextInt();
}
System.out.println("Username: " +username);
System.out.println("UserId: " +id);
userName.close();
}
catch (FileNotFoundException ex)
{
System.out.print("Error. Error. Path Not found");
}
}//Main end
public void actionPerformed(ActionEvent e){//Action Listener
//Tooltip Extras
tboxusrnm.setToolTipText("Username"); btnlogin.setToolTipText("Log-in!");
tboxpswd.setToolTipText("Password"); btnclear.setToolTipText("Clears both fields");
btncancel.setToolTipText("Exits the program");
if (e.getSource() == btnlogin || e.getSource() == tboxpswd) { //If Enter pressed or password entered
String totest = ("cashier"); //Change this to employee no. someday
if(totest.equals(tboxusrnm.getText()) && totest.equals(tboxpswd.getText())){
mainfrm.setVisible(false);
try {
cashier Start = new cashier();
Start.machina();
} catch(FileNotFoundException ex) {
System.out.print("Oops, something went wrong. Error code:Daeneishandsomex100");
}
} else {
tboxusrnm.setText("");
tboxpswd.setText("");
JOptionPane.showMessageDialog(null,"Login Incorrect. Please check your login details.","Login Username or Password Incorrect", JOptionPane.ERROR_MESSAGE);
}
} else if (e.getSource() == btncancel){
System.exit(0); //Kill the program
} else if (e.getSource() == btnclear){
tboxusrnm.setText("");
tboxpswd.setText("");
}
}
class cashier extends JFrame implements ItemListener{ //New class
//Set global variables
Scanner productInv;
int itemcount;
double itemprice, stockValue;
String itemname, itemcat, HVI = " ";
String[] categories = {"Consoles","Credits","Gadgets","Gaming Gear","Robotic Kits"};
//String dreamgirl = "Lindee";
int itemid = 10000;
// \\\\\
//ID Format Cat-No-Type-Amt
//[INI] Components
JPanel pnlmain = new JPanel();
//Buttons
JButton btnpay = new JButton("Payment"); JButton btnedit = new JButton("Edit");
JButton btndelete = new JButton("Delete"); JButton btncncl = new JButton("Cancel");
JButton btnadd = new JButton("Enter");
//Other Components
JTextArea rcptdisp = new JTextArea("Receipt Displayed here");
JComboBox selection = new JComboBox(categories);
JList stocks = new JList(); JList items = new JList(); JList price = new JList();
JLabel lbltotal = new JLabel("Total:"); JLabel txttotal = new JLabel("0.00"); JLabel lblid = new JLabel("ID:");
JTextField idinput = new JTextField(10);
//All about menus
JMenuBar menu = new JMenuBar(); JMenu file = new JMenu("File");
JMenuItem LogOut = new JMenuItem("Log Out"); JMenuItem NewTransaction = new JMenuItem("New Transaction");
//Data Sets
//*
//JFrame
JFrame mainprg = new JFrame();
public void machina() throws FileNotFoundException {//////////////////////////////////MACHINA------->
try {
Scanner productInv = new Scanner(new FileReader("productinv.txt"));
} catch(FileNotFoundException ex) { //If file not found
System.out.print("Oops, something went wrong. ");
}
selection.setSelectedIndex(1);//Set Default to 0
menu.add(file);
file.add(LogOut);
file.add(NewTransaction);
//[TLP]Topleftpanel components //Extra code For debugging
JPanel pnltopleft = new JPanel(); pnltopleft.setLayout(null); pnltopleft.setBackground(Color.black);
JPanel pnlitemdsp = new JPanel();
JPanel pnlaction = new JPanel();
//[DSN]JPanel Formatting
pnlitemdsp.setLayout(new FlowLayout());
pnlitemdsp.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.black), "Items")); pnlitemdsp.setBounds(20,60,500,300); pnlitemdsp.setBackground(Color.lightGray);
pnlitemdsp.setLayout(null);
pnlitemdsp.add(stocks, BorderLayout.WEST); stocks.setBounds(15,20,110,260); stocks.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.black), "Stocks"));
pnlitemdsp.add(items, BorderLayout.CENTER); items.setBounds(135,20,220,260); items.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.black), "ItemID//Item"));
pnlitemdsp.add(price, BorderLayout.EAST); price.setBounds(365,20,125,260); price.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.black), "Price"));
pnltopleft.add(pnlaction); pnlaction.setBounds(20,370,500,170); pnlaction.add(lblid);
pnltopleft.add(pnlitemdsp);
///[ACT]Action Display Panel
pnlaction.setLayout(null);
pnlaction.add(btnpay); btnpay.setBounds(10,20,100,40); btnpay.setBackground(Color.lightGray);
pnlaction.add(btnedit); btnedit.setBounds(115,20,100,40); btnedit.setBackground(Color.lightGray);
pnlaction.add(btndelete); btndelete.setBounds(220,20,100,40); btndelete.setBackground(Color.lightGray);
pnlaction.add(btncncl); btncncl.setBounds(325,20,100,40); btncncl.setBackground(Color.lightGray);
pnlaction.add(btnadd); btnadd.setBounds(10,70,100,20); btnadd.setBackground(Color.lightGray);
pnlaction.add(idinput); idinput.setBounds(140,70,200,22);
pnlaction.add(lblid); lblid.setBounds(120,70,40,22);
//[DSN]Custom positions //setBounds(x,y,L,W); //Add to panel //Color //Extras
selection.setBounds(20,20,500,20); pnltopleft.add(selection); selection.setEditable(false);
selection.addItemListener(this);
//[TRP]Toprightpanel components //Extracode For debugging
JPanel pnltopright = new JPanel(); pnltopright.setLayout(null); pnltopright.setBackground(Color.lightGray);
pnltopright.add(rcptdisp); rcptdisp.setBounds(5,260,440,220); rcptdisp.setBackground(Color.white);
pnltopright.add(lbltotal); lbltotal.setBounds(10,490,50,30); lbltotal.setFont(new Font("Agency FB",Font.BOLD, 20));
pnltopright.add(txttotal); txttotal.setBounds(400,490,50,30); txttotal.setFont(new Font("Agency FB",Font.BOLD, 20));
//Add right to left
pnltopleft.add(pnltopright);
pnltopright.setBounds(530,20,450,520);
//[PCP]PicturePanel
JPanel pnlpic = new JPanel(); pnltopright.add(pnlpic); pnlpic.setBounds(5,5,440,300); pnlpic.setBackground(Color.black);
//Image
ImageIcon icon = new ImageIcon("A5.jpg");
JLabel pic = new JLabel(icon);
pnlpic.setLayout(null);
pic.setBounds(10,10,420,220);
pnlpic.add(pic);
//[MAIN]Payload
mainprg.setTitle("Cashier_Bancud");
mainprg.add(pnltopleft);
mainprg.setJMenuBar(menu);
mainprg.setBounds(10,10,1005,610); ///Program size
mainprg.setDefaultCloseOperation(EXIT_ON_CLOSE);
mainprg.setVisible(true);
//System.out.print("Error. I am not a robot!");
//[DTA]Data Importer
//(itemid = 10000; itemid < 10000; itemid++ )
//Simply do the same thing in I/o With the scanner to display values
}
public void itemStateChanged(ItemEvent e) {//Add listeners
try {
Scanner productInv = new Scanner(new FileReader("productinv.txt"));
} catch(FileNotFoundException ex) { //If file not found
System.out.print("Oops, something went wrong. ");
}
int temp = 0;
//e.getSource();
//String category = (String)selection.getSelectedItem();
if (selection.getSelectedIndex() == 0){
//if (e.getStateChange() == ItemEvent.SELECTED){ //"Consoles","Credits","Gadgets","Gaming Gear","Robotic Kits"
//Consoles 40000+
int totest = 40000;
temp = productInv.nextInt();
while(productInv.hasNext() && temp >= totest){
itemid=productInv.nextInt();
itemcat=productInv.next();
itemname=productInv.next();
System.out.print("Critewerks "+itemid +itemcat +itemname);
}
} else if (selection.getSelectedIndex() == 1) {
//Credits 10000+
System.out.print("Critewerks "+itemid +itemcat +itemname);
} else if (selection.getSelectedIndex() == 2) {
//Gadgets 20000+
} else if (selection.getSelectedIndex() == 3) {
//Gaming Gear 50000+
} else if (selection.getSelectedIndex() == 4) {
//RobotKit 30000+
}//End of the statement
productInv.close();
}
}//Cashier end
}//End Of The Line of code XD
主要错误发生在:
temp = productInv.nextInt();
while(productInv.hasNext() && temp >= totest){
itemid=productInv.nextInt();
itemcat=productInv.next();
itemname=productInv.next();
System.out.print("Critewerks "+itemid +itemcat +itemname);
}
我收到此错误:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Bancud_Project$cashier.itemStateChanged(Bancud_Project.java:303)
at javax.swing.JComboBox.fireItemStateChanged(JComboBox.java:1225)
at javax.swing.JComboBox.selectedItemChanged(JComboBox.java:1273)
at javax.swing.JComboBox.contentsChanged(JComboBox.java:1329)
at javax.swing.AbstractListModel.fireContentsChanged(AbstractListModel.java:118)
at javax.swing.DefaultComboBoxModel.setSelectedItem(DefaultComboBoxModel.java:93)
at javax.swing.JComboBox.setSelectedItem(JComboBox.java:578)
at javax.swing.JComboBox.setSelectedIndex(JComboBox.java:624)
at javax.swing.plaf.basic.BasicComboPopup$Handler.mouseReleased(BasicComboPopup.java:835)
at java.awt.AWTEventMulticaster.mouseReleased(AWTEventMulticaster.java:290)
at java.awt.Component.processMouseEvent(Component.java:6505)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3320)
at javax.swing.plaf.basic.BasicComboPopup$1.processMouseEvent(BasicComboPopup.java:499)
at java.awt.Component.processEvent(Component.java:6270)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4861)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2719)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:735)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:694)
at java.awt.EventQueue$3.run(EventQueue.java:692)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:708)
at java.awt.EventQueue$4.run(EventQueue.java:706)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:705)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)