沐紮 ˙Ꙫ˙ 2023-06-20 15:48 采纳率: 0%
浏览 138

关于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;
            }
        }
    }

  • 写回答

2条回答 默认 最新

  • 於黾 2023-06-20 15:52
    关注

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

    评论 编辑记录

报告相同问题?

问题事件

  • 创建了问题 6月20日