Jump to content
  • 0

Code Npe


Gries

Question

I have this code in doDie method inside L2MonsterInstance.java file:

		int npcId = getTemplate().getId();
		switch (npcId)
		{
			case 150:
				if (((L2PcInstance) killer).getClan() == null)
				{
					return false;
				}
				if (killer.getActingPlayer() != null)
				{
					((L2PcInstance) killer).getClan().addReputationScore(10, true); // Change 1 with the reputation points you want to add upon kill monster
					((L2PcInstance) killer).sendMessage("Your Clan Reputation is increased by 10.");
				}
				final L2Summon summon = killer.getSummon();
				if (summon.getOwner() != null)
				{
					return false;
				}
				
		}

However, when i am killing monster with ID 150 i am getting a NullPointerException. Instead if the monster is killed by a summon or a pet i am getting a  ClassCasterException.
How to solve it?

Edited by Gries
Link to comment
Share on other sites

10 answers to this question

Recommended Posts

  • 0
		int npcId = getTemplate().getId();
		switch (npcId)
		{
			case 150:
				final L2PcInstance player = killer.getActingPlayer();
				if (player != null)
				{
					final L2Clan clan = player.getClan();
					
					if (clan != null)
					{
						clan.addReputationScore(10, true); // Change 10 with the reputation points you want to add upon kill monster
					}
					final L2Summon summon = killer.getSummon();
					if (summon != null)
					{
						return false;
					}
					
					player.sendMessage("Your Clan Reputation is increased by 10.");
				}

Changed code like that, now it is working without any NPE, thank you all  ^-^

Edited by Gries
Link to comment
Share on other sites

  • 0

The error must be here:

 

                final L2Summon summon = killer.getSummon();
                if (summon.getOwner() != null)
                {
                    return false;
                }

 

You need to check first:

if (summon != null)

{

                final L2Summon summon = killer.getSummon();
                if (summon.getOwner() != null)
                {
                    return false;
                }

}

 

 

But this doesn't make sense too:

 

                if (((L2PcInstance) killer).getClan() == null)
                {
                    return false;
                }
                if (killer.getActingPlayer() != null)
                {

 

You firstly check if killer's clan == null and then if killer is null (?). Unless getActingPlayer() returns smth else.

Edited by An4rchy
Link to comment
Share on other sites

  • 0

if (summon != null)

{

                final L2Summon summon = killer.getSummon();

                if (summon.getOwner() != null)

                {

                    return false;

                }

}

 

It's only me or it's kinda meh ? :P

 

I'd do like that

final L2Summon summon = killer.getPet();
if (summon != null)
   return false;
Edited by SweeTs
Link to comment
Share on other sites

  • 0

look... the way it is coded is horrible... if you call something you're supposed to do it like it:

final L2PcInstance player = killer.getActingPlayer();
if (player != null) {
    final L2Clan clan = player.getClan();

    if (clan != null) {
          clan.doAnythingYouWish();
    }

    player.doAnythingYouWish();
}

and definetly NOT:

if (killer.getActingPlayer() != null) {
     if (((L2PcInstance) killer).getClan() != null) {
         ((L2PcInstance) killer).getClan().doAnythingYouWish();
    }

    ((L2PcInstance) killer).doAnythingYouWish();
}
Edited by lord_rex
Link to comment
Share on other sites

  • 0

 

look... the way it is coded is horrible... if you call something you're supposed to do it like it:

final L2PcInstance player = killer.getActingPlayer();
if (player != null) {
    final L2Clan clan = player.getClan();

    if (clan != null) {
          clan.doAnythingYouWish();
    }

    player.doAnythingYouWish();
}

and definetly NOT:

if (killer.getActingPlayer() != null) {
     if (((L2PcInstance) killer).getClan() != null) {
         ((L2PcInstance) killer).getClan().doAnythingYouWish();
    }

    ((L2PcInstance) killer).doAnythingYouWish();
}

 

the best way for me is:

 

if (killer.getActingPlayer != null)

{     if (killer.getActingPlayer().getClan() != null)

      {         killer.getActingPlayer().getClan().doSmth();

      }

      killer.doSmth()

}

 

getActingPlayer turns L2Object into L2PcInstance without cast if the L2Object is instanceof L2PcInstance otherwise null.

 

Thus I would use your method if I wanted to apply more methods than one

Link to comment
Share on other sites

  • 0

the best way for me is:

 

if (killer.getActingPlayer != null)

{     if (killer.getActingPlayer().getClan() != null)

      {         killer.getActingPlayer().getClan().doSmth();

      }

      killer.doSmth()

}

 

getActingPlayer turns L2Object into L2PcInstance without cast if the L2Object is instanceof L2PcInstance otherwise null.

 

Thus I would use your method if I wanted to apply more methods than one

no... with your code you call killer.getActingPlayer() 3 times, my way calls it only once and avoids the casting also, and as you see, it is helped on Gries :)

Link to comment
Share on other sites

  • 0

no... with your code you call killer.getActingPlayer() 3 times, my way calls it only once and avoids the casting also, and as you see, it is helped on Gries :)

 

