I am following an online tutorial to learn how to write a serial communicator.
I am using VS2019 and .Net 5.0, which is different from the environment used in the tutorial.
To use the SerialPort tool, I try to create :
        static SerialPort serialPort1;
as in the code:
using System;
using System.Windows.Forms;
using System.IO.Ports;
namespace SerialPortCommunication {
    public partial class Form1 : Form {
        static SerialPort serialPort1;
        public Form1() {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e) {
            string[] ports = System.IO.Ports.SerialPort.GetPortNames();
            comboBox1.Items.AddRange(ports);
            comboBox1.SelectedIndex = comboBox1.Items.Count > 0 ? 0 : -1;
        }
        private void button1_Click(object sender, EventArgs e) {
            if (button1.Text == "Open Port") {
                serialPort1.PortName = comboBox1.Text;
                serialPort1.Open();
                button1.Text = "Close Port";
            }
            else {
                serialPort1.Close();
                button1.Text = "Open Port";
            }
            
        }
    }
}
When I executed it , I encountered the System.NullReferenceException: 'Object reference not set to an instance of an object.'
But after I changed it to
static SerialPort serialPort1 = new SerialPort();
the VS doesn't show the Exception anymore.
My quesition is :
I have been told that we don't need and we can not create an instance for a static object, but in this case why do I need to create one instance to initialize the serialPort1?
I am confused...
As a beginner, I may have some unclear concepts. Could you provide me with some guidance or recommend some resources for me to study?
 
     
    