在 C# 中处理 NullReferenceException
Handling a NullReferenceException in C#
我在线上收到 NullReferenceException:
件数[尺寸] = 名称; //这一行在插入函数的代码中途。
我明白我为什么会收到它,但我该如何处理它?
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace IP01
{
public class Program
{
public class MinHeap
{
static string[] items;
int size = 0;
static void Main(string[] args)
{
string[] array = new string[5] { "f", "a", "c", "d", "e" };
Console.Write("Original Array: ");
for (int i = 0; i < array.Length; i++)
Console.Write(" " + array[i]);
Console.WriteLine();
MinHeap heap = new MinHeap();
heap.createHeap(array);
heap.sort();
Console.Write("Sorted Array: ");
for (int i = 0; i < items.Length; i++)
Console.Write(" " + items[i]);
Console.WriteLine();
}
public void createHeap(string[] array)
{
if (array.Length > 0)
{
for (int i = 0; i < array.Length; i++)
insert(array[i]);
}
}
public void rebuild(int rootIndex, int n)
{
int max = 2 * rootIndex + 1;
bool larger;
int less = string.Compare(items[max], items[max + 1]);
if (less < 0)
larger = true;
else
larger = false;
bool larger2;
int less2 = string.Compare(items[rootIndex], items[max]);
if (less2 < 0)
larger2 = true;
else
larger2 = false;
if ((2 * rootIndex + 1) < n)
{
if ((2 * rootIndex + 2) < n)
{
max = larger ? max + 1 : max;
}
if (larger2)
{
string temp;
temp = items[rootIndex];
items[rootIndex] = items[max];
items[max] = temp;
rebuild(max, n);
}
}
}
public void Heap()
{
size = 0;
}
public bool isEmpty()
{
return size == 0;
}
public void insert(string name)
{
int child;
int parent;
items[size] = name;
child = size;
parent = (child - 1) / 2;
bool larger3;
int less3 = string.Compare(items[parent], items[child]);
if (less3 < 0)
larger3 = true;
else
larger3 = false;
while (parent >= 0 && larger3)
{
string temp;
temp = items[child];
items[child] = items[parent];
items[parent] = temp;
child = parent;
parent = (child - 1) / 2;
}
size++;
}
public void remove()
{
if (size == 0)
{
Console.Write("The heap is empty. No object removed.\n");
}
else
{
items[0] = items[size - 1];
size--;
rebuild(0, size);
}
}
public void sort()
{
for (int i = size - 1; i >= 0; i--)
{
rebuild(i, size);
}
int last = size - 1;
for (int i = 1; i <= size; i++)
{
string temp;
temp = items[0];
items[0] = items[last];
items[last] = temp;
last--;
rebuild(0, last);
}
}
}
}
}
你还没有初始化你的数组。试试这个。也许这对你有帮助。
items=new string[]{""};
items[size] = name;
我在线上收到 NullReferenceException:
件数[尺寸] = 名称; //这一行在插入函数的代码中途。
我明白我为什么会收到它,但我该如何处理它?
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace IP01
{
public class Program
{
public class MinHeap
{
static string[] items;
int size = 0;
static void Main(string[] args)
{
string[] array = new string[5] { "f", "a", "c", "d", "e" };
Console.Write("Original Array: ");
for (int i = 0; i < array.Length; i++)
Console.Write(" " + array[i]);
Console.WriteLine();
MinHeap heap = new MinHeap();
heap.createHeap(array);
heap.sort();
Console.Write("Sorted Array: ");
for (int i = 0; i < items.Length; i++)
Console.Write(" " + items[i]);
Console.WriteLine();
}
public void createHeap(string[] array)
{
if (array.Length > 0)
{
for (int i = 0; i < array.Length; i++)
insert(array[i]);
}
}
public void rebuild(int rootIndex, int n)
{
int max = 2 * rootIndex + 1;
bool larger;
int less = string.Compare(items[max], items[max + 1]);
if (less < 0)
larger = true;
else
larger = false;
bool larger2;
int less2 = string.Compare(items[rootIndex], items[max]);
if (less2 < 0)
larger2 = true;
else
larger2 = false;
if ((2 * rootIndex + 1) < n)
{
if ((2 * rootIndex + 2) < n)
{
max = larger ? max + 1 : max;
}
if (larger2)
{
string temp;
temp = items[rootIndex];
items[rootIndex] = items[max];
items[max] = temp;
rebuild(max, n);
}
}
}
public void Heap()
{
size = 0;
}
public bool isEmpty()
{
return size == 0;
}
public void insert(string name)
{
int child;
int parent;
items[size] = name;
child = size;
parent = (child - 1) / 2;
bool larger3;
int less3 = string.Compare(items[parent], items[child]);
if (less3 < 0)
larger3 = true;
else
larger3 = false;
while (parent >= 0 && larger3)
{
string temp;
temp = items[child];
items[child] = items[parent];
items[parent] = temp;
child = parent;
parent = (child - 1) / 2;
}
size++;
}
public void remove()
{
if (size == 0)
{
Console.Write("The heap is empty. No object removed.\n");
}
else
{
items[0] = items[size - 1];
size--;
rebuild(0, size);
}
}
public void sort()
{
for (int i = size - 1; i >= 0; i--)
{
rebuild(i, size);
}
int last = size - 1;
for (int i = 1; i <= size; i++)
{
string temp;
temp = items[0];
items[0] = items[last];
items[last] = temp;
last--;
rebuild(0, last);
}
}
}
}
}
你还没有初始化你的数组。试试这个。也许这对你有帮助。
items=new string[]{""};
items[size] = name;