• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Exiting a thread

 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 323
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
Posts: 323
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
(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
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 323
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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();
 
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you creating another, hidden, set of edit boxes that end up getting referenced when you try to reconnect?
 
Andy Rayner
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Can you hear that? That's my theme music. I don't know where it comes from. Check under this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic