none
*URGENT* Why am I getting this error? (please help!!!) RRS feed

  • Question

  • I am doing an assignment for C# on Huffman Coding using a priority queue and a binary heap, and I thought I had figured it out, but when I try to run the program, I get an error on line 47 on the HuffmanTree code file (3rd code block I inserted). It says Object reference not set to an instance of an object and node was null. Please help me figure out why I am getting this error and how to fix it, thank you.

    using System.Collections.Generic;
    using System.Linq;
    
    namespace Huffman1
    {
        internal class PriorityQueue<T>
        {
            private readonly SortedDictionary<int, Queue<T>> _sortedDictionary = new SortedDictionary<int, Queue<T>>();
    
            public int Count { get; private set; }
    
            public void Enqueue(T item, int priority)
            {
                ++Count;
                if (!_sortedDictionary.ContainsKey(priority)) _sortedDictionary[priority] = new Queue<T>();
                _sortedDictionary[priority].Enqueue(item);
            }
    
            public T Dequeue()
            {
                --Count;
                var item = _sortedDictionary.First();
                if (item.Value.Count == 1) _sortedDictionary.Remove(item.Key);
                return item.Value.Dequeue();
            }
        }
    }

    // HuffmanNode.cs
    namespace Huffman1
    {
        internal class HuffmanNode
        {
            public HuffmanNode Parent { get; set; }
            public HuffmanNode Left { get; set; }
            public HuffmanNode Right { get; set; }
            public char Value { get; set; }
            public int Count { get; set; }
        }
    }

    using System.Collections;
    using System.Collections.Generic;
    
    namespace Huffman1
    {
        internal class HuffmanTree
        {
            private readonly HuffmanNode _root;
            private IDictionary counts;
    
            public HuffmanTree(IEnumerable<KeyValuePair<char, int>> counts)
            {
                var priorityQueue = new PriorityQueue<HuffmanNode>();
    
                foreach (KeyValuePair<char, int> kvp in counts)
                {
                    priorityQueue.Enqueue(new HuffmanNode { Value = kvp.Key, Count = kvp.Value }, kvp.Value);
                }
    
                while (priorityQueue.Count > 1)
                {
                    HuffmanNode n1 = priorityQueue.Dequeue();
                    HuffmanNode n2 = priorityQueue.Dequeue();
                    var n3 = new HuffmanNode { Left = n1, Right = n2, Count = n1.Count + n2.Count };
                    n1.Parent = n3;
                    n2.Parent = n3;
                    priorityQueue.Enqueue(n3, n3.Count);
                }
    
                _root = priorityQueue.Dequeue();
            }
    
            public HuffmanTree(IDictionary counts)
            {
                this.counts = counts;
            }
    
            public IDictionary<char, string> CreateEncodings()
            {
                var encodings = new Dictionary<char, string>();
                Encode(_root, "", encodings);
                return encodings;
            }
    
            private void Encode(HuffmanNode node, string path, IDictionary<char, string> encodings)
            {
                if (node.Left != null)
                {
                    Encode(node.Left, path + "0", encodings);
                    Encode(node.Right, path + "1", encodings);
                }
                else
                {
                    encodings.Add(node.Value, path);
                }
            }
        }
    }

    using System;
    using System.Collections;
    using System.Collections.Generic;
    
    namespace Huffman1
    {
        class Program
        {
            static void Main(string[] args)
            {
                IDictionary counts = new Dictionary<char, int>();
                // ac bca ba z
                counts.Add(' ', 3);
                counts.Add('b', 2);
                counts.Add('a', 3);
                counts.Add('c', 2);
                counts.Add('z', 1);
                counts.Add('\n', 1);
                HuffmanTree tree = new HuffmanTree(counts);
                IDictionary<char, string> encodings = tree.CreateEncodings();
    
                foreach (KeyValuePair<char, string> kvp in encodings)
                {
                    Console.WriteLine((kvp.Key == '\n' ? "EOF" : kvp.Key.ToString()) + ":\t" + kvp.Value);
                }
    
                Console.ReadLine();
            }
        }
    }

    Tuesday, November 6, 2018 12:49 AM

Answers

  • Hi goofy_goober,

    Please refer to the following code.

    private void Encode(HuffmanNode node, string path, IDictionary<char, string> encodings)
            {
                if (node?.Left != null)
                {
                    Encode(node.Left, path + "0", encodings);
                    Encode(node.Right, path + "1", encodings);
                }
                else
                {
                    encodings.Add(node == null ? '\0' : node.Value, path);
                }
            }

    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Tuesday, November 6, 2018 1:43 AM
    Moderator

All replies

  •         private void Encode(HuffmanNode node, string path, IDictionary<char, string> encodings)
            {
                if (node?.Left != null)
                {
                    Encode(node.Left, path + "0", encodings);
                    Encode(node.Right, path + "1", encodings);
                }
                else
                {
                    encodings.Add(node.Value, path);
                }
            }

    Tuesday, November 6, 2018 12:55 AM
  • thanks, this resolves that error but now the same error is occurring on line 54. How do I resolve this?
    Tuesday, November 6, 2018 1:17 AM
  • Hi goofy_goober,

    Please refer to the following code.

    private void Encode(HuffmanNode node, string path, IDictionary<char, string> encodings)
            {
                if (node?.Left != null)
                {
                    Encode(node.Left, path + "0", encodings);
                    Encode(node.Right, path + "1", encodings);
                }
                else
                {
                    encodings.Add(node == null ? '\0' : node.Value, path);
                }
            }

    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Tuesday, November 6, 2018 1:43 AM
    Moderator
  • Thank you, that fixed the errors! But, I'm still a bit confused as to why nothing but a colon shows up when the program is ran rather than the nodes. (Sorry, I am very new to C# and am having a hard time with this.
    Tuesday, November 6, 2018 2:04 AM
  • Hi goofy_goober,

    Please modify your main method like this:

    static void Main(string[] args)
            {
                List<KeyValuePair<char, int>> counts = new List<KeyValuePair<char, int>>();
                // ac bca ba z
                counts.Add(new KeyValuePair<char, int>(' ', 3));
                counts.Add(new KeyValuePair<char, int>('b', 2));
                counts.Add(new KeyValuePair<char, int>('a', 3));
                counts.Add(new KeyValuePair<char, int>('c', 2));
                counts.Add(new KeyValuePair<char, int>('z', 1));
                counts.Add(new KeyValuePair<char, int>('\n', 1));
    
                HuffmanTree tree = new HuffmanTree(counts);
                IDictionary<char, string> encodings = tree.CreateEncodings();
    
                foreach (KeyValuePair<char, string> kvp in encodings)
                {
                    Console.WriteLine((kvp.Key == '\n' ? "EOF" : kvp.Key.ToString()) + ":\t" + kvp.Value);
                }
    
                Console.ReadLine();
            }

    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, November 6, 2018 2:22 AM
    Moderator