Click here to Skip to main content
15,904,348 members
Please Sign up or sign in to vote.
2.33/5 (2 votes)
See more:
Hi,

I have objects of common classes like this

C#
public static CANBusDetails CANBusRedDetails = null;
public static CANBusDetails CANBusYellowDetails = null;
public static CANBusDetails CANBusGreenDetails = null;
public static CANBusDetails CANBusOrangeDetails = null;


I defined CANBusDetails as class. When I am trying to loop through objects of common classes


C#
for (int i = 1; i < CANBusRedDetails.CANBusMsgIDList.Count; i++)


I am getting Nullreference exception

Can anyone help me in solving the problem???

Thanks
John
Posted
Comments
Pheonyx 3-Jan-14 9:14am    
You have been given solutions to solve the "nullreference" exception, but you are saying they don't work. Are they giving the same error or a different one? How are you defining your class CANBusDetails? You said with regards to Solution 3 that the class does not have a constructor that takes 0 arguments? What constructors have you defined in the class?
dan!sh 3-Jan-14 9:16am    
Please show code where you have defined CANBusMsgIDList property/member. Also, the bits where it is assigned any value.
Member 10408451 3-Jan-14 9:38am    
ok,thank you for your suggestions
I have arguments in my constructor which I am not passing
Now I solved the problem

Because you have initialized the object as null. You will need to use new keyword to initialize the object. And if you have a constructor defined, it will be called and members initialization code, if any, will be executed.

Currently your object points to null and thus you are getting a null reference error.
 
Share this answer
 
Comments
Member 10408451 3-Jan-14 8:36am    
I tried to use new keyword but showing some error like
does not contains a constructor that takes 0 arguments
Ankur\m/ 3-Jan-14 9:36am    
The error message says it all. You have a constructor defined in the class which accepts sone parameter. When u typed new className(), the intellisense must have shown what parameters are required. Try to find the method named CANBusRedDetails in your class and see what parameters it accepts. Pass those while creating the object.
you really need to get the fundamentals clear before you start programming.
hi Try like this..

might be CANBusRedDetails is null, use the following to avoid null reference error
C#
if( CANBusRedDetails.CANBusMsgIDList != null)
       for (int i = 1; i < CANBusRedDetails.CANBusMsgIDList.Count; i++)
 
Share this answer
 
v2
Comments
Member 10408451 3-Jan-14 8:35am    
I tried it
this does not solve my problem
Karthik_Mahalingam 3-Jan-14 8:43am    
apply break point and find in which line you are getting error ?
you are getting Nullreference excption because the object is null.

To loop through the object, you can use foreach
foreach (var obj in CANBusRedDetails.CANBusMsgIDList)
{

obj.yourObjectAttributes
...

}


Hope this will help you.

Thanks,
Amol
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900