Jump to content

Question

Posted (edited)

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

10 answers to this question

Recommended Posts

  • 0
Posted (edited)
		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
  • 0
Posted (edited)

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
  • 0
Posted

Solved NPE, thank you.

 

Still getting ClassCastException at line 185:

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

When a summon kills it.

  • 0
Posted (edited)

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
  • 0
Posted (edited)

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
  • 0
Posted

 

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

  • 0
Posted

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 :)

  • 0
Posted (edited)

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
  • 0
Posted

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

memory rather than CPU ;)

Guest
This topic is now closed to further replies.


  • Posts

    • Hello,iv got fafurion running localy just for fun and seems that files dont have geodata couse i can hit through walls so are mobs.Anyone can tell me how to activate geodata or where to insert geodata files?
    • Good luck with your g/o 😃
    • You're talking complete nonsense. The source code of SphereAPI, which handles all requests, is closed and has never been published anywhere.     That return $this doesn't affect anything, PHP just ignores it. Why are you showing it?     Yes, of course I want that, and I’m asking you — go ahead and hack it, I’d even be thankful. The most you can probably do is launch a DDoS attack, but that’s not hacking. The IP you see is just a public one, used by default to handle requests. There are many other IPs involved, which aren’t disclosed for privacy reasons. So, when should I expect your hacker attack? If you fail, it means you talked big for nothing. I hope you're not all talk and can actually back up your words. UPD: To make your task easier, I have disabled limits on incorrect and failed requests, so your site/IP won't get blocked.
    • Lineage2Dex Interlude+ x50 Server launches TODAY!       The wait is over – the Lineage2Dex Interlude+ x50 Server launches TODAY! Grand Opening - April 26 at 19:00 (UTC+3) Over 10,000 registered accounts are ready to dive in! Beta with massive activity over, thx all who participate on it! It's time to begin your real Jorney! 👉 Read full roadmap 👉 Full server description 👉 Download and registration The server will be split into 3 stages, each featuring multiple content episodes. Stage 1: April 26 – May 18 Welcome to the Classic Interlude Phase. This stage is built with minimal custom content, focused on delivering that pure Interlude experience. It’s perfect for both new players unfamiliar with our project and veterans who enjoy a traditional start. Expect a smooth and nostalgic journey with familiar mechanics. Stage 2: May 19 – June 22 Time to expand the world! As most Interlude content gets completed, we gradually introduce new elements. Hellbound will open first, followed by weekly content updates. Midway through this stage, the Isle of Prayer will become available — a new zone featuring Dynasty Gear and fresh challenges. Stage 3: From June 23 Welcome to full content access! Some location become to High-level location, offering great reward! Once this phase concludes, a full merge with Union server will take place — and yes, all your characters and items will carry over safely. We wish everyone a smooth start, fair play, and tons of fun on the new x50 server! See you in-game!
    • Hello everyone! I'm offering professional L2J development services, specialized for aCis projects.   🔹 What I offer: Ready-to-implement existing systems (events, features, gameplay improvements). Custom development: I can create new systems based on your specific requests and ideas. Bug fixing, optimization, and enhancement of your server's code. Professional, clean, and organized Java coding. Support after delivery to ensure smooth integration.   🔹 Some examples of available systems: PvP and PvE Mini-Events (fully automated). Custom Raid Boss Events. Advanced Clan Utilities (regroup features, clan ranking, etc). Reward Systems (based on kills, damage, objectives, etc). Achievements Subscription system Arena Fights (1v1 tournament, scratch made). Advanced event engine (12 events, many features). multi-client aCis project, progressive gameplay C4 to C6   🔹 Why work with me? Years of experience working directly with aCis pack. Fast, reliable communication. I care about performance and long-term stability. I can adjust the systems to fit your server's style and balance.   🔹 Pricing: Flexible depending on the complexity (fixed price per project or per system). Discounts for multiple systems or long-term collaboration.   🔹 Contact: MaxCheaters PM Discord: @Luminous   Serious and passionate about your project? Let's make your server unique!
  • Topics

×
×
  • Create New...