-2

I have a label on my form which i want to be filled with the value of a string, but i can't get it working. This is my code;

In the function Execute, i call the function ShowText((string)result[3]), result is the object I fetch from a database query. This is the function ShowText;

public void ShowText(string message)
{
    label4.Text = message;
}

I'm getting a NullReferenceException error, what am i doing wrong?

result[3]; enter image description here label4 enter image description here

code;

    Execute();
    InitializeComponent();
    var aTimer = new System.Timers.Timer();
    aTimer.Elapsed += new ElapsedEventHandler((sender, e) => Execute());
    aTimer.Interval = 300000;
    aTimer.Enabled = true;

}
public void Execute()
{
    int Tijd;
    int Videolengte;
    string resultaat;
    string URL = "";

    Database db = new Database(CONNECTION_STRING);
    object[] result = db.GetFirstRecord();

    if (result == null)
    {
        return;
    }
    else
    {
        //laat app steeds 50ms slapen zodat overgang van muziek mooi is
        System.Threading.Thread.Sleep(500);
        Audio.SetApplicationVolume(APP, 90);
        System.Threading.Thread.Sleep(500);
        Audio.SetApplicationVolume(APP, 80);
        System.Threading.Thread.Sleep(500);
        Audio.SetApplicationVolume(APP, 70);
        System.Threading.Thread.Sleep(500);
        Audio.SetApplicationVolume(APP, 60);
        System.Threading.Thread.Sleep(500);
        Audio.SetApplicationVolume(APP, 50);
        System.Threading.Thread.Sleep(500);
        Audio.SetApplicationVolume(APP, 40);
        //start youtube video op
        URL = (string)result[1];
        Process browser = Process.Start(@"chrome.exe", "http:\\www.youtube.com/watch?v=" + URL);
        ShowText((string)result[3]);
        //laat app steeds 50ms slapen zodat overgang van muziek mooi is
        System.Threading.Thread.Sleep(500);
        Audio.SetApplicationVolume(APP, 30);
        System.Threading.Thread.Sleep(500);
        Audio.SetApplicationVolume(APP, 20);
        System.Threading.Thread.Sleep(500);
        Audio.SetApplicationVolume(APP, 10);
        System.Threading.Thread.Sleep(500);
        Audio.SetApplicationVolume(APP, 0);
        //bepaald tijd die applicatie moet slapen
        resultaat = (string)result[2];
        Videolengte = Convert.ToInt32(resultaat);
        Tijd = Videolengte * 1000;
        //laat app slapen tot lengte van liedje
        System.Threading.Thread.Sleep(Tijd);
        //laat app steeds 50ms slapen zodat overgang van muziek mooi is
        System.Threading.Thread.Sleep(500);
        Audio.SetApplicationVolume(APP, 10);
        System.Threading.Thread.Sleep(500);
        Audio.SetApplicationVolume(APP, 20);
        System.Threading.Thread.Sleep(500);
        Audio.SetApplicationVolume(APP, 30);
        System.Threading.Thread.Sleep(500);
        Audio.SetApplicationVolume(APP, 40);
        System.Threading.Thread.Sleep(500);
        Audio.SetApplicationVolume(APP, 50);
        System.Threading.Thread.Sleep(500);
        Audio.SetApplicationVolume(APP, 60);
        System.Threading.Thread.Sleep(500);
        Audio.SetApplicationVolume(APP, 70);
        System.Threading.Thread.Sleep(500);
        Audio.SetApplicationVolume(APP, 80);
        System.Threading.Thread.Sleep(500);
        Audio.SetApplicationVolume(APP, 90);
        System.Threading.Thread.Sleep(500);
        Audio.SetApplicationVolume(APP, 100);
        //Kill alle google extensies
        Process[] localByName = Process.GetProcessesByName("chrome");
        foreach (Process p in localByName)
        {
            p.Kill();
        }
        //verwijder record uit database
        //deleteRow();

    }
}
public void ShowText(string message)
{
    label5.Text = message;
}
private void deleteRow()
{
    try
    {
        using (SqlConnection conn = new SqlConnection(CONNECTION_STRING))
        {
            conn.Open();
            using (SqlCommand cmd1 = new SqlCommand("SELECT * FROM Tracks ORDER BY Tijdstip", conn))
            using (SqlCommand cmd = new SqlCommand("DELETE TOP(1) FROM Tracks", conn))
            {
               cmd.ExecuteNonQuery();
               cmd1.ExecuteNonQuery();
            }
        }
    }
    catch (SqlException ex)
    {
        //Het is niet gelukt om het record uit de database te verwijderen
    }
}

private void FormMain_Load(object sender, EventArgs e)
{
    //Execute();
}

private Process GetWindowProcess(string windowTitle)
{
    foreach (var proc in Process.GetProcessesByName("chrome"))
    {
        if (proc.MainWindowTitle.IndexOf(windowTitle, StringComparison.OrdinalIgnoreCase) > 0)
        {
            return proc;
        }
    }

    return null;
}
joostmakaay
  • 437
  • 2
  • 7
  • 14

1 Answers1

5

You're either getting it here:

ShowText((string)result[3])

because result is null, or you're getting it here:

label4.Text = message;

because label4 is null.

It's most likely that result is null; especially if it's coming from the database.


UPDATE: now that all the code has been added, just move the call to Execute after the call to InitializeComponent. You see, InitializeComponent creates all of the stuff you dropped on the form in the WYSIWYG designer.

Mike Perrenoud
  • 66,820
  • 29
  • 157
  • 232
  • @joostmakaay, as I stated. If it's happening on that line then `label4` is `null`. I don't know where that field comes from, but it's definitely `null` right now. – Mike Perrenoud Dec 18 '13 at 15:51
  • I've declared the label4 in the FormMain Designer, the text in the properties window is set on label5, how could it be null then? – joostmakaay Dec 18 '13 at 15:57
  • @joostmakaay, I'll be honest, I don't know. However, go through all of your code and see if you're trying to build a `new` label for `label4` and end up assigning `null`. It's odd, but it's tracable somewhere in your code. – Mike Perrenoud Dec 18 '13 at 15:58
  • @joostmakaay probably `label4` is not the label you think it is, but some other. Go to the windows form designer and check it again – Julián Urbano Dec 18 '13 at 16:05
  • these are all the labels i have in my code, under the FormMain.Designer.cs `private System.Windows.Forms.Label label1; private System.Windows.Forms.Label label2; private System.Windows.Forms.Label label3; public System.Windows.Forms.Label label4; public System.Windows.Forms.Label label5; ` – joostmakaay Dec 18 '13 at 16:06
  • @joostmakaay yes, you have them declared in your class, but not necessarily initialized, that's why it is null. – Julián Urbano Dec 18 '13 at 16:09
  • And how do i do that? – joostmakaay Dec 18 '13 at 16:09
  • @joostmakaay, somewhere you need to say `label4 = new Label();`. – Mike Perrenoud Dec 18 '13 at 16:11
  • The call to Execute that call ShowText is made before the call to InitializeComponent. – Steve Dec 18 '13 at 18:57
  • @Steve, thanks a lot. The OP apparently added the entire set of code after I was working with him. That is 100% the cause of the issue! – Mike Perrenoud Dec 18 '13 at 18:58