Seminko 40 Posted November 10, 2017 Share Posted November 10, 2017 I'm at the end of my rope. For some reason this code crashes WRobot. I have it in a while loop. Log included... private void FoodManager() { if (sRogueSettings.CurrentSetting.UseBuffFood && sRogueSettings.CurrentSetting.BuffFoodName != null && sRogueSettings.CurrentSetting.BuffFoodBuffName != null) { uint foodID = wManager.Wow.Helpers.ItemsManager.GetIdByName(sRogueSettings.CurrentSetting.BuffFoodName); if (!ObjectManager.Me.HaveBuff(sRogueSettings.CurrentSetting.BuffFoodBuffName) && ItemsManager.HasItemById(foodID) && wManagerSetting.CurrentSetting.FoodName != sRogueSettings.CurrentSetting.BuffFoodName) { wManagerSetting.CurrentSetting.FoodName = sRogueSettings.CurrentSetting.BuffFoodName; } else if (ObjectManager.Me.HaveBuff(sRogueSettings.CurrentSetting.BuffFoodBuffName) && wManagerSetting.CurrentSetting.FoodName != originalFoodSetting) { wManagerSetting.CurrentSetting.FoodName = originalFoodSetting; } } } 10 11 2017 21H30.log.html Link to comment https://wrobot.eu/forums/topic/7620-why-does-this-code-crash-wrobot/ Share on other sites More sharing options...
reapler 154 Posted November 10, 2017 Share Posted November 10, 2017 Hello, if something crashes and it's not a stack overflow (recursive call) you may wrap your code in a try-catch statement: private void FoodManager() { try { if (sRogueSettings.CurrentSetting.UseBuffFood && sRogueSettings.CurrentSetting.BuffFoodName != null && sRogueSettings.CurrentSetting.BuffFoodBuffName != null) { uint foodID = wManager.Wow.Helpers.ItemsManager.GetIdByName(sRogueSettings.CurrentSetting.BuffFoodName); if (!ObjectManager.Me.HaveBuff(sRogueSettings.CurrentSetting.BuffFoodBuffName) && ItemsManager.HasItemById(foodID) && wManagerSetting.CurrentSetting.FoodName != sRogueSettings.CurrentSetting.BuffFoodName) { wManagerSetting.CurrentSetting.FoodName = sRogueSettings.CurrentSetting.BuffFoodName; } else if (ObjectManager.Me.HaveBuff(sRogueSettings.CurrentSetting.BuffFoodBuffName) && wManagerSetting.CurrentSetting.FoodName != originalFoodSetting) { wManagerSetting.CurrentSetting.FoodName = originalFoodSetting; } } } catch (Exception e) { robotManager.Helpful.Logging.WriteError(e.ToString()); } } I think you have probably a null reference exception on your settings, so you would check for "sRogueSettings.CurrentSetting" whether it is null at first. And "sRogueSettings.CurrentSetting.BuffFoodName != null" is not necessary on a string, since checking against null will result always in false. Link to comment https://wrobot.eu/forums/topic/7620-why-does-this-code-crash-wrobot/#findComment-34796 Share on other sites More sharing options...
Seminko 40 Posted November 10, 2017 Author Share Posted November 10, 2017 1 hour ago, reapler said: Hello, if something crashes and it's not a stack overflow (recursive call) you may wrap your code in a try-catch statement: private void FoodManager() { try { if (sRogueSettings.CurrentSetting.UseBuffFood && sRogueSettings.CurrentSetting.BuffFoodName != null && sRogueSettings.CurrentSetting.BuffFoodBuffName != null) { uint foodID = wManager.Wow.Helpers.ItemsManager.GetIdByName(sRogueSettings.CurrentSetting.BuffFoodName); if (!ObjectManager.Me.HaveBuff(sRogueSettings.CurrentSetting.BuffFoodBuffName) && ItemsManager.HasItemById(foodID) && wManagerSetting.CurrentSetting.FoodName != sRogueSettings.CurrentSetting.BuffFoodName) { wManagerSetting.CurrentSetting.FoodName = sRogueSettings.CurrentSetting.BuffFoodName; } else if (ObjectManager.Me.HaveBuff(sRogueSettings.CurrentSetting.BuffFoodBuffName) && wManagerSetting.CurrentSetting.FoodName != originalFoodSetting) { wManagerSetting.CurrentSetting.FoodName = originalFoodSetting; } } } catch (Exception e) { robotManager.Helpful.Logging.WriteError(e.ToString()); } } I think you have probably a null reference exception on your settings, so you would check for "sRogueSettings.CurrentSetting" whether it is null at first. And "sRogueSettings.CurrentSetting.BuffFoodName != null" is not necessary on a string, since checking against null will result always in false. Oh right, didn't think of that, still new and learning :) Yup, it was "Object reference not set to an instance of an object. in Main.FoodManager()" However still don't understand the exception. What is out of place? Everything seems to be set up correctly. How do I pinpoint where the error is? Link to comment https://wrobot.eu/forums/topic/7620-why-does-this-code-crash-wrobot/#findComment-34797 Share on other sites More sharing options...
reapler 154 Posted November 10, 2017 Share Posted November 10, 2017 (edited) 31 minutes ago, Seminko said: How do I pinpoint where the error is? You may check first whether "sRogueSettings.CurrentSetting" is null: private void FoodManager() { try { if (sRogueSettings.CurrentSetting == null) robotManager.Helpful.Logging.Write("CurrentSetting is null"); else robotManager.Helpful.Logging.Write("CurrentSetting is not null"); } catch (Exception e) { robotManager.Helpful.Logging.WriteError(e.ToString()); } } You get probably "CurrentSetting is null" ;) So in this case this means you haven't initialized "CurrentSetting" = no instance to your object is given = "CurrentSetting" is null, hence you cannot accessing its properties. In the end you need to call "sRogueSettings.Load();" to create an instance. However If it's not the case, you may check for other variables against null. Edited November 10, 2017 by reapler Seminko 1 Link to comment https://wrobot.eu/forums/topic/7620-why-does-this-code-crash-wrobot/#findComment-34798 Share on other sites More sharing options...
Seminko 40 Posted November 11, 2017 Author Share Posted November 11, 2017 10 hours ago, reapler said: You may check first whether "sRogueSettings.CurrentSetting" is null: private void FoodManager() { try { if (sRogueSettings.CurrentSetting == null) robotManager.Helpful.Logging.Write("CurrentSetting is null"); else robotManager.Helpful.Logging.Write("CurrentSetting is not null"); } catch (Exception e) { robotManager.Helpful.Logging.WriteError(e.ToString()); } } You get probably "CurrentSetting is null" ;) So in this case this means you haven't initialized "CurrentSetting" = no instance to your object is given = "CurrentSetting" is null, hence you cannot accessing its properties. In the end you need to call "sRogueSettings.Load();" to create an instance. However If it's not the case, you may check for other variables against null. I'm stupid... did not have sRogueSettings.Load(); Link to comment https://wrobot.eu/forums/topic/7620-why-does-this-code-crash-wrobot/#findComment-34801 Share on other sites More sharing options...
reapler 154 Posted November 11, 2017 Share Posted November 11, 2017 A small template, also creates an instance on null. Saving manually with "sRogueSettings.CurrentSetting.Save();" public class SRogueSettings : robotManager.Helpful.Settings { public bool UseBuffFood { get; set; } public string BuffFoodName { get; set; } //... public static SRogueSettings CurrentSetting { get { if (_currentSetting == null && !_cannotLoad) Load(); return _currentSetting; } set { _currentSetting = value; } } private static bool _cannotLoad; private static SRogueSettings _currentSetting; public bool Save() { try { _cannotLoad = !Save(AdviserFilePathAndName("SRogue", ObjectManager.Me.Name + "." + Usefuls.RealmName)); return !_cannotLoad; } catch (Exception e) { Logging.WriteError(e.ToString()); _cannotLoad = true; return false; } } public static bool Load() { try { if (File.Exists(AdviserFilePathAndName("SRogue", ObjectManager.Me.Name + "." + Usefuls.RealmName))) { CurrentSetting = Load<SRogueSettings>(AdviserFilePathAndName("SRogue", ObjectManager.Me.Name + "." + Usefuls.RealmName)); return true; } _cannotLoad = true; CurrentSetting = new SRogueSettings { UseBuffFood = true, BuffFoodName = "", }; } catch (Exception e) { Logging.WriteError(e.ToString()); } return false; } } Link to comment https://wrobot.eu/forums/topic/7620-why-does-this-code-crash-wrobot/#findComment-34808 Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now