Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Socket Reference Problem?

 
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
author
Posts: 23912
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:



 
Sheriff
Posts: 26973
85
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic