问题
I have 3 c# classes in that I have 2 lists, when I trying to make a json string, I got Object reference not set to an instance of an object Error.
I have tried with one list. it perfectly works, but 2 classes or more classes have the list in my program, it not worked. pls help me resolve.
Model Json -
{
"accessKey": "7eb228097576abf56968e9845ab51b90",
"channelId": "103",
"hotels": [
{
"hotelId": "2",
"rooms": [
{
"roomId": "1"
}
]
}
]
}
C# Classes -
public class RootObject
{
public string accessKey { get; set; }
public string channelId { get; set; }
public List<Hotel> hotels { get; set; }
}
public class Hotel
{
public string hotelId { get; set; }
public List<Room> rooms { get; set; }
}
public class Room
{
public string roomId { get; set; }
}
C#
public string cc() {
string s = "";
RootObject ro = new RootObject();
ro.accessKey = "7eb228097576abf56968e9845ab51b90";
ro.channelId = "103";
ro.hotels = new List<Hotel>();
Hotel h = new Hotel();
Room r = new Room();
string config = "server=localhost;username=someuser;password=somepwd;database=db";
MySqlConnection connection = new MySqlConnection(config);
string query = "select * from test1";
MySqlCommand command = new MySqlCommand(query, connection);
connection.Open();
MySqlDataReader Reader = command.ExecuteReader();
while (Reader.Read())
{
r.roomId = Reader[2].ToString();
}
connection.Close();
query = "select * from test1";
command = new MySqlCommand(query, connection);
connection.Open();
Reader = command.ExecuteReader();
while (Reader.Read())
{
h.hotelId = Reader[1].ToString();
}
connection.Close();
h.rooms.Add(r); // Object reference not set to an instance of an object Error
ro.hotels.Add(h);
JavaScriptSerializer js = new JavaScriptSerializer();
s = js.Serialize(ro);
return s;
}
回答1:
You need to initialise the Rooms property. Add a constructor to the Hotel calss that does so
public class Hotel
{
public Hotel(string hotelId){
this.hotelId = hotelId;
this.rooms = New List<Room>();
}
public string hotelId { get; private set; }
public List<Room> rooms { get; private set; }
}
then you'd instantiate a room like this new Hotel(hotelId)
It's generally a good idea to keep what should not be changed encapsulated. Keys should usually not be changed from outside of the object. E.g. would it make sense to change the hotelId and keep the list of rooms?
It's recommended not to expose a setter for collections at all. Usually it's enough to expose a getter and often it's even better to expose only the required operations E.g. in this case to expose a AddRoom
operation rather than exposing the list of rooms
In your code you are looping over all records in a data set (and you're doing this twice) and are overriding the value for hotelId. Ie you are only using the last record in the data set both for hotelId and for room
回答2:
initialize rooms field. I guess it is set to null.
EDIT:
You are initializing only hotels field in the code. Good practice is to initialize fields in constructor.
回答3:
Just initialize list of rooms.
public class Hotel
{
public Hotel()
{
rooms = new List<Room>();
}
public string hotelId { get; set; }
public List<Room> rooms { get; set; }
}
回答4:
The r
is only getting filled up with the Id or other data, when there is a while
loop. It might not get executed because of the condition. That is why the r
is still null
.
When you pass a null
value, this exception comes up. Try to give a simple id
to it outside of the while
loop.
Something like this:
Room r = new Room();
r.roomId = 1;
Then in the code below, alter it using while
loop. If loop executed it would change the value, otherwise you'll have a non null, 1 value.
来源:https://stackoverflow.com/questions/24360849/object-reference-not-set-error-in-json-serialize-in-lists