0

I stopped at one point writing c# code for Unity. I can't figure it out, do you guys have any ideas?

NullReferenceException: Object reference not set to an instance of an object

ConsoleLSystem.LSystemEvaluator._rewrite (ConsoleLSystem.LSystemNode node) (at Assets/Scripts/LSystem/Program.cs:157)

This is code from line 141 to 162. Here is the full code: https://pastebin.com/tL8inez5

public class LSystemEvaluator {
    public LSystemNode lSystemString { get; set; }
    public List<LSystemRule> lSystemRules { get; set; }
    public String[] ignored { get; set; }

    public LSystemEvaluator(LSystemNode startingString, List<LSystemRule> rules) {
        lSystemString = startingString;
        lSystemRules = rules;
        ignored = new String[0];
    }
    public LSystemEvaluator(LSystemNode startingString, List<LSystemRule> rules, String[] ignored) {
        lSystemString = startingString;
        lSystemRules = rules;
        this.ignored = ignored;
    }
    private LSystemNode _rewrite(LSystemNode node) {
        foreach (var rule in lSystemRules) {
            if (rule.is_aplicable(node, ignored)) {
                return rule.rewrite(node, ignored);
            }
        }
        return new LSystemNode(node.literal);
  • 1
    The only resolution for you to your problem would be to learn debugging the code. put some debug points before line of the error and pass line by line by hovering the objects and properties, verify each line and property that isn't null. there must be a null property or object. – Syed Muhammad Munis Ali May 21 '21 at 13:42
  • and by looking at your full code line no 157 is "foreach (var rule in lSystemRules) {", might be this list is null "lSystemRules". again it's time to debug the code. – Syed Muhammad Munis Ali May 21 '21 at 13:44
  • Thanks for the reply, it is indeed time to learn debugging. I'm a total layman in programming, and this program is for a needed school project :) – Denis Pagowski May 21 '21 at 13:45
  • hope it might help you to put some initial debug points and start escaping lines. https://docs.unity3d.com/Manual/ManagedCodeDebugging.html – Syed Muhammad Munis Ali May 21 '21 at 13:52
  • oh, this will probably help, thank you very much! :) – Denis Pagowski May 21 '21 at 13:54

0 Answers0