-1

I have the following code:

CustomerService service;
public CustomerService Service
{
    get
    {
        if (this.service == null)
        {
            this.service = new CustomerService();
        }
        return this.service;
    }
}

public DataTable GetCustomers()
{
    return this.Service.GetCustomers();
}

Now the question is: if I wrote the above method as follow (without "this"), it's giving me an error : instance is not reference to an object.

public DataTable GetCustomers()
{
    return Service.GetCustomers(); // this will spell the error "instance is not reference to an object"
}

Does anyone know? also it only happens while running via IIS and not from casini web server (VS 2010).

Timwi
  • 65,159
  • 33
  • 165
  • 230
user384080
  • 4,576
  • 15
  • 64
  • 96
  • Please use code blocks to format your code so it is easier to read. – Michael Shimmins Aug 14 '10 at 10:23
  • 1
    In what context is this code executing? Code behind, Controller, etc? What Intellisense are you seeing when you hit . in the `Service.` example? – Lazarus Aug 14 '10 at 10:31
  • this is so confusing.. Now even with "this" clause it is giving me the error of Object reference not set to an instance of an object. At the moment the class only contains those line of codes. no other service object declared.. really weird.. – user384080 Aug 14 '10 at 11:23
  • @ronald: you run a big risk of confusing service and Service, and Service is a common name in the lib as well. Consider renaming to _service and BaseService or something. Also be very clear and precise about the IIS factor. – H H Aug 14 '10 at 20:36
  • Posting a full stack trace might help. – Mark Byers Aug 14 '10 at 21:30
  • The problem is: it is running under cassini web server but not on IIS. How do post the stack trace while this textbox only limit 600 chars? – user384080 Aug 15 '10 at 06:25

5 Answers5

2

The presence or absence of this cannot explain the error you are witnessing. In this situation they mean exactly the same thing and will compile to the same IL code. Check the assembly using .NET Reflector to verify this if you wish. The error is occurring at random, probably due to a race condition.

One thing I can immediately see is that if you are running this code from multiple threads then it looks like you have a race condition here:

if (this.service == null)
{
    this.service = new CustomerService();
}
return this.service;

In the multithreaded case you would need to lock otherwise you could get two CustomerService objects. I'm not sure if this explains your error, but it certainly could create confusion. Race conditions can occur in one environment but not in another as the frequency of the error can depend on the type of the hardware and on what other processes are running on the server.

You may also have other race conditions in the code you haven't posted. Don't use this lazy initialization technique without locking unless you are sure that you have only one thread.

Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452
  • not sure about this racing thingy but when I created the CustomerServiceClass object in the page_load event (used to be outside the page_load event) now it works ok.. – user384080 Aug 15 '10 at 06:39
1

You probably have a name conflict with another 'Service' (class or namespace). The use of this solves it.

I'm a bit skeptical about the difference between Cassinin and IIS, have you carefully checked that?

H H
  • 263,252
  • 30
  • 330
  • 514
  • I tried and I couldn’t construct a situation in which a class or namespace with the same name would conflict with a field name. The field name always takes precedence. `this` seems to make a difference only when it’s a local variable or method parameter. Did I overlook anything? – Timwi Aug 14 '10 at 20:15
1

Something like this should be in a singleton. Which would resolve many issues like threading if implemented correctly and would make the implementation and readability of the code much better.

Thanks -Blake Niemyjski (.netTiers team member)

Blake Niemyjski
  • 3,432
  • 3
  • 25
  • 41
0

I fiddled a bit with your code in Visual Studio and I couldn’t even get a name conflict to produce the error message you described. I can’t think of any case in which “this.X” can ever be different from “X” except when “X” is a local variable or a method parameter.

Timwi
  • 65,159
  • 33
  • 165
  • 230
0

Would the CustomerService class derive from a base class called Service? If so, then that's the problem.

Enigmativity
  • 113,464
  • 11
  • 89
  • 172