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?
I suspect that if you break that line into two steps and look at what
GetDatareturns, you’ll see it is null.