我在获取从“listallButton”检索到的数据以正确显示到 JTable 中时遇到问题。数据库正在SELECT * FROM ATM
以一组字符串的形式正确发送信息。我不确定它是否被正确接收和记录,因为 JTables 没有显示任何数据并且正在获取 nullpointerexception:
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.table.*;
import java.util.*;
import javax.swing.table.AbstractTableModel;
public class AtmView extends JApplet implements ActionListener{
final static String banner = "WELCOME TO SHETH BANK!";
JLabel bannerLabel;
Border blackline;
CardLayout myLayout;
JPanel screensPanel; //main center panel
JPanel loginPanel; //login panel
JButton loginButton; //login button
JTextField accountNumber; //login account number field
JPasswordField password; //login password/PIN field
String access_lvl; //login access level
public String account;
String pass;
DataObject communicationObject;
final static String LOGIN = "LOGIN";
JPanel empPanel; //employee panel
CardLayout empLayout; //employee center panel
JButton createacctButton; //employee create account button
JButton listallButton; //employee list all accounts button
JPanel empcenterPanel;
final static String EMP1 = "EMPLOYEE";
String balance;
String withdrawal_amount;
String withdraw_amount;
JLabel balanceDisplay;
final static String LIST = "listallPanel";
JPanel listallPanel;
JTable table;
String[] data;
String[][] data2;
String[] temp;
String row;
DefaultTableModel tbm;
JLabel listdisplay;
String acclist;
ArrayList<String> al;
JButton empBackButton; //emp back
JButton emplogoutButton; //emp logout
JButton empwithdrawButton;
JPanel custPanel; //customer panel
final static String CUST = "CUSTOMER";
JButton checkbalButton; //check balance
JButton withdrawButton; //withdraw
JButton depositButton; //deposit
JButton transferButton; //transfer
JLabel display; //server response display area
@Override
public void init(){
getContentPane().setVisible(true);
getContentPane().setLayout(new BorderLayout());
getContentPane().setBackground(Color.BLUE);
//getContentPane().setBorder(blackline);
bannerLabel = new JLabel(banner,JLabel.CENTER);
bannerLabel.setVerticalTextPosition(JLabel.CENTER);
bannerLabel.setHorizontalTextPosition(JLabel.CENTER);
bannerLabel.setFont(new Font("Serif", Font.BOLD, 26));
bannerLabel.setForeground(Color.yellow);
blackline = BorderFactory.createLineBorder(Color.black);
//bannerLabel.setBorder(blackline);
add(bannerLabel, BorderLayout.NORTH);
//bannerLabel.setBorder(BorderFactory.createMatteBorder(0, 0, 2, 0, Color.BLACK));
myLayout = new CardLayout();
screensPanel = new JPanel();
screensPanel.setLayout(myLayout);
add(screensPanel, BorderLayout.CENTER);
//================ LOGIN VIEW ==================================
loginPanel = new JPanel(new GridLayout(3,1));
loginPanel.setBackground(Color.BLUE);
JLabel loginTitle = new JLabel("ENTER LOGIN INFO", JLabel.CENTER);
//loginTitle.setVerticalAlignment(SwingConstants.TOP);
loginTitle.setFont(new Font("Serif", Font.BOLD, 20));
loginTitle.setForeground(Color.yellow);
loginPanel.add(loginTitle);
JPanel accountInfo = new JPanel(new GridLayout(2,1));
accountInfo.setBackground(Color.blue);
JPanel accNum = new JPanel();
accNum.setBackground(Color.BLUE);
JLabel accLabel = new JLabel("Account Number: ", JLabel.CENTER);
accLabel.setFont(new Font("Serif", Font.BOLD, 14));
accLabel.setForeground(Color.yellow);
accountNumber = new JTextField(4);
accountNumber.setHorizontalAlignment(JTextField.CENTER);
accNum.add(accLabel);
accLabel.setLabelFor(accountNumber);
accNum.add(accountNumber);
accountInfo.add(accNum);
JPanel pw = new JPanel();
pw.setBackground(Color.BLUE);
JLabel pwLabel = new JLabel("Password/PIN: ", JLabel.CENTER);
pwLabel.setFont(new Font("Serif", Font.BOLD, 14));
pwLabel.setForeground(Color.yellow);
password = new JPasswordField(4);
password.setHorizontalAlignment(JTextField.CENTER);
pw.add(pwLabel);
pwLabel.setLabelFor(password);
pw.add(password);
accountInfo.add(pw);
loginPanel.add(accountInfo);
JPanel logBut = new JPanel();
loginButton = new JButton("Login");
loginButton.addActionListener(this);
logBut.setBackground(Color.blue);
logBut.add(loginButton);
loginPanel.add(logBut);
screensPanel.add(loginPanel, "LOGIN");
//================ EMPLOYEE VIEW ==================================
empPanel = new JPanel(new BorderLayout());
empPanel.setBackground(Color.BLUE);
JLabel empLabel = new JLabel("Employee View",JLabel.CENTER);
empLabel.setVerticalTextPosition(JLabel.CENTER);
empLabel.setHorizontalTextPosition(JLabel.CENTER);
empLabel.setFont(new Font("Serif", Font.BOLD, 20));
empLabel.setForeground(Color.yellow);
empPanel.add(empLabel, BorderLayout.NORTH);
empLayout = new CardLayout();
empcenterPanel = new JPanel();
empcenterPanel.setBackground(Color.blue);
empcenterPanel.setLayout(empLayout);
empPanel.add(empcenterPanel, BorderLayout.CENTER);
JPanel empbuttonPanel = new JPanel(new GridLayout(3,2));
empbuttonPanel.setBackground(Color.blue);
checkbalButton = new JButton("Check Balance");
checkbalButton.addActionListener(this);
empbuttonPanel.add(checkbalButton);
withdrawButton = new JButton("Withdraw");
withdrawButton.addActionListener(this);
empbuttonPanel.add(withdrawButton);
depositButton = new JButton("Deposit");
depositButton.addActionListener(this);
empbuttonPanel.add(depositButton);
transferButton = new JButton("Transfer");
transferButton.addActionListener(this);
empbuttonPanel.add(transferButton);
createacctButton = new JButton("Create account");
createacctButton.addActionListener(this);
empbuttonPanel.add(createacctButton);
listallButton = new JButton("List all accounts");
listallButton.addActionListener(this);
empbuttonPanel.add(listallButton);
empcenterPanel.add(empbuttonPanel, "EmpButtonPanel");
createPanel = new JPanel(new GridLayout(6,1));
createPanel.setBackground(Color.blue);
JLabel createTitle = new JLabel("Enter new account info: ",JLabel.CENTER);
createTitle.setVerticalTextPosition(JLabel.CENTER);
createTitle.setHorizontalTextPosition(JLabel.CENTER);
createTitle.setFont(new Font("Serif", Font.BOLD, 18));
createTitle.setForeground(Color.yellow);
createPanel.add(createTitle);
JPanel cloginPanel = new JPanel(new FlowLayout());
cloginPanel.setBackground(Color.blue);
JLabel cloginLabel = new JLabel("Account number: ",JLabel.CENTER);
cloginLabel.setVerticalTextPosition(JLabel.CENTER);
cloginLabel.setHorizontalTextPosition(JLabel.CENTER);
cloginLabel.setFont(new Font("Serif", Font.BOLD, 14));
cloginLabel.setForeground(Color.yellow);
cloginPanel.add(cloginLabel);
createloginField = new JTextField(4);
createloginField.setHorizontalAlignment(JTextField.CENTER);
cloginLabel.setLabelFor(createloginField);
cloginPanel.add(createloginField);
createPanel.add(cloginPanel);
JPanel cpinPanel = new JPanel(new FlowLayout());
cpinPanel.setBackground(Color.blue);
JLabel cpinLabel = new JLabel("PIN: ",JLabel.CENTER);
cpinLabel.setVerticalTextPosition(JLabel.CENTER);
cpinLabel.setHorizontalTextPosition(JLabel.CENTER);
cpinLabel.setFont(new Font("Serif", Font.BOLD, 14));
cpinLabel.setForeground(Color.yellow);
cpinPanel.add(cpinLabel);
createpinField = new JTextField(4);
createpinField.setHorizontalAlignment(JTextField.CENTER);
cloginLabel.setLabelFor(createpinField);
cpinPanel.add(createpinField);
createPanel.add(cpinPanel);
JPanel cbalPanel = new JPanel(new FlowLayout());
cbalPanel.setBackground(Color.blue);
JLabel cbalLabel = new JLabel("Starting balance: ",JLabel.CENTER);
cbalLabel.setVerticalTextPosition(JLabel.CENTER);
cbalLabel.setHorizontalTextPosition(JLabel.CENTER);
cbalLabel.setFont(new Font("Serif", Font.BOLD, 14));
cbalLabel.setForeground(Color.yellow);
cbalPanel.add(cbalLabel);
createbalanceField = new JTextField(10);
createbalanceField.setHorizontalAlignment(JTextField.CENTER);
cbalLabel.setLabelFor(createbalanceField);
cbalPanel.add(createbalanceField);
createPanel.add(cbalPanel);
JPanel clvlPanel = new JPanel(new FlowLayout());
clvlPanel.setBackground(Color.blue);
JLabel clvlLabel = new JLabel("Access level (1 for Employee, 2 for Customer): ",JLabel.CENTER);
clvlLabel.setVerticalTextPosition(JLabel.CENTER);
clvlLabel.setHorizontalTextPosition(JLabel.CENTER);
clvlLabel.setFont(new Font("Serif", Font.BOLD, 14));
clvlLabel.setForeground(Color.yellow);
clvlPanel.add(clvlLabel);
createlvlField = new JTextField(1);
createlvlField.setHorizontalAlignment(JTextField.CENTER);
cbalLabel.setLabelFor(createlvlField);
clvlPanel.add(createlvlField);
createPanel.add(clvlPanel);
JPanel createbutPanel = new JPanel();
createbutPanel.setBackground(Color.blue);
createButton = new JButton("Create");
createButton.addActionListener(this);
createbutPanel.add(createButton);
createPanel.add(createbutPanel);
empcenterPanel.add(createPanel, "createPanel");
listallPanel = new JPanel(new BorderLayout());
listallPanel.setBackground(Color.blue);
JLabel listTitle = new JLabel("All accounts in system: ",JLabel.CENTER);
listTitle.setVerticalTextPosition(JLabel.CENTER);
listTitle.setHorizontalTextPosition(JLabel.CENTER);
listTitle.setFont(new Font("Serif", Font.BOLD, 16));
listTitle.setForeground(Color.yellow);
listallPanel.add(listTitle,BorderLayout.NORTH);
String[] columnNames = {"Account Number","PIN","Access Level","Balance",};
String [][] rowTest = {{"1111","1111","1","1000",},{"1111","1111","1","1000",}};
String delimiter = ",";
tbm = new DefaultTableModel();
tbm.addColumn("Account Number");
tbm.addColumn("PIN");
tbm.addColumn("Access Level");
tbm.addColumn("Balance");
table = new JTable(tbm);
JScrollPane scrollPane = new JScrollPane(table);
listallPanel.add(scrollPane,BorderLayout.CENTER);
empcenterPanel.add(listallPanel, "listallPanel");
JPanel logoutPanel = new JPanel(new FlowLayout());
emplogoutButton = new JButton("Logout");
emplogoutButton.addActionListener(this);
empBackButton = new JButton("Back");
empBackButton.addActionListener(this);
logoutPanel.setBackground(Color.blue);
logoutPanel.add(emplogoutButton);
logoutPanel.add(empBackButton);
empPanel.add(logoutPanel, BorderLayout.SOUTH);
screensPanel.add(empPanel, "EMPLOYEE");
//================ CUSTOMER VIEW ==================================
}
public void actionPerformed(ActionEvent event) {
if (event.getSource() == loginButton) {
account = accountNumber.getText();
pass = password.getText();
String query = "SELECT access_level FROM ATM a WHERE a.account_number='"+account+"' AND a.pin='"+pass+"'";
try {
communicationObject = new DataObject();
communicationObject.setMessage(query);
Socket socketToServer = new Socket("sunlab32.njit.edu",31414);
ObjectOutputStream myOutputStream = new ObjectOutputStream(socketToServer.getOutputStream());
ObjectInputStream myInputStream = new ObjectInputStream(socketToServer.getInputStream());
myOutputStream.writeObject(communicationObject);
communicationObject = (DataObject)myInputStream.readObject();
access_lvl = communicationObject.getMessage();
myOutputStream.close();
myInputStream.close();
socketToServer.close(); }
catch(Exception e){System.out.println(e);}
if ((access_lvl.charAt(0)) == '1') {
myLayout.show(screensPanel, EMP1);
empLayout.first(empcenterPanel);}
else {myLayout.show(screensPanel, CUST); } }
else if(event.getSource() == emplogoutButton) {
accountNumber.setText("");
password.setText("");
account = "";
pass = "";
empLayout.first(empcenterPanel);
myLayout.show(screensPanel, LOGIN);
doLayout();}
else if(event.getSource() == checkbalButton) {
String query = "SELECT balance FROM ATM a WHERE a.account_number='"+account+"' AND a.pin='"+pass+"'";
try {
communicationObject = new DataObject();
communicationObject.setMessage(query);
Socket socketToServer = new Socket("sunlab32.njit.edu",31414);
ObjectOutputStream myOutputStream = new ObjectOutputStream(socketToServer.getOutputStream());
ObjectInputStream myInputStream = new ObjectInputStream(socketToServer.getInputStream());
myOutputStream.writeObject(communicationObject);
communicationObject = (DataObject)myInputStream.readObject();
balance = communicationObject.getMessage();
myOutputStream.close();
myInputStream.close();
socketToServer.close(); }
catch(Exception e){System.out.println(e);}
balanceDisplay.setText(balance);
//balanceDisplayPanel.add(balanceDisplay);
//checkBalancePanel.add(balanceDisplayPanel);
empLayout.show(empcenterPanel, EMPBAL); }
else if(event.getSource() == empBackButton) {
empLayout.first(empcenterPanel); }
else if(event.getSource() == withdrawButton) {
empLayout.show(empcenterPanel, EMPWITH); }
else if(event.getSource() == empwithdrawButton) {
withdraw_amount = withdrawField.getText();
Integer w_amt = Integer.valueOf(withdraw_amount);
String query = "SELECT balance FROM ATM a WHERE a.account_number='"+account+"' AND a.pin='"+pass+"'";
try {
communicationObject = new DataObject();
communicationObject.setMessage(query);
Socket socketToServer = new Socket("sunlab32.njit.edu",31414);
ObjectOutputStream myOutputStream = new ObjectOutputStream(socketToServer.getOutputStream());
ObjectInputStream myInputStream = new ObjectInputStream(socketToServer.getInputStream());
myOutputStream.writeObject(communicationObject);
communicationObject = (DataObject)myInputStream.readObject();
balance = communicationObject.getMessage();
myOutputStream.close();
myInputStream.close();
socketToServer.close(); }
catch(Exception e){System.out.println(e);}
Integer oldbalance = Integer.valueOf(balance);
Integer newbalanceamount = oldbalance - w_amt;
balance2 = "" + newbalanceamount;
String query2 = "UPDATE ATM SET balance='"+balance2+"' WHERE account_number='"+account+"' AND pin='"+pass+"'";
try {
communicationObject = new DataObject();
communicationObject.setMessage(query2);
Socket socketToServer = new Socket("sunlab32.njit.edu",31414);
ObjectOutputStream myOutputStream = new ObjectOutputStream(socketToServer.getOutputStream());
ObjectInputStream myInputStream = new ObjectInputStream(socketToServer.getInputStream());
myOutputStream.writeObject(communicationObject);
communicationObject = (DataObject)myInputStream.readObject();
//balance = communicationObject.getMessage();
myOutputStream.close();
myInputStream.close();
socketToServer.close(); }
catch(Exception e){System.out.println(e); }
withdrawField.setText("");
empLayout.first(empcenterPanel); }
else if(event.getSource() == depositButton) {
empLayout.show(empcenterPanel, EMPDEP); }
else if(event.getSource() == empdepositButton) {
deposit_amount = depositField.getText();
Integer d_amt = Integer.valueOf(deposit_amount);
String query = "SELECT balance FROM ATM a WHERE a.account_number='"+account+"' AND a.pin='"+pass+"'";
try {
communicationObject = new DataObject();
communicationObject.setMessage(query);
Socket socketToServer = new Socket("sunlab32.njit.edu",31414);
ObjectOutputStream myOutputStream = new ObjectOutputStream(socketToServer.getOutputStream());
ObjectInputStream myInputStream = new ObjectInputStream(socketToServer.getInputStream());
myOutputStream.writeObject(communicationObject);
communicationObject = (DataObject)myInputStream.readObject();
balance = communicationObject.getMessage();
myOutputStream.close();
myInputStream.close();
socketToServer.close(); }
catch(Exception e){System.out.println(e);}
Integer oldbalance2 = Integer.valueOf(balance);
Integer newbalanceamount2 = oldbalance2 + d_amt;
balance3 = "" + newbalanceamount2;
String query2 = "UPDATE ATM SET balance='"+balance3+"' WHERE account_number='"+account+"' AND pin='"+pass+"'";
try {
communicationObject = new DataObject();
communicationObject.setMessage(query2);
Socket socketToServer = new Socket("sunlab32.njit.edu",31414);
ObjectOutputStream myOutputStream = new ObjectOutputStream(socketToServer.getOutputStream());
ObjectInputStream myInputStream = new ObjectInputStream(socketToServer.getInputStream());
myOutputStream.writeObject(communicationObject);
communicationObject = (DataObject)myInputStream.readObject();
//balance = communicationObject.getMessage();
myOutputStream.close();
myInputStream.close();
socketToServer.close(); }
catch(Exception e){System.out.println(e);}
depositField.setText("");
empLayout.first(empcenterPanel); }
else if(event.getSource() == transferButton) {
empLayout.show(empcenterPanel, EMPTNS); }
else if(event.getSource() == transferamountButton) {
fromAccount = fromField.getText();
toAccount = toField.getText();
transAmount = transferAmount.getText();
Integer t_amt = Integer.valueOf(transAmount);
String query = "SELECT balance FROM ATM a WHERE a.account_number='"+fromAccount+"'";
try {
communicationObject = new DataObject();
communicationObject.setMessage(query);
Socket socketToServer = new Socket("sunlab32.njit.edu",31414);
ObjectOutputStream myOutputStream = new ObjectOutputStream(socketToServer.getOutputStream());
ObjectInputStream myInputStream = new ObjectInputStream(socketToServer.getInputStream());
myOutputStream.writeObject(communicationObject);
communicationObject = (DataObject)myInputStream.readObject();
balance = communicationObject.getMessage();
myOutputStream.close();
myInputStream.close();
socketToServer.close(); }
catch(Exception e){System.out.println(e);}
Integer fromBalance = Integer.valueOf(balance);
String query2 = "SELECT balance FROM ATM a WHERE a.account_number='"+toAccount+"'";
try {
communicationObject = new DataObject();
communicationObject.setMessage(query2);
Socket socketToServer = new Socket("sunlab32.njit.edu",31414);
ObjectOutputStream myOutputStream = new ObjectOutputStream(socketToServer.getOutputStream());
ObjectInputStream myInputStream = new ObjectInputStream(socketToServer.getInputStream());
myOutputStream.writeObject(communicationObject);
communicationObject = (DataObject)myInputStream.readObject();
balance = communicationObject.getMessage();
myOutputStream.close();
myInputStream.close();
socketToServer.close(); }
catch(Exception e){System.out.println(e);}
Integer toBalance = Integer.valueOf(balance);
Integer newFromBalance = fromBalance - t_amt;
Integer newToBalance = toBalance + t_amt;
String newFBalance = "" + newFromBalance;
String newTBalance = "" + newToBalance;
String query3 = "UPDATE ATM SET balance='"+newFBalance+"' WHERE account_number='"+fromAccount+"'";
try {
communicationObject = new DataObject();
communicationObject.setMessage(query3);
Socket socketToServer = new Socket("sunlab32.njit.edu",31414);
ObjectOutputStream myOutputStream = new ObjectOutputStream(socketToServer.getOutputStream());
ObjectInputStream myInputStream = new ObjectInputStream(socketToServer.getInputStream());
myOutputStream.writeObject(communicationObject);
communicationObject = (DataObject)myInputStream.readObject();
//balance = communicationObject.getMessage();
myOutputStream.close();
myInputStream.close();
socketToServer.close(); }
catch(Exception e){System.out.println(e);}
String query4 = "UPDATE ATM SET balance='"+newTBalance+"' WHERE account_number='"+toAccount+"'";
try {
communicationObject = new DataObject();
communicationObject.setMessage(query4);
Socket socketToServer = new Socket("sunlab32.njit.edu",31414);
ObjectOutputStream myOutputStream = new ObjectOutputStream(socketToServer.getOutputStream());
ObjectInputStream myInputStream = new ObjectInputStream(socketToServer.getInputStream());
myOutputStream.writeObject(communicationObject);
communicationObject = (DataObject)myInputStream.readObject();
//balance = communicationObject.getMessage();
myOutputStream.close();
myInputStream.close();
socketToServer.close(); }
catch(Exception e){System.out.println(e);}
toField.setText("");
fromField.setText("");
transferAmount.setText("");
empLayout.first(empcenterPanel); }
else if(event.getSource() == createacctButton) {
empLayout.show(empcenterPanel, CREATE); }
else if(event.getSource() == createButton) {
createLogin = createloginField.getText();
createPin = createpinField.getText();
createLvl = createlvlField.getText();
createBalance = createbalanceField.getText();
String query = "INSERT INTO ATM VALUES ("+createLogin+","+createPin+","+createLvl+","+createBalance+")";
try {
communicationObject = new DataObject();
communicationObject.setMessage(query);
Socket socketToServer = new Socket("sunlab32.njit.edu",31414);
ObjectOutputStream myOutputStream = new ObjectOutputStream(socketToServer.getOutputStream());
ObjectInputStream myInputStream = new ObjectInputStream(socketToServer.getInputStream());
myOutputStream.writeObject(communicationObject);
communicationObject = (DataObject)myInputStream.readObject();
myOutputStream.close();
myInputStream.close();
socketToServer.close(); }
catch(Exception e){System.out.println(e);}
createloginField.setText("");
createpinField.setText("");
createlvlField.setText("");
createbalanceField.setText("");
empLayout.first(empcenterPanel); }
else if(event.getSource() == listallButton) {
String query = "SELECT * FROM ATM";
String delimiter = ",";
String input = "go";
int count=0;
al = new ArrayList<String>();
try {
communicationObject = new DataObject();
communicationObject.setMessage(query);
Socket socketToServer = new Socket("sunlab32.njit.edu",31414);
ObjectOutputStream myOutputStream = new ObjectOutputStream(socketToServer.getOutputStream());
ObjectInputStream myInputStream = new ObjectInputStream(socketToServer.getInputStream());
myOutputStream.writeObject(communicationObject);
communicationObject = (DataObject)myInputStream.readObject();
input = communicationObject.getMessage();
if (input != "stop") {
al.add(input);
data[count] = input;
count++; }
for (int i=0;i<data.length;i++) {
row = data[i];
temp = row.split(delimiter);
tbm.addRow(new String[] {temp[0],temp[1],temp[2],temp[3]}); }
table.setModel(tbm);
table.repaint();
//Object Object_Array[]=al.toArray();
//data=new String[Object_Array.length];
//for (int i=0;i<data.length;i++) {data[i]=Object_Array[i].toString();}
myOutputStream.close();
myInputStream.close();
socketToServer.close(); }
catch(Exception e){System.out.println(e);}
empLayout.show(empcenterPanel, LIST); }
} }
堆栈跟踪:
java.lang.NullPointerException at AtmView.actionPerformed(AtmView.java:757) at
javax.swing.AbstractButton.fireActionPerformed(Unknown Source) at
javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source) at
javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source) at
javax.swing.DefaultButtonModel.setPressed(Unknown Source) at
javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source) at
java.awt.Component.processMouseEvent(Unknown Source) at
javax.swing.JComponent.processMouseEvent(Unknown Source) at
//...
java.awt.EventQueue.dispatchEventImpl(Unknown Source) at
java.awt.EventQueue.access$000(Unknown Source) at
java.awt.EventQueue$3.run(Unknown Source) at
java.awt.EventQueue$3.run(Unknown Source) at