java - ArrayIndexOutOfBoundExcxeption -


this client side of multiple chat application. reason, first line in main function, tried create new chat client giving me array index out of bounds exception. please know if there doing wrong. appreciated. below whole code client side, have issue on first line after main function.

import java.net.*; import java.io.*; import java.awt.*;  @suppresswarnings("serial") class chatclient extends frame implements runnable {     socket soc;      textfield tf;     textarea ta;     button btnsend,btnclose;     string sendto;     string loginname;     thread t = null;     dataoutputstream dout;     datainputstream din;      chatclient(string loginname,string chatwith) throws exception {         super(loginname);         this.loginname = loginname;         sendto = chatwith;         tf = new textfield(50);         ta = new textarea(50,50);         btnsend = new button("send");         btnclose = new button("close");         soc = new socket("localhost",12342);          din = new datainputstream(soc.getinputstream());          dout = new dataoutputstream(soc.getoutputstream());              dout.writeutf(loginname);          t = new thread(this);         t.start();     }     @suppresswarnings("deprecation")     void setup()     {         setsize(600,400);         setlayout(new gridlayout(2,1));         add(ta);         panel p = new panel();          p.add(tf);         p.add(btnsend);         p.add(btnclose);         add(p);         show();          }     @suppresswarnings("deprecation")     public boolean action(event e,object o){         if(e.arg.equals("send")){             try{                 dout.writeutf(sendto + " "  + "data" + " " + tf.gettext().tostring());                           ta.append("\n" + loginname + " says:" + tf.gettext().tostring());                    tf.settext("");             }             catch(exception ex){                 ex.printstacktrace();             }            }         else if(e.arg.equals("close")){             try{                 dout.writeutf(loginname + " logout");                 system.exit(1);             }             catch(exception ex){                 ex.printstacktrace();             }          }          return super.action(e,o);     }      public static void main(string args[]) throws exception{         chatclient client1 = new chatclient(args[0],args[1]);         client1.setup();                     }        public void run(){               while(true){             try{                 ta.append( "\n" + sendto + " says :" + din.readutf());              }             catch(exception ex){                 ex.printstacktrace();             }         }     } } 

the args variable not of length two. variable stores command line arguments passed application.

you forgot pass command line arguments. if you're using command line, specify them so:

java chatclient "arg1" "arg2" 

if you're using ide, eclipse build , run application, need specific instructions ide.


Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -