ASP.NET 中的 NullReferenceException 测试数据集
我编写了一个 3 层网站,其中具有用于访问数据库的单例存储库。我的存储库使用数据集连接到数据库并从数据库进行查询。
我想使用 Visual Studio 2010 测试项目测试该站点,但是当我在存储库中创建数据集的 TableAdapter 时,我在测试应用程序中收到以下错误:
System.NullReferenceException:未将对象引用设置到实例 一个对象的。
当我从站点内部使用存储库时,代码可以正常工作,但在测试应用程序中我收到了该错误。
我收到该错误的存储库之一如下:
public sealed class VehicleRepository
{
private readonly int gateCode;
private readonly VehicleTableAdapter vehicleSet;
private readonly VehicleTypeTableAdapter vehicleTypeSet;
private static VehicleRepository instance;
private VehicleRepository()
{
var configureTable = new ConfigurationTableAdapter();
--->>> var configuration = configureTable.GetData().ToList();
if (configuration.Count == 0)
throw new UserInterfaceException("some message");
if (configuration.Count != 1)
throw new UserInterfaceException("some message");
gateCode = configuration[0].GateCode;
vehicleSet=new VehicleTableAdapter();
vehicleTypeSet=new VehicleTypeTableAdapter();
}
public static VehicleRepository GetInstance()
{
return instance ?? (instance = new VehicleRepository());
}
public Vehicle GetVehicleByPlaque(string plaque)
{
.....
}
private static Vehicle ConvertVehicleRowToVehicle(TransportCo.VehicleRow vehicleRow,TransportCo.VehicleTypeRow vehicleTypeRow)
{
....
}
public void SaveOrUpdate(Vehicle vehicle)
{
...
}
private static void UpdateVehicle(Vehicle vehicle)
{
...
}
}
我在 --->>>>> 中收到错误线。 有人知道问题所在吗?
I have written a 3 layer web site which has singleton repository for accessing database. My repositories use dataset for connecting to and query from database.
I want to test the site using Visual Studio 2010 Test project but when I create TableAdapter of dataset in repository I got following error in test application:
System.NullReferenceException: Object reference not set to an instance
of an object.
the code work correctly when I use repository from the inside of site but in test application I got that error.
one of my repositories which I got that error is follow:
public sealed class VehicleRepository
{
private readonly int gateCode;
private readonly VehicleTableAdapter vehicleSet;
private readonly VehicleTypeTableAdapter vehicleTypeSet;
private static VehicleRepository instance;
private VehicleRepository()
{
var configureTable = new ConfigurationTableAdapter();
--->>> var configuration = configureTable.GetData().ToList();
if (configuration.Count == 0)
throw new UserInterfaceException("some message");
if (configuration.Count != 1)
throw new UserInterfaceException("some message");
gateCode = configuration[0].GateCode;
vehicleSet=new VehicleTableAdapter();
vehicleTypeSet=new VehicleTypeTableAdapter();
}
public static VehicleRepository GetInstance()
{
return instance ?? (instance = new VehicleRepository());
}
public Vehicle GetVehicleByPlaque(string plaque)
{
.....
}
private static Vehicle ConvertVehicleRowToVehicle(TransportCo.VehicleRow vehicleRow,TransportCo.VehicleTypeRow vehicleTypeRow)
{
....
}
public void SaveOrUpdate(Vehicle vehicle)
{
...
}
private static void UpdateVehicle(Vehicle vehicle)
{
...
}
}
I got the error in the --->>> line.
does anyone know the problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我怀疑如果您将该行分成两步并查看
GetData
返回的内容,您会发现它为空。I suspect that if you break that line into two steps and look at what
GetData
returns, you'll see it is null.数据库和测试的一个常见问题是连接字符串通常存储在网站的配置文件中。执行测试时,web.config 通常不会发挥作用。这很容易导致像您所描述的那样的情况。因此,即使您知道它在生产中有效,它也不会说明连接在测试中是否正确设置
One Common catch ya with databases and test is that usually the connection string is stored in the configuration file of the web site. The web.config is usually not in play when executing the tests. That can easily lead to a scenario like the one you describe. So even though you know that it works in production it doesn't say anything about whether the connection is setup up correctly in test