1

I am getting an undefined null pointer exception if I try this. (Removing the thread code solves it). Isn't it possible to spawn a new thread from a setter? If so, why not? I am used to doing it this way from Java. Thank you

EDIT: The actual stripped down code:

    public string AuthToken { 
        set {

            this.authToken = value;


            var RunOnNewThread = new Thread(() => 
                {
                    Console.WriteLine("Test");
                } );
            RunOnNewThread.Start();

        }
        get { return this.authToken;  }
    }   

It does actually run the thread. Note! This is in an Xamarin Android app, and the error only occurs in debug mode. Release works perfectly.

Thread started: #2
Test
Thread finished: #2

Here a screenshot from Xamarin: Xamarin Mac

Trace

I tried surrounding the thread creation in a try/catch, but it still throws the null pointer on RunOnNewThread.Start().

try catch

However, there must be something else going on. Because it just ran fine 2 times, and the 3rd time I got the null pointer again. Any ideas?

Fhl
  • 1,079
  • 1
  • 12
  • 26
  • 5
    The code you show does not throw that exception. See [duplicate](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) for troubleshooting `NullReferenceException`s. If you want help with your specific case, show the exact code throwing the exception and the line at which it occurs and the values of the variables involved (hint: the relevant one is `null`). There's nothing special about setters and threads, so the exception must come from your `() => ` lambda. You also may want to reconsider the design, why do you spawn a thread in a setter? – CodeCaster Apr 17 '15 at 10:59
  • Thanks for helping out, @CodeCaster. Can you please remove the duplicate? I don't believe it's correct in this case. – Fhl Apr 17 '15 at 11:30
  • The exception is thrown in `Thread.Start()` (or rather, in an explicit conversion operator of `IntPtr`), looks like a framework bug at first glance (the framework itself should never throw `NullReferenceException`s). What if you don't use a lambda, but move the code to a method and use `new Thread(TheMethod)`? – CodeCaster Apr 17 '15 at 11:37
  • I tried using a dedicated method like you said. The results were similar. I ran the app 4 times. The first time I got the same exception, 2nd and 3rd it ran perfectly, and the 4th time it gave the same exception but also printed out "Test". I don't really know where to begin to solve this. Do you think its an issue with Mono/Xamarin? – Fhl Apr 17 '15 at 11:45
  • @FredLarsen Properties have nothing to do with this, they are compiled as method calls at any rate. That said, this code does a lot for a property so I'd move it to a method just for general best-practise reasons. It appears as though the code you've provided doesn't throw the exception (nothing can be null at that point other than something in the inner workings of the thread). Have you thought about migrating to tasks? I'm not sure if they are available in xamarin though. – Adam Houldsworth Apr 17 '15 at 12:40
  • I agree with Adam. While they are technically just a pair of methods, properties have specific semantics in C#/.NET and it's important that they be minimally implemented. Starting a new thread is definitely contrary to this philosophy, and can lead to a variety of problems. Now, that said...there's nothing special about a property setter that would explain your NRE; given what little code you've posted here, it is likely you are seeing a bug in your platform but you would need to reduce/narrow the problem further to receive help with it. – Peter Duniho Apr 17 '15 at 20:51

0 Answers0