Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.

Java
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);
   }
}
Posted
Updated 14-Dec-13 5:15am
v3

1 solution

At a guess it is happening in these lines:
Java
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]);

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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900