Thread cannot access static variable

Hi there,

I have declared a static string in my class

public static string a = "lockvariable";

and am then trying to access it from a thread (which I should be able to do shouldnt I )

public static void theThread()

{

Program.f.postFeedback(1, a);

Program.f.postFeedback(1, b);

File.Copy("c:\\vid.wmv","c:\\vid2.wmv");

Program.f.UpdateTransferList(Int32.Parse(idTransfer), 2);

Program.f.postFeedback(1, "Operation Completed");

TransferInProgress = 0;

MyThread.Abort();

}

I am getting the following error:

System.NullReferenceException was unhandled
Message="Object reference not set to an instance of an object."
Source="LearningObjectsServer"
StackTrace:
at LearningObjectsServer.Form1.theThread() in C:\Documents and Settings\Owner\My Documents\Visual Studio 2005\Projects\LearningObjectsServer\LearningObjectsServer\Form1.cs:line 30
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()

The funny thing is, if I put a messagebox in just before where the error occurs, it finds the string.

Cheers,

Phil Gould



Answer this question

Thread cannot access static variable

  • Quilnux

    If you were unable to access the static you'd get a compile-time error not a run-time error.

    The exception indicates that some object reference is set to 'null' when it shouldn't be. Unless your code explicitly sets a = null; somewhere it seems very unlikely that the reference a would be null. Even if a was null, you wouldn't get a null reference exception at the call to postFeedback, you'd just pass null into postFeedback and perhaps get an null reference exception from inside postFeedback (which would depend on what it was that postFeedback did with its second parameter)..

    Most likely your error is really that Program.f is null. Depending on when 'theThread' is launched it may be that theThread accesses Program.f before Program.f has been initialised


  • DavidAtPEfiberoptics

    Hi there, thanks for the help.

    I tried the code you suggested and it threw the exception "program.f is null"

    I have managed to bodge a way around this problem, thanks !


  • mEt

    Glad you've got something that works even if it is a bodge.

    It sounds like perhaps whatever code initialises Program.f is running after the first statement of 'theThread' tries to make use of the reference. One of the joys of multi-thread programming is getting everything to happen in the right order Sad


  • Xanthius

    Ok, I have figured out that the thread can access the static variable. It is just not able to access the method

    public void postFeedback(int box, string feedback)

    {

    if (listbox.InvokeRequired)

    {

    SetTextCallback d = new SetTextCallback(postFeedback);

    this.Invoke(d, new object[] { box, feedback });

    }

    else

    {

    if (box == 1)

    {

    listbox.Items.Add(feedback);

    }

    if (box == 2)

    {

    listBox1.Items.Add(feedback);

    }

    }

    }

    Any ideas


  • skalapeno

    I think it's not able to access the method because there is no object reference to use to get to the method. As an experiment, try changing the first line of 'theThread' to look something like this...


    if (Program.f == null)
    {
    throw new ApplicationException("Program.f is null");
    }
    else
    {
    Program.f.postFeedback(1, a);
    }


  • Thread cannot access static variable