Why am I getting an error when I try to run my socket program in Java?

You have to specify the port number at the command line It is always useful to know how to read the logs See. In your logs you have a bunch of class names, but there is only one related to your code: Exception in thread "main" java.lang. ArrayIndexOutOfBoundsException: 0 at ServidorTCP.

Main(ServidorTCP. Java:21) The rest of them are all about java. Lang, java.

Util, java. Io, etc, classes that you didn't coded If you look at that error line it says: java.lang. ArrayIndexOutOfBoundsException: That means that you attempted to access an array out of its bounds That is if the array size is 5 you probably attempted to access the 6th element.

That's illegal, that's an exception ( more formally a RuntimeException that means you have a programming mistake ) Then the log adds information where that happened at ServidorTCP. Main(ServidorTCP. Java:21) In the class ServidorTCP ( in java file ServidorTCP at line 21 You can take a look at that file at that line this is what it shows: 20: // args 0 is the port number to be heard 21: int puerto = new Integer(args0).intValue() That says, the value of the variable "puerto" will be a new Integer created with the content of args0 That is the first element of the args array That array is defined in the main method: public static void main(String args){ And filled automatically with the command line arguments So, if you ran your program without providing an argument the search of index 0 in the array args will fail I don't know exactly where in jcreator you can add command line arguments, but I'm pretty sure somewhere in a "Run" menu, or in the "Run" configuration or something like that, there should be something like "program arguments" You can fill that value with a port number, try using 8085 OR You can also change the line 21 of the server and initialize it like this: int puerto = new Integer(args0).intValue(); int puerto = 8085; // por mis pistolas :) And see it run Espero que esto te ayude Chau!

You have to specify the port number at the command line. It is always useful to know how to read the logs. See.In your logs you have a bunch of class names, but there is only one related to your code: Exception in thread "main" java.lang.

ArrayIndexOutOfBoundsException: 0 at ServidorTCP. Main(ServidorTCP. Java:21) The rest of them are all about java.

Lang, java. Util, java.Io, etc, classes that you didn't coded. If you look at that error line it says: java.lang.

ArrayIndexOutOfBoundsException: That means that you attempted to access an array out of its bounds. That is if the array size is 5 you probably attempted to access the 6th element. That's illegal, that's an exception ( more formally a RuntimeException that means you have a programming mistake ) Then the log adds information where that happened.

At ServidorTCP. Main(ServidorTCP. Java:21) In the class ServidorTCP ( in java file ServidorTCP at line 21.

You can take a look at that file at that line this is what it shows: 20: // args 0 is the port number to be heard 21: int puerto = new Integer(args0).intValue(); That says, the value of the variable "puerto" will be a new Integer created with the content of args0 That is the first element of the args array. That array is defined in the main method: public static void main(String args){ And filled automatically with the command line arguments. So, if you ran your program without providing an argument the search of index 0 in the array args will fail.

I don't know exactly where in jcreator you can add command line arguments, but I'm pretty sure somewhere in a "Run" menu, or in the "Run" configuration or something like that, there should be something like "program arguments" You can fill that value with a port number, try using 8085. OR You can also change the line 21 of the server and initialize it like this: //int puerto = new Integer(args0).intValue(); int puerto = 8085; // por mis pistolas :) And see it run. Espero que esto te ayude.

Chau!

Sounds like when you're starting the server you're not giving it any command-line arguments. In other words, you're running something like: java -cp . ServidorTCP when you should be running java -cp .

ServidorTCP 40000 (to start the server on port 40000) If you're starting the program from the IDE, you need to find out how to pass in arguments when it starts the program.

Since its an ArrayIndexOutOfBounds exception, I would start looking at where you are using arrays. So, in your case, its where you are trying to grab the port number from the array of arguments. So take a look at that first, and make sure you are passing a port number in as a command line argument.

Look the signature of your main() method: public static void main(String args) The error is here: int puerto = new Integer(args0).intValue(); If you invoke your program by passing no arguments, it'll try to read an integer from an array that is in fact empty. If you doesn't want to care about parameters, just do this: int arguments = new Integer (args. Length); if (arguments = 0) { this.

Puerto = 4000; } That should do the job. If no arguments are passed, it'll just assume port 4000 (or whatever port you choose) as the default port.

I am getting an error when I try to run my socket program in java because (without quotes):.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions