Computer Science Canada

ArrayIndexOutOfBoundsException 1 error while running a socket programming code

Author:  shwetakode [ Sun Sep 11, 2011 1:18 pm ]
Post subject:  ArrayIndexOutOfBoundsException 1 error while running a socket programming code

Hi,
I have written a socket programming code which reads a line from the user and sends to server character by character in the below format DATA seq_no char
eg : DATA1A (seq no : 1 , char = 'A')
it works fine for 1st character but getting exception for 2nd character.


Getting error at line : --- god3[i+5]= god1[i]; Please help.


package again1;
import java.io.*;
import java.net.*;



/**
*
* @author shweta
*/
public class Again1 {

/**
* @param args the command line arguments
*/
public static void main(String[] args) throws Exception {
// TODO code application logic here

String hostName = null;
int servPort = 12346;

try
{
hostName = args[0];
}
catch(ArrayIndexOutOfBoundsException e) {
System.out.println("Need argument: remoteHost1234");
System.exit(-1);
}



BufferedReader buffer = new BufferedReader(new InputStreamReader(System.in));
DatagramSocket clientSocket = new DatagramSocket();

int i=0,j=0;

String sentence = null;
System.out.println("Enter the string");
sentence = buffer.readLine();
InetAddress IPAddress = InetAddress.getByName(hostName);
byte[] god3 = new byte[6];
byte[] god1 = new byte[6];

god3[0] = 'D';
god3[1]= 'A';
god3[2]= 'T';
god3[3]= 'A';

for(i=0;i<sentence.length();i++)
{

char c = sentence.charAt(i);
System.out.println("The char expected is:"+c);
String god = Character.toString(c);
System.out.println("\n");
System.out.println("The String is"+god);
System.out.println("Before god1:"+god1.toString());
god1 = god.getBytes();

for(j=0;j<god1.length;j++)
{
System.out.println("After god1:"+god1[j]);
System.out.println("\n");
}



/*god3[i+4] = (byte)i;*/

try
{
god3[4] = (byte)i;
god3[i+5]= god1[i];
}
catch (ArrayIndexOutOfBoundsException e1)
{
System.out.println("Index beyond range");
System.exit(-1);
}
System.out.println("\n");
System.out.println("the sequence is:"+god3[4]);
System.out.println("\n");
System.out.println("the byte of char is:"+god3[5]);
System.out.println("\n");

String strIP = new String(god1);
String strIP3 = new String(god3);
System.out.println("the string is:"+strIP);
System.out.println("\n");
System.out.println("the string appended with DATA is:"+strIP3);

System.out.println("\n");


DatagramPacket sendPacket = new DatagramPacket(god3,god3.length,IPAddress,servPort);
clientSocket.send(sendPacket);


}
}
}

Author:  Tony [ Sun Sep 11, 2011 1:57 pm ]
Post subject:  RE:ArrayIndexOutOfBoundsException 1 error while running a socket programming code

ArrayIndexOutOfBoundsException happens when you attempt to access an array element at an index that does not exist (e.g. if you have an array of 10 elements, and then try to assign something to the 15th spot).

Author:  S_Grimm [ Sun Sep 11, 2011 10:37 pm ]
Post subject:  Re: ArrayIndexOutOfBoundsException 1 error while running a socket programming code

try replacing your arrays with Vectors of type Byte or Character.
Vectors automatically scale when you attempt to add more data to them.

Example of Vectors:

code:
import java.util.Vector;

public class VectorTest {
       
        public static void main(String[]args){

        Vector<Character> charVector = new Vector<Character>();
       
        charVector.addElement('a');
        charVector.addElement('b');
        charVector.addElement('c');
        charVector.addElement('d');
        charVector.addElement('e');
        charVector.addElement('f');
        charVector.addElement('g');
       
        for( int x = 0; x < charVector.size(); ++x)
        {
                //code to send vector element
                System.out.println("Element at " + charVector.elementAt(x));
        }
       
        }
}


Also check what you reading at your line of code
code:
god3[i+5]= god1[i];


If your array is only 6 elements long and you attempt to add and element at god3[i+5] where i == 2........ 5+2 = 7...... god3[7] doesnt exist in an array of 6 elements....
Now you'll notice that in an Vector, i don't specify a size, because there isn't a need in this case. Especially if your reading user input then sending it as characters, you want the Storage Container to expand to the size of the user input. If you want to delete the data after each input use the command
code:
removeAllElements()

Author:  DemonWasp [ Mon Sep 12, 2011 8:00 am ]
Post subject:  RE:ArrayIndexOutOfBoundsException 1 error while running a socket programming code

Though if you're going to use a Vector in single-threaded code, you should probably just use an ArrayList instead.

Author:  S_Grimm [ Mon Sep 12, 2011 8:20 am ]
Post subject:  Re: RE:ArrayIndexOutOfBoundsException 1 error while running a socket programming code

DemonWasp @ Mon Sep 12, 2011 8:00 am wrote:
Though if you're going to use a Vector in single-threaded code, you should probably just use an ArrayList instead.


Ahh yes. I didn't think of that. All my machines run quad cores xD

Author:  Tony [ Mon Sep 12, 2011 8:44 am ]
Post subject:  Re: RE:ArrayIndexOutOfBoundsException 1 error while running a socket programming code

AV @ Mon Sep 12, 2011 8:20 am wrote:
All my machines run quad cores xD

So you always write multi-threaded code? Laughing

But this is getting rather off-topic. OP doesn't need to scale the array size, as the "sentence" is read just once, and before all the inits. What would help more is to properly name the variables, so that the code is actually readable.


: