开发者

Null Reference in editing a task scheduler trigger in c#

开发者 https://www.devze.com 2023-02-18 12:15 出处:网络
if (t != null)is always null why help.. when ever i try to get value in the variable name t it always gets in the else part but i am sure that there is valuse in tat variable.

if (t != null) is always null why help.. when ever i try to get value in the variable name t it always gets in the else part but i am sure that there is valuse in tat variable.

private void button3_Click(object sender, EventArgs e)
      {
            try
            {
                if (search=="")
                {

            开发者_如何学Go    }
                else
                {
                    if (textBox1.Text=="")
                    {
                        MessageBox.Show("Select A Task Or Find One");
                    }
                    else
                    {
                        search = textBox1.Text;
                    }
                                 }
                if (search != null)
                {
                    t = tasks.OpenTask(search);
                    if (textBox2.Text!="")
                    {

                    short hour = short.Parse(textBox2.Text.Substring(0, 2));
                    short minute = short.Parse(textBox2.Text.Substring(3, 2));
                        if (t != null)  // this is null dont know why
                    {
                        foreach (Trigger tr in t.Triggers)
                        {
                            if (tr is StartableTrigger)
                            {
                                (tr as StartableTrigger).StartHour = hour;
                                (tr as StartableTrigger).StartMinute = minute;
                            }
                        }
                        t.Save();
                        t.Close();
                    }
                    tasks.Dispose();
                    button2.Visible = true;
                    textBox3.Visible = true;
                    search = "";
                    }
                    else
                    {
                        MessageBox.Show("Enter Time ");
                    }
                }
            }
            catch (Exception b)
            {
                MessageBox.Show(b.ToString());
               // MessageBox.Show("Select A Task From The List ");
            } 

        }

help guys .. well i tried it to debug but didnt get a break through..


t is null because tasks.OpenTask(search) returns null. Probably there is no task matching your search criteria.

Why are you disposing of tasks in the first place?


Any place in your source ,where you have written something like this MyClass t = new MyClass().. where t is your class object. If you have not declared,It will always come null.

Or might be you have declared something like this private Task t; but forgot to add new keyword. Checkout!!!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号