473,395 Members | 2,253 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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

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.

2 2969
r035198x
13,262 8TB
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
vijay6
158 100+
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

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

Similar topics

11
by: Lem | last post by:
I get the error Exception in thread "main" java.lang.NoClassDefFoundError when I type java app2 in the command prompt. I've tried moving to the jre directory and typed java c:\app2\app2, but it...
1
by: CM | last post by:
Hi, when i want connect me in my BD with a JSP (with this simple code), this exception is throw. Thank's for ur help Mathieu CODE of my JSP ---------------------
6
by: ganesh.m | last post by:
Hi, I am new to DB2. I am getting this error while loading the DB2Driver. I don't have any idea about where i might have gone wrong. please help me. Below is the stack trace. Stack Trace:...
22
oll3i
by: oll3i | last post by:
i get Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2 at Producent.main(producent.java:605) when i run it from bat @start "Supply Chain Management-Producer to...
6
by: Light | last post by:
Hi I m getting this error while i m trying to run my program and i don't understand why that happens. import java.util.*; public class Exercise6 {
4
by: jmitch89 | last post by:
I don't why I get this error: Exception in thread "main" java.lang.NoClassDefFoundError The statement below works just fine: java -cp...
1
by: shaikhussain | last post by:
public class EmployeeServicesTestCase { /** * @param args */ public static void main(String s)throws Exception { // TODO Auto-generated method stub BeanFactory beans=new...
2
by: David Rothabuer | last post by:
I work with an integration engine that is java based. All my scripting is javascripting and I've run into a problem that I can't find a solution to. I am getting this error when I attempt to read...
1
by: Buena Velasco | last post by:
I'm having trouble finding the error on my code. I know what "Exception thread ... ArrayIndexOutOfBounds... " means but I couldn't tell which part in the looping it is or in other. import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.