what cause the exception and how to fix itSystemNullReferenceException Object reference not set to an instance of an object at TsunaCampMain+c__DisplayClass16_4Testb__6 AssetsScriptsApplicationCardUn
The exception "System.NullReferenceException: Object reference not set to an instance of an object" occurs when the code is trying to access a member or variable that is null.
In this specific code, the exception is most likely caused by the line:
CardUnificationData cardUnificationData = cards.FirstOrDefault(c =>
{
var cardCampSkill = MasterUtil.GetCampSkillMaster(c.MasterCardId, 1);
var desc_text = MasterUtil.GetCampText(cardCampSkill.desc_text_id);
return c.MemberId == m.chara_id && UselessEffectDict[campid].All(r => !desc_text.Contains(r));
});
The cards collection may be null or empty, causing the FirstOrDefault method to return null, and trying to access properties or methods of a null object would result in a NullReferenceException.
To fix this issue, you can add a null check before accessing the cards collection. For example:
CardUnificationData cardUnificationData = null;
if (cards != null && cards.Any())
{
cardUnificationData = cards.FirstOrDefault(c =>
{
var cardCampSkill = MasterUtil.GetCampSkillMaster(c.MasterCardId, 1);
var desc_text = MasterUtil.GetCampText(cardCampSkill.desc_text_id);
return c.MemberId == m.chara_id && UselessEffectDict[campid].All(r => !desc_text.Contains(r));
});
}
This way, if cards is null or empty, the cardUnificationData variable will remain null, and you can handle this case accordingly in your code
原文地址: https://www.cveoy.top/t/topic/hWFb 著作权归作者所有。请勿转载和采集!