我试图从类中的循环返回一个数组,但是这样做时我收到错误“NullReferenceException is Unhandled”
我可以返回一个没有任何问题的字符串,但是返回一个数组会带来这个错误。
这是我的代码:
class OleDB
{
string[] test;
public string [] readexcel()
{
//Below runs the OleDB connections which essentially turns excel into an Access database.
string conn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:/Users/nlongmore/Documents/Ebay Click and Collect/Ebay MIP/MIP - UK - General Metadata.xlsx;Extended Properties=\"Excel 12.0;HDR=YES;\"";
//Parameters for OleDB
using (OleDbConnection connection = new OleDbConnection(conn))
{ //Opens the connection
connection.Open();
OleDbCommand command = new OleDbCommand("select Value from [Domestic Shipping Service$]", connection); //Searches the column Value from the Worksheet DSS
using (OleDbDataReader dr = command.ExecuteReader())
{ //Reads in the data
for (int x = 0; x < 23; x++)
{
while (dr.Read())
{
var details = Convert.ToString(dr[0]);
test[x] = details;
}
}
connection.Close();
}
}
return test;
}
}
任何建议都会很棒。我还在学习很多关于C#的知识,所以可能错过了一些完全明显的东西。如果是这种情况,请道歉。
答案 0 :(得分:3)
您不创建实例。你只是在宣布考试; 您需要创建一个实例:
string[] test = new string[sizeOfTheArray];
但是,您不需要将test声明为私有字段。
顺便说一下:你的代码没有任何意义。您覆盖test[x]
的次数与dr.Read()
一样多true
。
for (int x = 0; x < 23; x++)
{
while (dr.Read())
{
var details = Convert.ToString(dr[0]);
test[x] = details;
}
}
如果while循环运行大约10次,则会丢失9个值。您只是存储最后一个值。
如果您不期望这种行为,我建议您使用List<string>
。
class OleDB
{
public string [] readexcel()
{
List<string> test = new List<string>();
//Below runs the OleDB connections which essentially turns excel into an Access database.
string conn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:/Users/nlongmore/Documents/Ebay Click and Collect/Ebay MIP/MIP - UK - General Metadata.xlsx;Extended Properties=\"Excel 12.0;HDR=YES;\"";
//Parameters for OleDB
using (OleDbConnection connection = new OleDbConnection(conn))
{ //Opens the connection
connection.Open();
OleDbCommand command = new OleDbCommand("select Value from [Domestic Shipping Service$]", connection); //Searches the column Value from the Worksheet DSS
using (OleDbDataReader dr = command.ExecuteReader())
{ //Reads in the data
for (int x = 0; x < 23; x++)
{
while (dr.Read())
{
var details = Convert.ToString(dr[0]);
test.Add(details);
}
}
connection.Close();
}
}
return test.ToArray();
}
}
答案 1 :(得分:1)
您没有在任何地方初始化数组,它的默认值为NULL
。
string[] test = new string[10];
会准备你的数组包含10个元素。 但是,这是不灵活的。更好地使用可变的东西,比如List.
using System.Collections.Generic;
List<string> test = new List<string>();
...
test.Add(details);
...
return test.ToArray();
顺便说一句:您的原始代码执行循环,然后循环数据读取器。因此,您将多次覆盖test[x]
的内容。我怀疑这不是故意的。
答案 2 :(得分:1)
你需要新的&#39;声明数组时
string[] test = new string[24];
答案 3 :(得分:1)
您将test
声明为成员变量,但未对其进行初始化,因此默认值为null
。
在任何情况下,您都不希望返回成员变量,而是将其定义为局部变量。
class OleDB
{
public string [] readexcel()
{
string[] test = new string[23];
//Below runs the OleDB connections which essentially turns excel into an Access database.
string conn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:/Users/nlongmore/Documents/Ebay Click and Collect/Ebay MIP/MIP - UK - General Metadata.xlsx;Extended Properties=\"Excel 12.0;HDR=YES;\"";
//Parameters for OleDB
using (OleDbConnection connection = new OleDbConnection(conn))
{ //Opens the connection
connection.Open();
OleDbCommand command = new OleDbCommand("select Value from [Domestic Shipping Service$]", connection); //Searches the column Value from the Worksheet DSS
using (OleDbDataReader dr = command.ExecuteReader())
{ //Reads in the data
for (int x = 0; x < 23; x++)
{
while (dr.Read())
{
var details = Convert.ToString(dr[0]);
test[x] = details;
}
}
connection.Close();
}
}
return test;
}
}