java .lang。 arrayindexoutofbound ... [英] java .lang. arrayindexoutofbound...

查看:61
本文介绍了java .lang。 arrayindexoutofbound ...的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

上面的错误是什么意思,每当我想将帐户余额更新为m,y数据库时,就会出现上述错误,我不知道错误发生在哪里。感谢您的任何帮助。



what is meant by the error above, everytime i want to update the account balance into m,y database, the above error occur, i have no idea on where the error occur. Thank you for any help.

private void submitActionPerformed(java.awt.event.ActionEvent evt) {                                       
   try{
      Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
      conn = DriverManager.getConnection("jdbc:odbc:BankSystem");
      select = "Select * From SingaporeWithdraw where AccountNo = ?;";
      update = "Update SingaporeWithdraw Set Balance = ? where AccountNo = ?";
      stmtSelect = conn.createStatement();
      stmtUpdate = conn.createStatement();
      pstmtSelect = conn.prepareStatement(select);
      pstmtUpdate = conn.prepareStatement(update);

      //get value from textField and pass it into prepareStatement
      String accNo = accNumber.getText();
      //this is to set the accNo into ?
      pstmtSelect.setString(1, accNo);
      rsSelect = pstmtSelect.executeQuery();

      String branch = null;
      while(rsSelect.next()){
         //custNameText.setText(rsSelect.getString("CustName"));
         // jlblbalance.setText(rsSelect.getString("Balance"));
         branch = rsSelect.getString("Branch");
      }
      if(branch.equalsIgnoreCase("Singapore")){
         double balance = Double.parseDouble(jlblbalance.getText());
         double result = withdraw(balance);
         //newResult is the latest balance
         String newResult = String.valueOf(result);
         jlblbalance.setText(newResult);
         long ref = (long)(Math.random()*10000000);
         jlblRef.setText(String.valueOf(ref));

         pstmtUpdate.setString(4, newResult);
         rsUpdate = pstmtUpdate.executeQuery();
      }
      else{
         JOptionPane.showMessageDialog(null, "Sorry the customer details is not in this branch. The information shall be forwsrded to related branch.");
         String allInfo = accNumber.getText()+","+amountText.getText()+","+branchCombo.getSelectedItem().toString();

         //from malaysia
         byte [] dataReceived = new byte[65536];
         //to thailand
         byte [] sendData = new byte[65536];

         //from malaysia
         DatagramPacket packetReceived = new DatagramPacket(dataReceived, dataReceived.length);
         //to thailand
         DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length);

         //for data from malaysia use
         DatagramSocket myMalServer = new DatagramSocket(100);
                
         //for data to thailand use
         DatagramSocket myThaiServer = new DatagramSocket(200);

         //receive "On" from malaysia
         myMalServer.receive(packetReceived);
         String onMsg = new String (packetReceived.getData(),0,packetReceived.getLength());
         System.out.println(onMsg);
                
         //receive codes here from malaysia
         myMalServer.receive(packetReceived);
         String serverMsg = new String(packetReceived.getData(), 0, packetReceived.getLength());
         String [] text = serverMsg.split(",");
         //continue to split the message here
         accNumber.setText(text[0]);
         amountText.setText(text[1]);
         jlblbranch.setText(text[2]);
                
         //process all the info if its for singapore
         String correctBranch = jlblbranch.getText();
         if(correctBranch.equalsIgnoreCase("Singapore")){
            rsSelect = pstmtSelect.executeQuery();
            double balance = Double.parseDouble(jlblbalance.getText());
            double result = withdraw(balance);
            String newResult = String.valueOf(result);
            jlblbalance.setText(newResult);
            double ref = (double)(Math.random()*10000);
            jlblRef.setText(String.valueOf(ref));
            rsUpdate = pstmtUpdate.executeQuery();
         }
         else{
            //to establish connection with thailand
            InetAddress destAddress = InetAddress.getByName("localhost");
            String SingaporeMsg = "On";
            sendData = SingaporeMsg.getBytes();
            sendPacket.setData(sendData);
            sendPacket.setAddress(destAddress);
            sendPacket.setPort(100);
            myThaiServer.send(sendPacket);

            //after establishing connection, send all related information to thailand
            sendData = allInfo.getBytes();
            sendPacket.setData(sendData);
            sendPacket.setAddress(packetReceived.getAddress());
            sendPacket.setPort(packetReceived.getPort());
            myThaiServer.send(sendPacket);
         }

         myMalServer.close();
         myThaiServer.close();
      }
   }
   catch(Exception ex){
   JOptionPane.showMessageDialog(null, ex);
   }
}

推荐答案

据猜测,这些行正在发生:

At a guess it is happening in these lines:
String serverMsg = new String(packetReceived.getData(), 0, packetReceived.getLength());
String [] text = serverMsg.split(",");
//continue to split the message here
accNumber.setText(text[0]);
amountText.setText(text[1]);
jlblbranch.setText(text[2]);



您假设 text 包含三个元素,但您尚未检查以确保它实际上。永远不要假设您的代码始终有效,请检查所有内容。您还应该花一些时间研究数组及其索引,以便您了解此异常只是告诉您正在尝试访问不存在的数组元素。


You are assuming that text contains three elements, but you have not checked to make sure it actually does. Never assume that your code will always work, check everything. You should also spend some time studying arrays and their indices so you understand that this exception is merely telling you that you are trying to access an element of an array that does not exist.


这篇关于java .lang。 arrayindexoutofbound ...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