Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Socket Reference Problem?  RSS feed

 
Ben Jass
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've established a connection between a client and server. On the client side, I'm trying to pass a socket as an argument to a constructor.



However, Whenever I do this, when I attach a stream I get a big fat null. (Yes, I initialized in the constructor).



Are Socket's not able to be passed over as arguments? How would I do this without any polymorphism? So that the same connection gets brought over to another class?
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This line...

Ben Jass wrote:


Doesn't instantiate an instance, and hence, doesn't call the constructor. Can you show us the code for the NewGui() method, which is what is being called here?

Also, the constructor that you provided doesn't make sense. It doesn't compile.

Henry
 
Tony Docherty
Bartender
Posts: 3268
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Please post the code you are actually using - this code won't compile.
Having said that it looks like you are assigning the socket to 'sock' but passing 's'.

Ben Jass wrote:
However, Whenever I do this, when I attach a stream I get a big fat null. (Yes, I initialized in the constructor).

Have you checked to see if the value you are passing is null?

Ben Jass wrote:
Are Socket's not able to be passed over as arguments?

Any object can be passed to another object.
 
Ben Jass
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry lol, I simply forgot the new keyword. I was trying to save you guys from seeing the whole program. I'm trying to shorten this down to the keyparts:

Initialized Instance =



Connection to Server:




Sock still has it's values..
After so and so user action, we come to this call:



 
Paul Clapham
Sheriff
Posts: 22487
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ben Jass wrote:After so and so user action, we come to this call:



So given the code you posted, at this point sock is null. You haven't posted the code which would persuade us that can't happen, and in the code you posted it can happen.

However I notice that your Networking constructor is calling a public method to do the rest of its initialization. I'm assuming you still aren't showing us an SSCCE, so let me just mention that calling public methods from a constructor can backfire if you write a subclass which overrides that public method. Things which you expected to be initialized are surprisingly not initialized, for example. Perhaps that's what you really did? An SSCCE would be helpful not only to us but to you as well.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!