• Post Reply Bookmark Topic Watch Topic
  • New Topic

calling method which involves socket in MainActivity  RSS feed

 
simone giusti
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
MainActivity established a socket connection. I have a method in MainActivity which sends commands to this connection.
How can invoke this method from another class ???

This is the error:
Attempt to invoke virtual method 'java.lang.String it.rockopera.scsremote.TelnetClient.command(java.lang.String)' on a null object reference



This is method whick invoke MainActivity's method


This method could not be static.

Thanks
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
simone giusti wrote:This is the error:
Attempt to invoke virtual method 'java.lang.String it.rockopera.scsremote.TelnetClient.command(java.lang.String)' on a null object reference

The error is actually pretty clear. Somewhere (not in the code you've shown) you have a reference to a TelnetClient on which you call command. However, that reference is null.

Can you show the piece of code where you call this command method?
 
simone giusti
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is method in mainactivity:



This method reports no errors if called from inside MainActivity.

Thanks.




Rob Spoor wrote:
simone giusti wrote:This is the error:
Attempt to invoke virtual method 'java.lang.String it.rockopera.scsremote.TelnetClient.command(java.lang.String)' on a null object reference

The error is actually pretty clear. Somewhere (not in the code you've shown) you have a reference to a TelnetClient on which you call command. However, that reference is null.

Can you show the piece of code where you call this command method?


 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And where do you assign anything to your telnetClient? Because my guess is that you don't, which means the reference remains null.
 
simone giusti
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes .. maybe you are right but since method works from main I thought it worked from other class
How can I assign ?

Rob Spoor wrote:And where do you assign anything to your telnetClient? Because my guess is that you don't, which means the reference remains null.
 
Jon Crow
Greenhorn
Posts: 8
1
Firefox Browser Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It matters when and where that you assign telnetClient variable. Assigning example:
telnetClient = new TelnetSocket();

If you are assigning it in a constructor, and are trying to use the variable from another class, then you might be trying to use it before your constructor created it.

Can you show us where you set your telnetClient variable? If you haven't set it to anything, then that is why it would be null.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!