title: Unexpected Error: An unhandled exception of type 'System.Reflection.TargetInvocationException' occurred in mscorlib.dll
body: <p>I'm building a windows forms program on Visual Studio 2013 (C#) which reads data from Serial Port (in my case - Arduino) and use it to change a picture. The Arduino returns an 8-bit string of 0's and 1's, which indicates what picture to change. For example - if the string is <code>10001001</code> the program should change the 1'st, 5'th and 8'th picture. The program is able to get the correct string from the Serial Port (via Serial Communication), but when I added a method that makes the picture to change, it <strong>sometimes</strong> gives me this error:</p>
  
  <blockquote>
    <p>An unhandled exception of type 'System.Reflection.TargetInvocationException' occurred in mscorlib.dll</p>
  </blockquote>
  
  <p>and point that line on <code>Program.cs</code>:</p>
  
  <pre><code>Application.Run(new Form1());
  </code></pre>
  
  <p>(My form name is "form1.cs" and the namespace is serial_read)</p>
  
  <p>Here's what the program looks like:
  <img src="https://i.stack.imgur.com/CNRtv.png" alt="enter image description here"></p>
  
  <p>And here's my form1.cs code:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.ComponentModel;
  using System.Data;
  using System.Drawing;
  using System.Linq;
  using System.Text;
  using System.Windows.Forms;
  using System.IO.Ports;
  using System.Threading;
  
  namespace Serial_receive
  {
  public partial class Form1 : Form
  {
      SerialPort serialPort1 = new SerialPort(); //Defines Serial Port
  
      public Form1()
      {
          InitializeComponent();
  
          serialPort1.BaudRate = 9600;
          serialPort1.DtrEnable = true;
  
          foreach (string port in SerialPort.GetPortNames()) {comboBox1.Items.Add(port);} //Gives a list of the available ports
  
          serialPort1.DataReceived += serialPort1_DataReceived;
      }
  
     private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
     {
         string line = serialPort1.ReadLine(); //The serial string
         this.BeginInvoke(new LineReceivedEvent(LineReceived), line);
  
     }
     private delegate void LineReceivedEvent(string line);
  
     //Change image to a different image
     private void changeColor(PictureBox ob, string line, int l) //ob = PictureBox, line = the serial string, l = number of PictureBox
     {
         if (int.Parse(line.Substring(l, 1)) == 1)
             ob.Image = Serial_receive.Properties.Resources.red;
     }
  
     private void LineReceived(string line)
     {
         label1.Text = line;
  
         changeColor(l1, line, 0); //Picture 1
         changeColor(l2, line, 1); //Picture 2
         changeColor(l3, line, 2); //Picture 3
         changeColor(l4, line, 3); //Picture 4
         changeColor(l5, line, 4); //Picture 5
         changeColor(l6, line, 5); //Picture 6
         changeColor(l7, line, 6); //Picture 7
         changeColor(l8, line, 7); //Picture 8
  
     }
  
     private void button1_Click(object sender, EventArgs e) //Start Button
     {
         serialPort1.PortName = comboBox1.Text.ToString();
  
         if (comboBox1.Text.ToString() == "Select Port... (Default: COM4)" || comboBox1.Text.ToString() == "")
              serialPort1.PortName = "COM4";
         serialPort1.Open();
         button1.Enabled = false;
     }
  }
  </code></pre>
  
  <p>}</p>
  
  <p>Any ideas why I get this error? I'm kinda new to C# and don't really know what's the problem..</p>