0

hello I wonder why this code make me an exception.

namespace Test_Inotify
{
class Test_Onchange
{
    private int value;
    private object _lock = new object();
    /*removed private object _lock;*/

    public event System.EventHandler ValueChanged;

    protected virtual void OnValueChange()
    {
        lock (_lock)
        {
            if (ValueChanged != null)
                ValueChanged(this, EventArgs.Empty);
        }
    }

    public int Value
    {
        get { return this.value; }
        set
        { 
        this.value = value;
        OnValueChange();
        }
    }
}
}

And the form

namespace Test_Inotify
{
public partial class Form1 : Form
{
    Test_Onchange ValueClass ;//ForceValue removed;

    public Form1()
    {
        InitializeComponent();
       /* Test_Onchange removed*/ ValueClass = new Test_Onchange();

        ValueClass.ValueChanged += new EventHandler(EventValueChanged);

        for (int i = 0; i < 10; i++)
        {
            Random Rndvalue = new Random();
            int RandVal = Rndvalue.Next(0, 100);
            ValueClass.Value = RandVal;
            System.Threading.Thread.Sleep(500);           
        }

    }
    private void EventValueChanged(object sender, EventArgs e)
    {
        int valueforce = ValueClass.Value; /*removedForceClass.Value;*/
        MessageBox.Show("Event raised"+valueforce);
    }
}
}

I think it's in the EventValueChanged.... but I don't modify anything in the values, I only copy it in a local variable.

Could anyone help me??

JudgeDreed
  • 147
  • 12
  • What's the exception? – Martheen Jun 30 '16 at 09:50
  • 2
    Welcome to StackOverflow! Why don't you tell us _what_ exception is thrown and _where_? An exception has a _message_ that normally tells you what went wrong and a _stack trace_ that tells you where. – René Vogt Jun 30 '16 at 09:50
  • 1
    Possible duplicate of [What is a NullReferenceException, and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – René Vogt Jun 30 '16 at 09:52
  • guess you did not initialize the `ForceClass` member, so you get a `NullReferenceException`... – René Vogt Jun 30 '16 at 09:53
  • Also: the `_lock` variable doesn't seem to get initialized. Locking on `null` is not allowed. – Me.Name Jun 30 '16 at 09:54

2 Answers2

0

How about this:

Test_Onchange ForceClass = new Test_Onchange();
ForceClass.ValueChanged += new EventHandler(EventValueChanged);

and

ForceClass.Value = RandVal;

why do you have ForceClass and ValueClass ? and ForceClass is always NULL

0

You are creating a new object of type Test_Onchange here

Test_Onchange ValueClass = new Test_Onchange();
ValueClass.ValueChanged += new EventHandler(EventValueChanged);

But in the event, you're using your global object ForceClass. You'll get a NullReferenceException because your ForeClass is not instantiated. Replace your code with this :

ForeClass = new Test_Onchange();
ForeClass.ValueChanged += new EventHandler(EventValueChanged);

for (int i = 0; i < 10; i++)
{
    Random Rndvalue = new Random();
    int RandVal = Rndvalue.Next(0, 100);
    ForeClass.Value = RandVal;
    System.Threading.Thread.Sleep(500);           
}
Haytam
  • 4,250
  • 2
  • 10
  • 37
  • Yes thanks! I changed the name but not everywhere.... and I was in the multithreading problems and I thought it was that... sorry... – JudgeDreed Jun 30 '16 at 10:14