Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

NullReference

Name: Anonymous 2008-03-02 9:32

Why are lines 12 and 7 returning a NullReferenceException?

    using System;

    public class LiteralDecoder : StringReplacer
    {
        public static LiteralDecoder Default = new LiteralDecoder();
        private static string[][] m_Codes = new string[][] { new string[] { @"\\", @"\" }, new string[] { @"\t", "\t" }, new string[] { @"\n", "\n" }, new string[] { @"\r", "\r" } };

        public LiteralDecoder()
        {
            for (int i = 0; i < m_Codes.Length; i++)
            {
                base.Register(m_Codes[i][0], m_Codes[i][1]);
            }
        }
    }
}


Here is StringReplacer:

namespace Sallos
{
    using System;
    using System.Collections;
    using System.Globalization;
    using System.Text;

    public class StringReplacer
    {
        private ArrayList[] m_Entries = new ArrayList[0x100];

        private Entry FindEntry(string text, int offset)
        {
            char index = text[offset];
            if ((index >= '\0') && (index < this.m_Entries.Length))
            {
                ArrayList list = this.m_Entries[index];
                if (list != null)
                {
                    for (int i = 0; i < list.Count; i++)
                    {
                        Entry entry = (Entry) list[i];
                        if (entry.IsMatch(text, offset))
                        {
                            return entry;
                        }
                    }
                }
            }
            return null;
        }

        public void Register(StringReplacer replacer)
        {
            for (int i = 0; i < replacer.m_Entries.Length; i++)
            {
                ArrayList c = replacer.m_Entries[i];
                if (c != null)
                {
                    ArrayList list2 = this.m_Entries[i];
                    if (list2 == null)
                    {
                        this.m_Entries[i] = list2 = new ArrayList(c.Count);
                    }
                    list2.AddRange(c);
                }
            }
        }

        private void Register(char c, Entry entry)
        {
            if ((c >= '\0') && (c < this.m_Entries.Length))
            {
                ArrayList list = this.m_Entries[c];
                if (list == null)
                {
                    this.m_Entries[c] = list = new ArrayList();
                }
                list.Add(entry);
            }
        }

        public void Register(string input, string output)
        {
            if (((input != null) && (output != null)) && (input.Length != 0))
            {
                char c = input[0];
                Entry entry = new Entry(input, output);
                this.Register(char.ToLower(c), entry);
                this.Register(char.ToUpper(c), entry);
            }
        }

        public string Replace(string text)
        {
            StringBuilder builder = null;
            int offset = 0;
            while (offset < text.Length)
            {
                Entry entry = this.FindEntry(text, offset);
                if (entry == null)
                {
                    if (builder != null)
                    {
                        builder.Append(text[offset]);
                    }
                    offset++;
                }
                else
                {
                    if (builder == null)
                    {
                        builder = new StringBuilder(text, 0, offset, text.Length + 40);
                    }
                    builder.Append(entry.m_Output);
                    offset += entry.m_Input.Length;
                }
            }
            if (builder == null)
            {
                return text;
            }
            return builder.ToString();
        }

        private class Entry
        {
            private static CompareInfo m_Comparer = CultureInfo.CurrentUICulture.CompareInfo;
            public string m_Input;
            public string m_Output;

            public Entry(string input, string output)
            {
                this.m_Input = input;
                this.m_Output = output;
            }

            public bool IsMatch(string text, int offset)
            {
                if ((offset + this.m_Input.Length) > text.Length)
                {
                    return false;
                }
                return (m_Comparer.Compare(text, offset + 1, this.m_Input.Length - 1, this.m_Input, 1, this.m_Input.Length - 1, CompareOptions.IgnoreCase) == 0);
            }
        }
    }
}


Thanks in advance...

Name: Anonymous 2008-03-02 9:35

NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL

Name: Anonymous 2008-03-02 9:36

Sage.

Name: Anonymous 2008-03-02 9:40

The problem is with StringReplacer.Register. Not exactly sure what it is, though...

Name: Anonymous 2008-03-02 9:48

m_Codes is not being initialized properly.

Name: Anonymous 2008-03-02 9:49

>>5

How not so?

Name: Anonymous 2010-12-09 13:59

Name: Anonymous 2010-12-23 5:33


Don't change these.
Name: Email:
Entire Thread Thread List