DictionaryForMids Forum

DictionaryForMIDs for Mobiles (Java ME; most devices from Nokia, Samsungs, RIM (Blackberry), LG, SonyEricsson, Motorola and plenty more) => Problems => Topic started by: njp on 14. April 2008, 18:02:19

Title: Interfacing to DictionaryForMids
Post by: njp on 14. April 2008, 18:02:19
Hello there,

I've downloaded the DictionaryForMIDs_engdeu_DTS.jar from the dicts.info site and added it to my project in netbeans.

I have a basic Form

public class LwlPlay extends Form implements CommandListener, ItemStateListener, TranslationExecutionCallback{
}


with this line in the constructor...

TranslationExecution.setTranslationExecutionCallback(this);

with the following implementations for the TranslationExecutionCallback interface...

    public void newTranslationResult(TranslationResult resultOfTranslation)
    {
        if (resultOfTranslation.translationFound)
        {
            Enumeration temp = resultOfTranslation.translations.elements();
           
            while(temp.hasMoreElements())
            {
                SingleTranslation t = (SingleTranslation)temp.nextElement();
                append(new StringItem(t.fromText.toString(), t.toText.toString()));
            }
        }
    }

    public void deletePreviousTranslationResult()
    {

    }

during an event when a button is clicked I execute this...

                try
                {
                    TranslationExecution.executeTranslation("hello", false);
                }
                catch(Exception e)
                {
                    e.printStackTrace();
                }

if executeInBackground is false, I get the following exception...

java.lang.NullPointerException
        at de.kugihan.dictionaryformids.general.Util.memCheck(+3)
        at de.kugihan.dictionaryformids.translation.Translation.getTranslationResult(+20)
        at de.kugihan.dictionaryformids.translation.TranslationThread.doTranslation(+220)
        at de.kugihan.dictionaryformids.translation.TranslationThread.runInForeground(+4)
        at de.kugihan.dictionaryformids.translation.TranslationExecution.executeTranslation(+46)
        at LwlPlay.commandAction(LwlPlay.java:92)
        at javax.microedition.lcdui.Display$DisplayAccessor.commandAction(Display.java:2093)
        at javax.microedition.lcdui.Display$DisplayManagerImpl.commandAction(Display.java:2929)
        at com.sun.midp.lcdui.DefaultEventHandler.commandEvent(DefaultEventHandler.java:297)
        at com.sun.midp.lcdui.AutomatedEventHandler.commandEvent(AutomatedEventHandler.java:667)
        at com.sun.midp.lcdui.DefaultEventHandler$QueuedEventHandler.handleVmEvent(DefaultEventHandler.java:711)
        at com.sun.midp.lcdui.DefaultEventHandler$QueuedEventHandler.run(DefaultEventHandler.java:608)

if its set to true, I get an unhandeled java.lang.NullPointerException

If anyone can help me out I'd greatly appriciate it!

Cheers

John

Title: Re: Interfacing to DictionaryForMids
Post by: Gert on 14. April 2008, 20:11:57
Util.memCheck is a static debugging function that uses the static Util.utilObj. You need first to create an object of type UtilMid and then call Util.setUtil() with that object.

Such as in

UtilMid utilObj = new UtilMid();
utilObj.setLogForm(yourFormObject);
Util.setUtil(utilObj);


That should help !

Gert

P.S.: Well, I have to admit that Util.memCheck probably should not be a static method. I should change this some time in the future (hope I do not forget ;) )
Title: Re: Interfacing to DictionaryForMids
Post by: njp on 15. April 2008, 11:04:12
Thanks for the response Gert.

However, I'm only one step closer to the solution.


Having added the additional code you suggested I now have this error when the getTranslationResult function is called from within the Translation class.



java.lang.NullPointerException
        at de.kugihan.dictionaryformids.translation.Translation.getTranslationResult(+26)
        at de.kugihan.dictionaryformids.translation.TranslationThread.doTranslation(+220)
        at de.kugihan.dictionaryformids.translation.TranslationThread.runInForeground(+4)
        at de.kugihan.dictionaryformids.translation.TranslationExecution.executeTranslation(+46)
        at LwlPlay.commandAction(LwlPlay.java:99)



I'm confident the exception is being reported from within this block of code within the Translation.java file in the getTranslationResults function.



try {
Normation normationObj =      DictionaryDataFile.supportedLanguages[DictionarySettings.getInputLanguage()].normationObj;
// determine search words from the to be translated word
Vector searchWords = normationObj.searchWord(toBeTranslatedWord);
// get translation for each searchWord
for (int wordCount = 0; wordCount < searchWords.size(); ++wordCount) {
SearchedWord searchWord = (SearchedWord) searchWords.elementAt(wordCount);
String nonNormatedWord = searchWord.word;
String toBeTranslatedWordNormated = normationObj.normateWord(new StringBuffer(nonNormatedWord), true).toString();
if (toBeTranslatedWordNormated.length() > 0) {
searchTranslationForNormatedWord(toBeTranslatedWordNormated);
}
}
}
catch (Throwable t) {
Util.getUtil().log(t);
}




I'm affraid I dont know enough about the architechture to solve this problem easily though.

Again, any help appreciated  :)
Title: Re: Interfacing to DictionaryForMids
Post by: Gert on 16. April 2008, 04:02:43
In general: you need to make sure that all initializations are done before you use the Translation class. Please have a look at the class DictionaryForMIDs to see about the initialisation.

Specifically, you need to call

DictionaryDataFile.initValues(false);

Ok, I realize that from your postings I get very useful hints for improving the documentation !! I plan to enhance this 'as soon as I find time'.

Please do continue asking until everything runs fine for you !!

Gert

P.S.: from the byte position of the Exception, e.g. 26 in getTranslationResult(+26), with the help of a Java class decompiler you can exactly locate where the Exception was thrown in the source code. This is very helpful for debugging.
Title: Re: Interfacing to DictionaryForMids
Post by: njp on 16. April 2008, 15:43:09

QuotePlease do continue asking until everything runs fine for you !!

OK then, in the interest of improving your documentation.  Having added



DictionaryDataFile.initValues(false);



I now get the following error.  The line in the code represents the call to DictionaryDataFile.initValues(false);



java.lang.NullPointerException
        at de.kugihan.dictionaryformids.general.UtilMid.openProperties(+38)
        at de.kugihan.dictionaryformids.dataaccess.DictionaryDataFile.initValues(+8)
        at LwlPlay.<init>(LwlPlay.java:58)

Title: Re: Interfacing to DictionaryForMids
Post by: Gert on 16. April 2008, 20:01:59
You need to do all the initializations from the constructor of class DictionaryForMIDs.

also:

FileAccessHandler.setDictionaryDataFileISAccess(dfmInputStreamObj);

Probably the files will be read from a resource that is packaged in the JAR file, right ? Then you need to set the file access to an object of class ResourceDfMInputStreamAccess.

Gert
Title: Re: Interfacing to DictionaryForMids
Post by: Gert on 17. April 2008, 18:58:05
I expect everything works perfectly now ;) ?

Gert