Page 1 of 1

Problem with HashMap in sigleton class

Posted: Wed Jul 03, 2013 10:47 am
by ekozan
Hello all

I have an little crazy problem with HashMap

when i register an new events like my custom TownInvasion
it's show OK registered in DATATABLESOFEVENT and count of HashMap is 1

Code: Select all

 EventManager.getInstance().registerPrimalEvent(new TownInvasionManager()); 


but when i call openMenu DATATABLESOFEVENT is null ...

i dont understund why

Code: Select all

 public class EventManager extends L2Script{     public static final int NPC_ID_PRIMALHELPER = 1000008;    private Map<String, IPrimalEventManager> DATATABLESOFEVENT;     public EventManager(){        super(-1, "EventManager", "custom EventManager");        DATATABLESOFEVENT = new HashMap<String, IPrimalEventManager>();        _log.info("Event constructor");    }     public void registerPrimalEvent(IPrimalEventManager pem){        _log.info("new event loaded -> "+ pem.getEventName().toLowerCase());        DATATABLESOFEVENT.put(pem.getEventName().toLowerCase(),pem);    }     public int size()    {        return DATATABLESOFEVENT.size();    }     public IPrimalEventManager getPrimalEvent(String name){        return DATATABLESOFEVENT.get(name.toLowerCase());    }      public void openMenu(String command, L2PcInstance activeChar){        NpcHtmlMessage html = new NpcHtmlMessage(1000008);        String htmlrender = "";        htmlrender += "<html><body>";        htmlrender += "Primal EventManager:<br>";        htmlrender += "<br>";        _log.info("gen menu -> events : " + size());        for( Map.Entry<String, IPrimalEventManager> primalEventd : DATATABLES.entrySet()){            _log.info(" Event key -> " + primalEventd.getKey());            htmlrender += primalEventd.getValue().getEventName();        }        htmlrender += "</center><br>";        htmlrender += "</body></html>";        html.setHtml(htmlrender);        activeChar.sendPacket(html);    }     public static EventManager getInstance()    {            return SingletonHolder._instance;    }     private static class SingletonHolder    {        protected static final EventManager _instance = new EventManager();    }     public static void main(String[] args)    {        AdminCommandHandler.getInstance().registerHandler(new EventManagerCmd());        EventManager.getInstance();    }}  
if this problem is bec i'm noob java coder you can hit me :P

Re: Problem with HashMap in sigleton class

Posted: Wed Jul 03, 2013 11:01 am
by UnAfraid
The call to the manager must be made from the same package otherwise it will create whole new class and instance for that singleton.
That's happening because your class is located in DataPack which means whenever ecj compiles (Eclipse java compiler) it uses separate class loader per package and your class is loaded in another class loader which is not accessible from other packages then the one created it.
So u have only two options.
Either move the class in core or simply put all classes that calling it on the same package.

Re: Problem with HashMap in sigleton class

Posted: Wed Jul 03, 2013 11:15 am
by ekozan
Thx I'll try your solution

Re: Problem with HashMap in sigleton class

Posted: Wed Jul 03, 2013 11:21 am
by jurchiks
also, constructor should be private, and StringBuilder should be used in openMenu().

Re: Problem with HashMap in sigleton class

Posted: Wed Jul 03, 2013 1:55 pm
by ekozan
I have set same package to all script ( package Primal.Event; )
but the problem persist

so i have put "_log.info("Init Constructor") in the script and each .getinstance() run the constructor

i'm newbie in java but i think it's not normal

any idee :?:


P.S : thx for StringBuilder and private constructor .

Re: Problem with HashMap in sigleton class

Posted: Wed Jul 03, 2013 2:24 pm
by jurchiks
It's not normal, but unfortunately we can't do anything about it at the moment, that's why UnAfraid suggested you put that class in core; that would allow you to avoid that problem for the time being.

Re: Problem with HashMap in sigleton class

Posted: Wed Jul 03, 2013 4:06 pm
by Zoey76
You can always use static nested classes, that should be a workaround to avoid core changes.

Re: Problem with HashMap in sigleton class

Posted: Wed Jul 03, 2013 4:31 pm
by ekozan
I have edit the core and it's work

Thx for your help all :mrgreen: