Java Swing & Socket Messenger StackOverflow Error -


i making simple client-server messenger pair sockets , jswing, , have run stackoverflow error. know(or @ least think) it's caused recursive functon(which don't think have, may) doesn't have correct termination condition(does mean return; in switch statement?) here's of error(since it's stackoverflow, repeats itself, of course)

exception in thread "awt-eventqueue-0" java.lang.stackoverflowerror @ java.util.hashmap.getentry(unknown source) @ java.util.hashmap.get(unknown source) @ sun.awt.appcontext.get(unknown source) @ com.sun.java.swing.swingutilities3.getdelegaterepaintmanager(unknown source) @ javax.swing.repaintmanager.getdelegate(unknown source) @ javax.swing.repaintmanager.adddirtyregion(unknown source) @ javax.swing.jcomponent.repaint(unknown source) @ java.awt.component.repaint(unknown source) @ javax.swing.jcomponent.setbackground(unknown source) @ javax.swing.lookandfeel.installcolors(unknown source) @ javax.swing.lookandfeel.installcolorsandfont(unknown source) @ javax.swing.plaf.basic.basicpanelui.installdefaults(unknown source) @ javax.swing.plaf.basic.basicpanelui.installui(unknown source) @ javax.swing.jcomponent.setui(unknown source) @ javax.swing.jpanel.setui(unknown source) @ javax.swing.jpanel.updateui(unknown source) @ javax.swing.jpanel.<init>(unknown source) @ javax.swing.jpanel.<init>(unknown source) @ javax.swing.jpanel.<init>(unknown source) @ javax.swing.jrootpane.createglasspane(unknown source) @ javax.swing.jrootpane.<init>(unknown source) @ javax.swing.jframe.createrootpane(unknown source) @ javax.swing.jframe.frameinit(unknown source) @ javax.swing.jframe.<init>(unknown source) @ socket_swing_database_test.socketmanager.<init>(socketmanager.java:9) @ socket_swing_database_test.clientswing.<init>(clientswing.java:6) @ socket_swing_database_test.socketmanager.<init>(socketmanager.java:10) @ socket_swing_database_test.clientswing.<init>(clientswing.java:6) @ socket_swing_database_test.socketmanager.<init>(socketmanager.java:10) @ socket_swing_database_test.clientswing.<init>(clientswing.java:6) @ socket_swing_database_test.socketmanager.<init>(socketmanager.java:10) 

my code:

public class clientmain extends jframe { private socketmanager sm = new socketmanager();  public static void main(string args[]) {     swingutilities.invokelater(new runnable() {          @override         public void run() {             clientswing window = new clientswing();             window.setvisible(true);          }     }); }  public void run() {     sm = new socketmanager();      thread thread = new thread(sm);     thread.run(); } 

}

clientswing:

public class clientswing extends jframe implements runnable { public socketmanager socketmanager = new socketmanager();  public final int null = 0; public final int disconnected = 1; public final int disconnecting = 2; public final int begin_connect = 3; public final int connected = 4; public int connectionstatus = socketmanager.connectionstatus;  public jpanel panel, statusbar; public jtextarea textarea; public jtextfield textfield, statuscolor, usernamefield; public jlabel statusfield; public jmenubar menubar; public jbutton connectbutton, disconectbutton;  public int width = 640; public int height = 480;  public clientswing() {     super("client");      panel = new jpanel();     panel.setlayout(null);        setdefaultcloseoperation(exit_on_close);     setlocationrelativeto(null);     setsize(width, height);     setresizable(true);     setjmenubar(menubar); }  @override public void run() {     switch(connectionstatus) {     case disconnected:          break;      case disconnecting:          break;      case connected:          break;      case begin_connect:          break;     } }  public void invoke(runnable runnable) {     swingutilities.invokelater(runnable);  } 

}

socketmanager:

public class socketmanager extends jframe implements runnable { public clientswing cs = new clientswing();  public final int null = 0; public final int disconnected = 1; public final int disconnecting = 2; public final int begin_connect = 3; public final int connected = 4;  public final string statusmessages[] = {     " error! not connect", " disconnected",     " disconnecting...", " connceting...", " connected" };  public final string end_chat_session = new character((char)0).tostring();  public string hostip = "localhost"; public int port = 2484; public int connectionstatus = disconnected; public stringbuffer toappend = new stringbuffer(""); public stringbuffer tosend = new stringbuffer(""); public string statusstring = statusmessages[connectionstatus];  public socket socket; public bufferedreader in; public printwriter out;  public string error;  //make method gets username username field public string username = "username";   public void changestatusnts(int newconnectstatus, boolean noerror) {     if(newconnectstatus != null) {         connectionstatus = newconnectstatus;     }      if(noerror) {         statusstring = statusmessages[connectionstatus];      } else {         statusstring = statusmessages[null];      }      swingutilities.invokelater(cs); }  public void changestatusts(int newconnectstatus, boolean noerror) {     if(newconnectstatus != null) {         connectionstatus = newconnectstatus;     }      if(noerror) {         statusstring = statusmessages[connectionstatus];      } else {         statusstring = statusmessages[null];      }      cs.run(); }  public void cleanup() {  }  private void appendtochatbox(string s) {     synchronized(toappend) {         toappend.append(s);     } }  private void sendstring(string s) {  }  @override public void run() {     while(true) {         try {             thread.sleep(10);          } catch(interruptedexception e) {             error = e.tostring();             //append error textarea         }          switch(connectionstatus) {         case begin_connect:             try {                 socket = new socket(hostip, port);                 in = new bufferedreader(new inputstreamreader(socket.getinputstream())); //i right here                 out = new printwriter(socket.getoutputstream(), true);                 changestatusts(connected, true);              } catch(exception e) {                 cleanup();                 changestatusts(disconnected, false);                  error = e.tostring();              }              break;          case connected:              try {                 if(tosend.length() != 0) {                     out.print(tosend);                     out.flush();                     tosend.setlength(0);                     changestatusts(null, true);                 }                  if(in.ready()) {                     string input = in.readline();                     if((input != null) && (input.length() != 0)) {                         if(input.equals(end_chat_session)) {                             changestatusts(disconnecting, true);                          } else {                             appendtochatbox(username + ": " + input + "\n");                         }                     }                 }             }  catch(ioexception e) {                 cleanup();                 changestatusts(disconnected, false);                  error = e.tostring();             }             break;          case disconnecting:             out.print(end_chat_session);             out.flush();              cleanup();             changestatusts(disconnected, true);              break;          default: break; //do nothing         }     } }  class actionadapter implements actionlistener {     public void actionperformed(actionevent e) {} } 

}

you're getting stackoverflow because of this

public class socketmanager extends jframe implements runnable {     public clientswing cs = new clientswing(); ....  public class clientswing extends jframe implements runnable {     public socketmanager socketmanager = new socketmanager(); 

you're creating clientswing, create socketmanager, creates clientswing, create socketmanager, creates clientswing, create socketmanager, creates clientswing, create socketmanager, creates clientswing, create socketmanager, creates clientswing, create socketmanager, creates clientswing, create socketmanager, creates aclientswing, create socketmanager, creates a.... stackoverflow

get picture??

  • if want need access class, don't instantiate it. can pass 1 class other reference. , have appropriate getters , setters fields want access to.
  • or, can create mvc design, more appropriate way sharing data between classes.

side note


edit

something pointed posibility. instead of instantiating, pass value

public class clientswing ...{     socketmanager socketmanager;     public clientmanger(socketmanager socketmanger) {         this.socketmanage = socketmanager;     } } 

Comments

Popular posts from this blog

php - regexp cyrillic filename not matches -

c# - OpenXML hanging while writing elements -

sql - Select Query has unexpected multiple records (MS Access) -