Click here to Skip to main content
15,917,481 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I am using NHunspell on a virtual dedicated server. I am starting/creating the spell check objects as such:
C#
static SpellEngine spellEngine;
static public SpellEngine SpellEngine { get { return spellEngine; } }


void Application_Start(object sender, EventArgs e)
{
    // Code that runs on application startup
    try
    {
        //System.Diagnostics.Debugger.Break();

        string dictionaryPath = Server.MapPath("Bin") + "\\";
        Hunspell.NativeDllPath = dictionaryPath;

        spellEngine = new SpellEngine();
        LanguageConfig enConfig = new LanguageConfig();
        enConfig.LanguageCode = "en";
        enConfig.HunspellAffFile = dictionaryPath + "en_us.aff";
        enConfig.HunspellDictFile = dictionaryPath + "en_us.dic";
        enConfig.HunspellKey = "";

        //enConfig.HyphenDictFile = dictionaryPath + "hyph_en_us.dic";
        //enConfig.MyThesIdxFile = dictionaryPath + "th_en_us_new.idx";
        //enConfig.MyThesDatFile = dictionaryPath + "th_en_us_new.dat";
        spellEngine.AddLanguage(enConfig);

        LanguageConfig esConfig = new LanguageConfig();
        esConfig.LanguageCode = "es";
        esConfig.HunspellAffFile = dictionaryPath + "es_ES.aff";
        esConfig.HunspellDictFile = dictionaryPath + "es_ES.dic";
        esConfig.HunspellKey = "";
        spellEngine.AddLanguage(esConfig);

        LanguageConfig frConfig = new LanguageConfig();
        frConfig.LanguageCode = "fr";
        frConfig.HunspellAffFile = dictionaryPath + "fr-moderne.aff";
        frConfig.HunspellDictFile = dictionaryPath + "fr-moderne.dic";
        frConfig.HunspellKey = "";
        spellEngine.AddLanguage(frConfig);

        LanguageConfig deConfig = new LanguageConfig();
        deConfig.LanguageCode = "de";
        deConfig.HunspellAffFile = dictionaryPath + "de_DE_igerman98.aff";
        deConfig.HunspellDictFile = dictionaryPath + "de_DE_igerman98.dic";
        deConfig.HunspellKey = "";
        spellEngine.AddLanguage(deConfig);

    }
    catch (Exception)
    {
        if (spellEngine != null)
            spellEngine.Dispose();
    }

}


I start getting this error after some time (minutes) of testing the server:

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

It works for a while though.

Calling the spell checker object seems to be creating this:
SQL
if (Global.SpellEngine[Lang].Spell(WordToCheck) != true)//Incorrect spelling
{
...


My question is why does the SpellEngine object seem to get disposed of or is null after a while? I thought that Application_Start would create the object for the life of the web application?

Can anyone tell me what troubleshooting direction I should take? How can I determine what is happening to the SpellEngine object?

Also, how/where can I (or should I have to) recreate this object if it is null?

Another thing...sometimes it will work for a while then it will crash (Object null) but then after some time, it will start working again.

Thanks
Posted
Updated 23-Nov-10 5:52am
v2

Hi Will,

I would check for the following
- Global.SpellEngine != null
- Lang != null
- WordToCheck != null
- and if Lang is in "en es fr de"

Last point could be an issue if Lang is not in the set of configurations then I suppose SpellEngine[Land] will return
null (Badaboom).

Cheers

Manfred
 
Share this answer
 
v2
Comments
willworknow1 23-Nov-10 10:17am    
Thanks for the answer....but....I understand checking for null/values, Global.SpellEngine should never be null unless there's a failure. There IS a failure and that's the failure that I am seeing...Global.SpellEngine == null. My question is WHY is this null and how can I rectify it? I want Global.SpellEngine to exist for the life of the web app.

Thanks.
Manfred Rudolf Bihy 23-Nov-10 10:24am    
Sorry, I didn't read it that way. I use Global.asax too in our solutions and have not yet run into this problem (and I hope I never will ;) ).
I found the problem....it was some code I had in Global.Session_End. I am testing it right now and it *seems* to be working without issues.
 
Share this answer
 
Comments
Manfred Rudolf Bihy 24-Nov-10 5:50am    
Good for you, so i understand that it willworknow ;) .

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900