jQuery in Action, 2nd edition*
The moose likes Threads and Synchronization and the fly likes Exiting a thread Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Exiting a thread" Watch "Exiting a thread" New topic
Author

Exiting a thread

Andy Rayner
Ranch Hand

Joined: Dec 09, 2004
Posts: 36
Hello i have wrote a client to connect to a MUD server and the only problem i am getting is when i disconnect i cannot reconnect again without shutting down the client and running it again

when i close the server i do this:


setText just sets the text in my JTextAre, sock.close closes my socket to the server

output is my output from the server class which is a thread
here is the code

this loop is in my run() method, getBoolean returns the boolean which i set from the setBoolean method back in the other class

this is the error i get when i try to reconnect
Exception occurred during event dispatching:
java.lang.NumberFormatException:
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at ViewClass.getPort(ViewClass.java:216)
at Controller.actionPerformed(Controller.java:28)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$ForwardActionEvents.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

any help would be appreciated
thank you
M Beck
Ranch Hand

Joined: Jan 14, 2005
Posts: 323
Originally posted by Andy Rayner:
Exception occurred during event dispatching:
java.lang.NumberFormatException:
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at ViewClass.getPort(ViewClass.java:216)


could you post the getPort() method of ViewClass.java, and point out which line in it is line #216 of ViewClass.java? that would make it easier to see what, if anything, might be calling Integer.parseInt() on a string that doesn't represent an integer number.
Andy Rayner
Ranch Hand

Joined: Dec 09, 2004
Posts: 36


all this does is get the text from the JTextBox called port and converts it into an int. the method is then used to set the port when i create a new socket.
M Beck
Ranch Hand

Joined: Jan 14, 2005
Posts: 323
(funny, i could have sworn i already posted this reply once...)

Originally posted by Andy Rayner:


all this does is get the text from the JTextBox called port and converts it into an int. the method is then used to set the port when i create a new socket.


and somewhere between the disconnect and the reconnect, that JTextBox gets its contents reset, modified, or otherwise messed up somehow. i'll have to leave it up to you to work out how, unfortunately, since there's too many ways this could happen for me to look into it without basically taking over the whole project from you.

but there's a lesson here for you: never use the (Typename).parse(Typename) methods in Integer, Double, and so on, without wrapping them in exception handler blocks to intercept NumberFormatException. especially when the string you're parsing is user input — that JTextBox could hold anything from the number of angels dancing on a pin to a line of insults and cusswords.

also, GUI elements like text boxes have this odd habit of changing their contents in response to GUI events, several of which happen more or less automatically, and most of which us mere mortals never really learn about. there's a lot of black magic behind the workings of Swing...
Andy Rayner
Ranch Hand

Joined: Dec 09, 2004
Posts: 36
I understand what your saying, when the threads are running after the event has been activated, the textfields hostname and port may have been changed and modified.

What i dont understand is why when i put my hostname and port in the first time and click ok it connects, but when i disconnect and try and do it again, it then gives me a numberformat exception.

Do you know what the best way would be to solve this, i have tried setting the fields to "" after i establish the first connection.
Andy Rayner
Ranch Hand

Joined: Dec 09, 2004
Posts: 36
I have amended my code to this:


the first time i start the program and connect it prints out the correct port 23, the second time i get This is not a number and a 0, which obviously then throws an IOException error when it trys to connect the socket, its just wierd how im putting a number in and its not parsing one properly
M Beck
Ranch Hand

Joined: Jan 14, 2005
Posts: 323
if you're writing a multi-threaded program, it might have something to do with synchronizing too. maybe one of the threads is messing up the state of the JTextField behind the backs of the other ones? all i can suggest is, in the catch{} block, print out the value of the variable "p". that way you'll find out what's actually in the JTextField, and maybe get a clue as to how it got there.
Andy Rayner
Ranch Hand

Joined: Dec 09, 2004
Posts: 36
well p is blank
it is like nothing is been picked up
in the code i have to initialise the varibles
String p = "";
and portNo = 0;

and that is whats been returned after i try to do the parseint and get the text, it is as if it wont pick up the text in the fields, the same thing happens for my method getHost();
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
Are you creating another, hidden, set of edit boxes that end up getting referenced when you try to reconnect?
Andy Rayner
Ranch Hand

Joined: Dec 09, 2004
Posts: 36
No, would this be a problem..

On my GUI i have some buttons one is connect and one is disconnect

When i click connect it opens up a new smaller gui
this has two textfields and an ok and cancel button.

My Main Gui and the connectGUI (as i call the method) are in the same class (ViewClass) so when i click connect

it is using the same GUI everytime. Would it probably work if the gui was in a seperate class and i create a new one everytime?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Exiting a thread
 
Similar Threads
display CGM images iingn web browser with out using any viewer in java
RMI stress...
Head First Java QuizCardBuilder not saving files correctly
Java RMC popup applet error
SQLException on connect