2012-05-01 60 views
0

我在从“listallButton”中检索到的数据正确显示到JTable中时遇到问题。数据库正在发送SELECT * FROM ATM信息作为一组字符串。我不知道,如果它被正确地接收和记录为JTable中没有显示数据,并得到一个NullPointerException异常:从Oracle DB加载Java JTable数据库

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 
+0

堆栈跟踪?空指针异常在哪里发生? – ChadNC

+0

即时通讯不知道如何StackTrace ...所有我在控制台中看到的是java.lang.NullPointerException ...当我用JTable加载面板时发生错误 – paul62285

+0

@ChadNC java.lang.NullPointerException \t at AtmView.actionPerformed (AtmView.java:757) \t在javax.swing.AbstractButton.fireActionPerformed(来源不明) \t在javax.swing.AbstractButton中的$ Handler.actionPerformed(来源不明) \t在javax.swing.DefaultButtonModel.fireActionPerformed(来源不明) \t在javax.swing.DefaultButtonModel.setPressed(未知来源) \t在javax.swing.plaf.basic.BasicButtonListener.mouseReleased(未知叟RCE) \t在java.awt.Component.processMouseEvent(未知来源) \t在javax.swing.JComponent.processMouseEvent(未知来源) – paul62285

回答

1

零点异常可能是由一些空所致DB中的列。你的做法不好,如果数据库中有大量记录,它会使程序崩溃,更好的方法是使用分页来避免加载大量记录。

+0

我的数据库只有5条记录......问题是表没有向表中绘制任何记录...感兴趣的面板是listallpanel, listallButton – paul62285

+0

使用'e.printStackTrace(System.out);'并发布错误跟踪,您提供的信息不足以识别错误。 – user1335794

+0

在程序中,我添加了printStackTrace?每个“catch”块都有一个 – paul62285

0

你的问题是你还没有初始化你的字符串数组data

这个实例变量首先访问正在这里进行的查询执行

if (input != "stop") { 
    al.add(input); 
    data[count] = input; 
    count++; 
} 

后,此时数据为空,并且您试图访问因此它的第0个元素NPE。

在一个侧面说明:

不要在EventModelThread做这样大量的工作,使用SwingWorker的执行数据加载操作,然后按在UI的变化。 Swing是单线程的,当您执行数据加载时会阻止用户界面。

虽然在目前的情况下它不会有太大的影响,考虑到你只有5行,但万一你的行像1000说左右,你的用户界面将无法响应。另外,不要期望人们在一页中看到所有1000行,并有分页。

Pagination Sample Code

Swing Worker Tutorial

+0

谢谢!我把循环移到try/catch后面,它起作用 – paul62285

+0

很高兴能有帮助:)请接受答案;)如果你想要,你也可以赞成它:) – mprabhat