关于c#的问题,System.NullReferenceException: 未将对象引用设置到对象的实例

一个作业出现了这个警告System.NullReferenceException: 未将对象引用设置到对象的实例 马上交了球球大佬救救我
源代码:



using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
    public partial class Form7 : Form 

    {
            static void Main(string[] args)
            {
                // 读取DEM数据文件
                string filePath = "dem_data.txt";
                int[,] demData = ReadDataFile(filePath);
                // 计算剖面区域的面积
                double profileArea = CalculateProfileArea(demData);
                Console.WriteLine("剖面面积为:" + profileArea.ToString() + "平方米");
                Console.ReadLine();
            }
            // 从文件中读取DEM数据
            static int[,] ReadDataFile(string filePath)
            {
                int[,] data = null;
                if (File.Exists(filePath))
                {
                    string[] lines = File.ReadAllLines(filePath);
                    int rowCount = lines.Length;
                    int colCount = lines[0].Split(' ').Length;
                    data = new int[rowCount, colCount];
                    for (int i = 0; i < rowCount; i++)
                    {
                        string[] rowValues = lines[i].Split(' ');
                        for (int j = 0; j < colCount; j++)
                        {
                            int value = Convert.ToInt32(rowValues[j]);
                            data[i, j] = value;
                        }
                    }
                }
                else
                {
                    Console.WriteLine("文件不存在!");
                }
                return data;
            }
            // 计算剖面区域的面积
            static double CalculateProfileArea(int[,] data)
            {
                double area = 0.0;
                // 剖面线的高度(假设为第1列)
                int profileHeight = data[0,0];
                for (int i = 1; i < data.GetLength(0); i++)
                {
                    int curHeight = data[i, 0];
                    if (curHeight > profileHeight)
                    {
                        // 面积增加的高度差
                        int heightDiff = curHeight - profileHeight;
                        // 计算面积增加的梯形面积
                        double trapezoidArea = ((double)profileHeight + (double)curHeight) / 2.0 * 1.0;
                        // 累加面积
                        area += trapezoidArea * heightDiff;
                        // 更新剖面线高度
                        profileHeight = curHeight;
                    }
                }
                return area;
            }
        }
    }

ReadDataFile函数中,如果文件不存在,返回值是null
也就是说,int[,] demData = ReadDataFile(filePath);得到的是个null
那么后续传递进CalculateProfileArea里面之后,执行到data[0,0];的时候,对null执行取值肯定抛异常了
在执行后续计算之前,先做非空判断

  • 帮你找了个相似的问题, 你可以看下: https://ask.csdn.net/questions/7445838
  • 我还给你找了一篇非常好的博客,你可以看看是否有帮助,链接:C#System.NullReferenceException:未将对象引用设置到对象的实例
  • 除此之外, 这篇博客: C#-错误:System.NullReferenceException:未将对象引用设置到对象的实例中的 原因 部分也许能够解决你的问题, 你可以仔细阅读以下内容或跳转源博客中阅读:
  • 本案例中出现错误的原因是:对象在使用之前未进行初始化

    分析:

    在一开始,只是声明了一个 Timer 类的对象 sendToMachineTimer ,并未对其进行实例化。说白了,目前是“有名无分”。

    随后 Connet_Click 事件中调用 ConnectToMachine() 方法,在该方法中,直接执行语句:

    sendToMachineTimer.Change(0, 500);
    

    问题来了,sendToMachineTimer 刚刚只是有了“名”而已,并没有“实权”,即没有进行实例化。自然的,此时sendToMachineTimer 是无法调用 Timer 中的方法的。因而,直接报错。