By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,640 Members | 1,510 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,640 IT Pros & Developers. It's quick & easy.

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0 at Por

P: 17
Expand|Select|Wrap|Line Numbers
  1. import java.net.*;
  2. public class PortScan
  3. {
  4.     public static void main(String args[]) throws Exception
  5.     {
  6.         int s1,s2,in;
  7.         s1=Integer.parseInt(args[0]);
  8.         System.out.println("Start Port No is " +s1);
  9.         s2=Integer.parseInt(args[1]);
  10.         System.out.println("End Port No. is " +s2);
  11.         InetAddress i=InetAddress.getLocalHost();
  12.         System.out.println("InetAddress is "+i);
  13.         System.out.println("IP Address is "+i);
  14.         System.out.println("Active ports are ");
  15.         for(in=0;in<s2;in++)
  16.         {
  17.             try
  18.             {
  19.                 Socket s=new Socket(i,in);
  20.                 System.out.println(s);
  21.             }
  22.             catch(Exception e)
  23.             {
  24.             }
  25.         }
  26.     }
  27. }
Apr 4 '13 #1

✓ answered by r035198x

The program accesses args[0] and args[1] so you need to run it by providing those values on the command line. Also don't do

Expand|Select|Wrap|Line Numbers
  1. catch(Exception e)
  2.             {
  3.             }
That just hides exceptions if they occur which is very bad.

Share this Question
Share on Google+
2 Replies


10K+
P: 13,264
The program accesses args[0] and args[1] so you need to run it by providing those values on the command line. Also don't do

Expand|Select|Wrap|Line Numbers
  1. catch(Exception e)
  2.             {
  3.             }
That just hides exceptions if they occur which is very bad.
Apr 4 '13 #2

100+
P: 158
Hey neeru, Its because you're not passed any argument to the main function while executing the program. Your program takes first two arguments as input and assigning it to s1 and s2 variables after converting it to integer (In line number 7 and line number 9). To avoid this error before line number 7 check whether the argument length is greater than or equal to 2 or not (Because you need atleast two arguments).

Expand|Select|Wrap|Line Numbers
  1. if(args.length >= 2){ // Before line number 7 in your code.
  2. }//Close this if statement after line number 25 in your code.
Apr 4 '13 #3

Post your reply

Sign in to post your reply or Sign up for a free account.