问题描述
- C#交叉数组出现未将对象引用设置到对象的实例 问题
-
代码其实很简单,创建一个C#窗体应用程序,运行时输入“妈妈和小女孩在动物园看熊猫”,但总是报错,却又解决不了。求解```using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Text.RegularExpressions;namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string[][] data = new string[25][];data[0] = new string[] { "妈妈", "麻麻", "老妈", "老麻", "母亲", "娘", "老娘", "阿姨", "女士", "女人", "姐姐", "婶婶", "女朋友", "老婆", "女性", "她" }; data[1] = new string[] { "书房", "房间", "工作室", "家里", "工作间" }; data[3] = new string[] { "工作", "学习", "看书" }; data[4] = new string[] { "电话", "手机" }; data[4] = new string[] { "书架", "书柜", "书橱", "书桌" }; data[5] = new string[] { "翻阅", "查阅", "阅览", "查找", "查询", "查找", "查", "阅读" }; data[6] = new string[] { "开灯", "照亮", "光明" }; data[7] = new string[] { "水族馆", "海底世界", "大海", "水里", "水下", "水中", "海中", "深海" }; data[8] = new string[] { "鲸", "鲸鱼" }; data[9] = new string[] { "鲨", "鲨鱼", "大白鲨" }; data[10] = new string[] { "水母" }; data[11] = new string[] { "动物园", "动物世界" }; data[12] = new string[] { "小女孩", "女孩", "女子", "女纸", "女生", "女森", "女儿", "妹妹" }; data[13] = new string[] { "熊猫", "大熊猫" }; data[14] = new string[] { "竹", "竹子" }; data[15] = new string[] { "草", "草原", "草丛" }; data[16] = new string[] { "马", "马匹", }; data[17] = new string[] { "鱼", "鱼群" }; data[18] = new string[] { "珊瑚", "珊瑚海", "珊瑚礁" }; data[19] = new string[] { "章鱼", "乌贼" }; data[20] = new string[] { "鼠标" }; data[21] = new string[] { "台灯", "灯" }; data[22] = new string[] { "熊" }; data[23] = new string[] { "虎" }; data[24] = new string[] { "狮子" }; string[] resource; resource = new string[25]; resource[0] = "x-010100"; resource[1] = " x-010200"; resource[2] = " x-010300"; resource[3] = " x-010400"; resource[4] = " x-010500"; resource[5] = " x-010600"; resource[6] = " x-010700"; resource[7] = " x-010800"; resource[8] = " x-010900"; resource[9] = " x-020100"; resource[10] = " x-020200"; resource[11] = " x-020300"; resource[12] = " x-020400"; resource[13] = " x-020500"; resource[14] = " x-020600"; resource[15] = " x-020700"; resource[16] = " x-030100"; resource[17] = " x-030200"; resource[18] = " x-030300"; resource[19] = " x-030400"; resource[20] = " x-030500"; resource[21] = " x-030600"; resource[22] = " x-030700"; resource[23] = " x-040100"; resource[24] = " x-040200"; string information; information = Console.ReadLine(); //获取查询的字符串 string statics; for (int i = 0; i < data.Length; i++) { for (int j = 0; j < data[i].Length; j++) { Regex reg = new Regex(data[i][j]); if (reg.IsMatch(information)) { statics = resource[i] + " "; Console.WriteLine("你要的代码串为{0}", statics); } } } Console.ReadKey(); } }
}
解决方案
data[1] = new string[] { "书房", "房间", "工作室", "家里", "工作间" };
data[3] = new string[] { "工作", "学习", "看书" };
明显data[2]没有赋值,你循环的时候就null了
解决方案二:
楼上已解,初学者,凡事需谨慎
时间: 2025-01-01 10:13:52