It all depends on coding style, I just don't like to occupy memory when not needed, old C traits ;p

Edited by xdem
Link to comment
Share on other sites

Guest
This topic is now closed to further replies.


  • Posts

    • to my store : https://topestore.mysellix.io/fr/ 2015-2022 Aged Discord Account 2015 Discord Account : 50.99 $ 2016 Discord Account : 10$ 2017 Discord Account :3.99 $ 2018 Discord Account : 3.50$ 2019 Discord Account : 2.70 $ 2020 Discord Account :1.50$ 2021 Discord Account :0.99$ 2022 Discord Account :0.70$ Warranty :Lifetime Payment Methods : Crypto/ PayPal Contact Me On Discord Or Telegram Discord : @ultrasstore11 Telegram : https://t.me/ultrastore11 Whatsapp ; +212614849119
    • This is my first and last topic created on a Lineage Forum, because if there's one thing that causes headaches for a lot of people, it's the critical error, that's why I decided to share it...   I'm creating a server again and before starting configurations I started collecting dozens of customs, without realizing my system folder had 950MB of files, so I started to enter the game, every 1 to 2 hours my client was reaching the maximum limit of virtual memory of the game due to the absurd amount of customs inside the system folder, reaching the maximum limit of 2047MB virtual ram, automatically we get critical, regardless of the error that appears on your screen, "THE WORDS ARE IRRELEVANT, unless it is a critical error before the ram memory limit reaches its limit", if you are seeing "2047 MB", it means that the game's virtual memory has reached its limit, and this memory is not configurable due to the game being created on 32-bit architecture, so I significantly reduced the amount of customs within the system from 950MB to 625MB, so my client started to reach the maximum memory limit every 10 to 12h, a huge progress, but my goal was at least 24h before reaching the virtual memory limit (playing frantically), so I reduced the system's custom files from 625MB to 267MB, the result was a first virtual memory limit critical error appearing after 41h.   My client is H5, the only types of customs I added to the game were focused only on equipment, one of the main causes were these skins and cloaks, they consume a huge amount of MB's within the "system", the problem is not adding customs to the client, the problem is adapting a custom and adding it inside the system folder, the system folder was not created with the intention of storing a huge amount of files.   Adding customs to the system folder means significantly reducing the time your client can remain open before reaching the virtual ram limit and make no mistake, there are hundreds of different critical errors with a small summary of the cause of the error, but if the critical table shows 2047MB of RAM, the problem is only 1. Example: I can see 3, 5, 9 critical error with different messages, but if this table is showing 2047MB ram, the reason was the virtual memory limit, an important detail is that this 2047MB ram is not a "fixed message or information from your computer", this 2047MB only appear when the cause is the virtual ram limit, it means that when you get a critical error with random numbers of ram memory appearing, such as 358MB ram, 715MB ram, it means that the cause of this error has no connection with ram virtual memory.
    • DISCORD : utchiha_market telegram : https://t.me/utchiha_market SELLIX STORE : https://utchihamkt.mysellix.io/ Join our server for more products  https://discord.gg/hoodservices https://campsite.bio/utchihaamkt
  • Topics

×
×
  • Create New...