Problem with HashMap in sigleton class

Have you created a useful tool? or Do you want to get help building one? This is the right place!
Forum rules
READ NOW: L2j Forums Rules of Conduct
Post Reply
ekozan
Posts: 6
Joined: Fri Nov 11, 2011 10:26 am

Problem with HashMap in sigleton class

Post 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
User avatar
UnAfraid
L2j Veteran
L2j Veteran
Posts: 4199
Joined: Mon Jul 23, 2007 4:25 pm
Location: Bulgaria
Contact:

Re: Problem with HashMap in sigleton class

Post 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.
Image
ekozan
Posts: 6
Joined: Fri Nov 11, 2011 10:26 am

Re: Problem with HashMap in sigleton class

Post by ekozan »

Thx I'll try your solution
User avatar
jurchiks
Posts: 6769
Joined: Sat Sep 19, 2009 4:16 pm
Location: Eastern Europe

Re: Problem with HashMap in sigleton class

Post by jurchiks »

also, constructor should be private, and StringBuilder should be used in openMenu().
If you have problems, FIRST TRY SOLVING THEM YOURSELF, and if you get errors, TRY TO ANALYZE THEM, and ONLY if you can't help it, THEN ask here.
Otherwise you will never learn anything if all you do is copy-paste!
Discussion breeds innovation.
ekozan
Posts: 6
Joined: Fri Nov 11, 2011 10:26 am

Re: Problem with HashMap in sigleton class

Post 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 .
User avatar
jurchiks
Posts: 6769
Joined: Sat Sep 19, 2009 4:16 pm
Location: Eastern Europe

Re: Problem with HashMap in sigleton class

Post 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.
If you have problems, FIRST TRY SOLVING THEM YOURSELF, and if you get errors, TRY TO ANALYZE THEM, and ONLY if you can't help it, THEN ask here.
Otherwise you will never learn anything if all you do is copy-paste!
Discussion breeds innovation.
User avatar
Zoey76
L2j Inner Circle
L2j Inner Circle
Posts: 7005
Joined: Tue Aug 11, 2009 3:36 am

Re: Problem with HashMap in sigleton class

Post by Zoey76 »

You can always use static nested classes, that should be a workaround to avoid core changes.
Powered by Eclipse 4.30 🌌 | Eclipse Temurin 21 ☕ | MariaDB 11.3.2 🗃️ | L2J Server 2.6.3.0 - High Five 🚀

🔗 Join our Discord! 🎮💬
ekozan
Posts: 6
Joined: Fri Nov 11, 2011 10:26 am

Re: Problem with HashMap in sigleton class

Post by ekozan »

I have edit the core and it's work

Thx for your help all :mrgreen:
Post Reply