This discussion is archived
8 Replies Latest reply: Nov 25, 2007 3:23 PM by 807603 RSS

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

807603 Newbie
Currently Being Moderated
I build this 2 echo programs in netbeans an I got the same errors on both of them ("Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0 on EchoingClient ec = new EchoingClient(args[0],Integer.parseInt(args[1]));"").I'm new in java programming and i don't know how to fix it.Sry for my english.


Client Programm

import java.net.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;

class EchoingClient extends Frame implements ActionListener
{
     TextField tf = new TextField(50);
     Button btn = new Button("Click");
     Button exit = new Button("Exit");
     TextArea ta = new TextArea(50,20);
     String hostName;
     int Port;

     public static void main(String args[]) throws Exception
     {
          EchoingClient ec = new EchoingClient(args[0],Integer.parseInt(args[1]));
     }

     EchoingClient(String hName, int prt)
     {
          Panel p = new Panel();
          add(p,BorderLayout.NORTH);
          p.add(tf);
          p.add(btn);
          p.add(exit);
          btn.addActionListener(this);
          exit.addActionListener(this);
          add(ta,BorderLayout.CENTER);
          setSize(700,200);
          show();
          hostName = hName;
          Port = prt;
     }


     public void actionPerformed(ActionEvent ae)
     {
          if(ae.getSource() == btn)
          {
               try
               {
                    Socket s = new Socket(hostName,Port);
                    DataOutputStream dos = new DataOutputStream(s.getOutputStream());
                    DataInputStream dis = new DataInputStream(s.getInputStream());
                    dos.writeUTF(tf.getText());
                    ta.append("Received From Server : " + dis.readUTF() + "\n");
                    tf.setText("");
                    dos.close();
               }
               catch(Exception e)
               {
                    System.out.println(e.getMessage());
               }     
          }
          else
          {
               System.exit(0);
          }
     }

}


run-single:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at EchoingClient.main(EchoingClient.java:24)



Server Program

import java.net.*;
import java.io.*;

class EchoingServer
{
     public static void main(String args[]) throws Exception
     {
          ServerSocket ss = new ServerSocket(Integer.parseInt(args[0]));

          while(true)
          {
               Socket s = ss.accept();
               (new EchoingServerThread(s)).start();
          }
     }
}


class EchoingServerThread extends Thread
{
     Socket soc;

     EchoingServerThread(Socket c)
     {
          soc=c;
     }

     public void run()
     {
          try
          {
               DataInputStream dis = new DataInputStream(soc.getInputStream());
               DataOutputStream dos = new DataOutputStream(soc.getOutputStream());
               String getLine = dis.readUTF();
               System.out.println("Received \"" + getLine + "\" From : " + soc.getInetAddress().getHostName());
               dos.writeUTF(getLine);
               dis.close();
               dos.close();
               soc.close();
          }
          catch(Exception e)
          {
          }
     }
}
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at EchoingServer.main(EchoingServer.java:15)
Java Result: 1
  • 1. Re: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
    807603 Newbie
    Currently Being Moderated
    Welcome to the forum. You have posted quite a bit of unformatted code which unfortunately is hard to read. I recommend that you repost that code but format it first:

    After you have pasted the code into the forum messange window, highlight the code, then press the "code" button in the message window toolbar. Alternatively, you could place code tags around your code. Place the tag [code] on top of your code and [/code] on the bottom of your code will work too like so:
    [code]
      // your code here
    [/code]
    Finally, when you run your program, are you providing args? That's why it seems you're getting the errors, no args or parameters (I keep getting the two confused) when calling the program. If calling from the os, it's easy as java yourprogram arg1 arg2. I'm not sure how to do this in netbeans.

    Edited by: petes1234 on Nov 24, 2007 8:16 AM
  • 2. Re: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
    616040 Newbie
    Currently Being Moderated
    When you run the program you have to supply run time parameters (in this case 2 parameters if you want to use args[0] and args[1]), by the looks of your program these are the hostname and the port.
  • 3. Re: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
    807603 Newbie
    Currently Being Moderated
    HI I also have this problem just wondering what u mean by set runtime parameters
    Do mean ask the user at run time?
    im looking to use the String []args(my hostname and port number) and use them outside the main

    public static void main(String args[])
    {

    int portnum = Integer.valueOf(args[1]).intValue(); THIS IS MY ERROR!!!
    String host = args[0];
    GUIClass ob = new GUIClass();
    ob.meth(host,portnum);
    try
    {
    if (args.length == 2)
    {
    return;
    }
    }
    catch (Exception err)
    {
    System.out.println("Error : " + err.getMessage());
    }

    When i run it returns this error :
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1

    Any help would be greatly appreciated
    Thanks
  • 4. Re: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
    616040 Newbie
    Currently Being Moderated
    When you actually start the program you have to do it at the command line and give it the parameters of hostname and portnumber. Not sure of the exact syntax for this, but it would be something like:

    ProgramName.exe -hostname -port
  • 5. Re: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
    DarrylBurke Guru Moderator
    Currently Being Moderated
    {color:0000ff}http://java.about.com/library/weekly/aa_methods2.htm{color}

    Read the section Passing Arguments to main()

    db
  • 6. Re: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
    807603 Newbie
    Currently Being Moderated
    The program should start through command line
    java -jar client localhost 3001( host and portnumber(my arguements)
    im trying to put them in my action Performed method so when i hit sumbit in my GUI it will create the new socket and send info to a server and recieve info also.

    Thanks for the information
    Al
  • 7. Re: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
    807603 Newbie
    Currently Being Moderated
    I fixed the problem, The main error what is happening is

    Error : connect: Address is invalid on local machine, or port is not valid on remote machine

    Does anyone know what this means?

    Thanks
    Alan
    any help would be greatly appreicated
  • 8. Re: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
    807603 Newbie
    Currently Being Moderated
    //CLIENT
    import java.net.*;
    import java.io.*;
    import java.awt.*;
    import java.awt.event.*;
    
    class EchoingClient extends Frame implements ActionListener
    {
    TextField tf = new TextField(50);
    Button btn = new Button("Click");
    Button exit = new Button("Exit");
    TextArea ta = new TextArea(50,20);
    String hostName;
    int Port;
    
    public static void main(String args[]) throws Exception
    {
    EchoingClient ec = new EchoingClient(args[0],Integer.parseInt(args[1]));
    }
    
    EchoingClient(String hName, int prt)
    {
    Panel p = new Panel();
    add(p,BorderLayout.NORTH);
    p.add(tf);
    p.add(btn);
    p.add(exit);
    btn.addActionListener(this);
    exit.addActionListener(this);
    add(ta,BorderLayout.CENTER);
    setSize(700,200);
    show();
    hostName = hName;
    Port = prt;
    }
    
    public void actionPerformed(ActionEvent ae)
    {
    if(ae.getSource() == btn)
    {
    try
    {
    Socket s = new Socket(hostName,Port);
    DataOutputStream dos = new DataOutputStream(s.getOutputStream());
    DataInputStream dis = new DataInputStream(s.getInputStream());
    dos.writeUTF(tf.getText());
    ta.append("Received From Server : " + dis.readUTF() + "\n");
    tf.setText("");
    dos.close();
    }
    catch(Exception e)
    {
    System.out.println(e.getMessage());
    }
    }
    else
    {
    System.exit(0);
    }
    }
    
    }
    import java.net.*;
    import java.io.*;
    
    class EchoingServer
    {
    public static void main(String args[]) throws Exception
    {
    ServerSocket ss = new ServerSocket(Integer.parseInt(args[0]));
    
    while(true)
    {
    Socket s = ss.accept();
    (new EchoingServerThread(s)).start();
    }
    }
    }
    
    class EchoingServerThread extends Thread
    {
    Socket soc;
    
    EchoingServerThread(Socket c)
    {
    soc=c;
    }
    
    public void run()
    {
    try
    {
    DataInputStream dis = new DataInputStream(soc.getInputStream());
    DataOutputStream dos = new DataOutputStream(soc.getOutputStream());
    String getLine = dis.readUTF();
    System.out.println("Received \"" + getLine + "\" From : " + soc.getInetAddress().getHostName());
    dos.writeUTF(getLine);
    dis.close();
    dos.close();
    soc.close();
    }
    catch(Exception e)
    {
    }
    }
    }
    Thanks guys for help.I will try to see if it woks.