[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

microsoft.public.dotnet.framework.remoting

Exception "Object Reference not set to an instance of the object

Steve L

9/26/2004 12:07:00 AM

Subject: .NET Remoting problem - exception "Object Reference not set to
instance of an object"
From: aacool <a@b.c>
Newsgroups: microsoft.public.dotnet.framework

Hi,

I need some help with a .NET Remoting problem - an "Object Reference not
set to instance of an object"exception is thrown in the client when
accessing methods of a remote object.

Basically, I have a server implemented as a Windows Service, using a
config file, and implementing an object from the General namespace. The
server code is below.

////Data definitions namespace
namespace General
{
/// <summary>
/// Interface to access data object
/// </summary>
public interface ICustomerManager
{
Customer getCustomer(int id);
int getAge(int id);
}
/// <summary>
/// Data object implementation
/// </summary>
[Serializable]
public class Customer
{
//put all functions in an interface
protected int cust_id;
public string firstName,lastName;
public DateTime BirthDate;
public Customer(int id)
{
cust_id = id;
}
}
}


/////////Server code
namespace WindowsServiceSrvr
{
public class ServiceSrvr : System.ServiceProcess.ServiceBase
{
private ComponentModel.Container components = null;
public static string SVC_NAME = "ServiceSrvr";
public static string cfgFile = "server.config";
public ServiceSrvr(){...}

static void Main()
{
System.ServiceProcess.ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceProcess.ServiceBase[] {
new ServiceSrvr() };
evt.Source = SVC_NAME;
ServiceProcess.ServiceBase.Run(ServicesToRun);
}
protected override void OnStart(string[] args)
{
RemotingConfiguration.Configure(cfgFile);
}
}

[Serializable]
public class CustomerMgr : MarshalByRefObject,
General.ICustomerManager
{
public CustomerMgr()
{
}

public Customer getCustomer(int custId)
{
General.Customer cust = new Customer(custId);
cust.BirthDate = new DateTime(1945,12,12);
return cust;
}
public int getAge(int custId)
{
General.Customer cust = this.getCustomer(custId);
TimeSpan tmp = DateTime.Today.Subtract(cust.BirthDate);
return (int)tmp.Days/355;
}

}
}


My client uses a Generated_General.dll generated by soapsuds

soapsuds -url:http://localhost:custSrvr?wsdl -oa:gen.dll

The client code is:

namespace ClientUsingService
{
/// <summary>
/// Implementation of client - config file, soapsuds generated
/// metaschema, and a server running as a windows service
/// </summary>
class Client
{
public static string cfgFile = "app.config";
[STAThread]
static void Main(string[] args)
{
try
{
RemotingConfiguration.Configure(cfgFile);
// Instantion CustomerMgr interface
WindowsServiceSrvr.CustomerMgr custMgr = new
WindowsServiceSrvr.CustomerMgr();
if(custMgr!=null)
Console.WriteLine("Got server connection");
General.Customer cust = custMgr.getCustomer(5434);
Console.WriteLine(
cust.BirthDate.ToShortDateString());
// throws Invalid Reference Exception
Console.WriteLine("Customer 5434 is {0} yrs
old",custMgr.getAge(5443));
Console.ReadLine();
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
Console.WriteLine(ex.StackTrace);
Console.ReadLine();
}
}
}
}

The problem is that when the methods of custMgr are called by the client
the exception

"Object Reference not set to an instance of an object" is thrown.

I am at my wits end!! Please help explain what is wrong.

3 Answers

Lord2702

9/26/2004 6:47:00 AM

0

You should check if your cust object has returned properly that is it is not
null.

"aacool" <a@b.c> wrote in message
news:Xns956FC36E11E09aacool@24.94.170.88...
> Subject: .NET Remoting problem - exception "Object Reference not set to
> instance of an object"
> From: aacool <a@b.c>
> Newsgroups: microsoft.public.dotnet.framework
>
> Hi,
>
> I need some help with a .NET Remoting problem - an "Object Reference not
> set to instance of an object"exception is thrown in the client when
> accessing methods of a remote object.
>
> Basically, I have a server implemented as a Windows Service, using a
> config file, and implementing an object from the General namespace. The
> server code is below.
>
> ////Data definitions namespace
> namespace General
> {
> /// <summary>
> /// Interface to access data object
> /// </summary>
> public interface ICustomerManager
> {
> Customer getCustomer(int id);
> int getAge(int id);
> }
> /// <summary>
> /// Data object implementation
> /// </summary>
> [Serializable]
> public class Customer
> {
> //put all functions in an interface
> protected int cust_id;
> public string firstName,lastName;
> public DateTime BirthDate;
> public Customer(int id)
> {
> cust_id = id;
> }
> }
> }
>
>
> /////////Server code
> namespace WindowsServiceSrvr
> {
> public class ServiceSrvr : System.ServiceProcess.ServiceBase
> {
> private ComponentModel.Container components = null;
> public static string SVC_NAME = "ServiceSrvr";
> public static string cfgFile = "server.config";
> public ServiceSrvr(){...}
>
> static void Main()
> {
> System.ServiceProcess.ServiceBase[] ServicesToRun;
> ServicesToRun = new ServiceProcess.ServiceBase[] {
> new ServiceSrvr() };
> evt.Source = SVC_NAME;
> ServiceProcess.ServiceBase.Run(ServicesToRun);
> }
> protected override void OnStart(string[] args)
> {
> RemotingConfiguration.Configure(cfgFile);
> }
> }
>
> [Serializable]
> public class CustomerMgr : MarshalByRefObject,
> General.ICustomerManager
> {
> public CustomerMgr()
> {
> }
>
> public Customer getCustomer(int custId)
> {
> General.Customer cust = new Customer(custId);
> cust.BirthDate = new DateTime(1945,12,12);
> return cust;
> }
> public int getAge(int custId)
> {
> General.Customer cust = this.getCustomer(custId);
> TimeSpan tmp = DateTime.Today.Subtract(cust.BirthDate);
> return (int)tmp.Days/355;
> }
>
> }
> }
>
>
> My client uses a Generated_General.dll generated by soapsuds
>
> soapsuds -url:http://localhost:custSrvr?wsdl -oa:gen.dll
>
> The client code is:
>
> namespace ClientUsingService
> {
> /// <summary>
> /// Implementation of client - config file, soapsuds generated
> /// metaschema, and a server running as a windows service
> /// </summary>
> class Client
> {
> public static string cfgFile = "app.config";
> [STAThread]
> static void Main(string[] args)
> {
> try
> {
> RemotingConfiguration.Configure(cfgFile);
> // Instantion CustomerMgr interface
> WindowsServiceSrvr.CustomerMgr custMgr = new
> WindowsServiceSrvr.CustomerMgr();
> if(custMgr!=null)
> Console.WriteLine("Got server connection");
> General.Customer cust = custMgr.getCustomer(5434);
> Console.WriteLine(
> cust.BirthDate.ToShortDateString());
> // throws Invalid Reference Exception
> Console.WriteLine("Customer 5434 is {0} yrs
> old",custMgr.getAge(5443));
> Console.ReadLine();
> }
> catch(Exception ex)
> {
> Console.WriteLine(ex.Message);
> Console.WriteLine(ex.StackTrace);
> Console.ReadLine();
> }
> }
> }
> }
>
> The problem is that when the methods of custMgr are called by the client
> the exception
>
> "Object Reference not set to an instance of an object" is thrown.
>
> I am at my wits end!! Please help explain what is wrong.
>


Steve L

9/26/2004 2:57:00 PM

0

I rebuilt all three assemblies and reinstalled the service - it works
fine now. I have put in a check on the cust object not being null, but
why would cust be null sometimes and not null other times with the same
code?

Thanks for the tip
aacool
"Lord2702" <Lord2702@MSN.com> wrote in
news:#hpfYS5oEHA.3392@TK2MSFTNGP15.phx.gbl:

> You should check if your cust object has returned properly that is it
> is not null.
>
> "aacool" <a@b.c> wrote in message
> news:Xns956FC36E11E09aacool@24.94.170.88...
>> Subject: .NET Remoting problem - exception "Object Reference not set
>> to instance of an object"
>> From: aacool <a@b.c>
>> Newsgroups: microsoft.public.dotnet.framework
>>
>> Hi,
>>
>> I need some help with a .NET Remoting problem - an "Object Reference
>> not set to instance of an object"exception is thrown in the client
>> when accessing methods of a remote object.
>>
>> Basically, I have a server implemented as a Windows Service, using a
>> config file, and implementing an object from the General namespace.
>> The server code is below.
>>
>> The problem is that when the methods of custMgr are called by the
>> client the exception
>>
>> "Object Reference not set to an instance of an object" is thrown.
>>
>> I am at my wits end!! Please help explain what is wrong.
>>
>
>
>

Sooraj PM

9/27/2004 10:39:00 AM

0

Hi

I think you have posted the same question in another section. Please check
the answer I had given to that

Sooraj
Microsoft Community Star

"aacool" wrote:

> I rebuilt all three assemblies and reinstalled the service - it works
> fine now. I have put in a check on the cust object not being null, but
> why would cust be null sometimes and not null other times with the same
> code?
>
> Thanks for the tip
> aacool
> "Lord2702" <Lord2702@MSN.com> wrote in
> news:#hpfYS5oEHA.3392@TK2MSFTNGP15.phx.gbl:
>
> > You should check if your cust object has returned properly that is it
> > is not null.
> >
> > "aacool" <a@b.c> wrote in message
> > news:Xns956FC36E11E09aacool@24.94.170.88...
> >> Subject: .NET Remoting problem - exception "Object Reference not set
> >> to instance of an object"
> >> From: aacool <a@b.c>
> >> Newsgroups: microsoft.public.dotnet.framework
> >>
> >> Hi,
> >>
> >> I need some help with a .NET Remoting problem - an "Object Reference
> >> not set to instance of an object"exception is thrown in the client
> >> when accessing methods of a remote object.
> >>
> >> Basically, I have a server implemented as a Windows Service, using a
> >> config file, and implementing an object from the General namespace.
> >> The server code is below.
> >>
> >> The problem is that when the methods of custMgr are called by the
> >> client the exception
> >>
> >> "Object Reference not set to an instance of an object" is thrown.
> >>
> >> I am at my wits end!! Please help explain what is wrong.
> >>
> >
> >
> >
>
>