Jump to content

my mod crashes my session returns to main menu "Exception thrown java.lang.NullPointerException:"


Harp797

Recommended Posts

Hello forum,

 

I wanted to try modding Project Zomboid so for the past day or two I have been making a barbwire baseball bat mod (that works like the nail bat) using posts from here at the forum and the PZ modding wiki. I've almost finished the core of the mod which is just the weapon, the repair scripts, model , & texture(s) and everything works fine until you strike with the weapon. There seems to be a 1/5(and if not the first, then at some point in the next four swings) chance that the game fades to black with error codes like (1,2,3,4,5, or 6) just a single number seemingly increasing by one with every crash. This has happened when striking a zombie or just swinging in the air, but only when my character swings the bat, not while idle with it. the screen fades to black and then loads the main menu.

 

Ive narrowed it down in the debug logs to this one line that seems to cause the crash,

 

ExceptionLogger.logException> Exception thrown java.lang.NullPointerException: Cannot invoke "zombie.inventory.types.HandWeapon.getSwingAnim()" because "<parameter1>.bareHands" is null at SwipeStatePlayer.CalcAttackVars line:651..

 

I've looked up "zombie.inventory.types.HandWeapon.getSwingAnim()" through this page https://projectzomboid.com/modding/zombie/inventory/types/HandWeapon.html

and am not entirely sure what is going wrong. I will attach the mod itself (forgive me if its formatted oddly or if i made some mistakes of somekind, and please let me know if i did) so you can see for yourself.

 

I have attached the FBX model file for the bat

the Fixing script

the weapon script itself

and mod.info

 

And if you want to recreate the error for yourself here is a dropbox link to all of the Mod

 

https://www.dropbox.com/scl/fo/ru8vdce4b3sqtkftwndmz/h?rlkey=0l3mq81dvlgx2oiy5h798gl8z&dl=0

the work folder in their is just to keep track of files I was using.

 

And help is greatly appreciated!

 

edit: rewording

 

WiredBaseballBat.fbx fixing.txt items_weapons.txt mod.info

Edited by Harp797
Link to comment
Share on other sites

  • Harp797 changed the title to my mod crashes my session returns to main menu "Exception thrown java.lang.NullPointerException:"

The following is the function CalcAttackVars referred to in the error, decompiled with decompiler.com.

 

   public void CalcAttackVars(IsoLivingCharacter var1, AttackVars var2) {
      HandWeapon var3 = (HandWeapon)Type.tryCastTo(var1.getPrimaryHandItem(), HandWeapon.class);
      if (var3 != null && var3.getOtherHandRequire() != null) {
         InventoryItem var4 = var1.getSecondaryHandItem();
         if (var4 == null || !var4.getType().equals(var3.getOtherHandRequire())) {
            var3 = null;
         }
      }

      if (!GameClient.bClient || var1.isLocal()) {
         boolean var13 = var1.isAttackAnim() || var1.getVariableBoolean("ShoveAnim") || var1.getVariableBoolean("StompAnim");
         var2.setWeapon(var3 == null ? var1.bareHands : var3);
         var2.targetOnGround.setMovingObject((IsoMovingObject)null);
         var2.bAimAtFloor = false;
         var2.bCloseKill = false;
         var2.bDoShove = var1.bDoShove;
         if (!var13) {
            var1.setVariable("ShoveAimX", 0.5F);
            var1.setVariable("ShoveAimY", 1.0F);
            if (var2.bDoShove && var1.getVariableBoolean("isMoving")) {
               var1.setVariable("ShoveAim", true);
            } else {
               var1.setVariable("ShoveAim", false);
            }
         }

         var2.useChargeDelta = var1.useChargeDelta;
         var2.recoilDelay = 0;
         if (var2.getWeapon(var1) == var1.bareHands || var2.bDoShove || var1.isForceShove()) {
            var2.bDoShove = true;
            var2.bAimAtFloor = false;
            var2.setWeapon(var1.bareHands);
         }

         this.calcValidTargets(var1, var2.getWeapon(var1), true, var2.targetsProne, var2.targetsStanding);
         HitInfo var5 = var2.targetsStanding.isEmpty() ? null : (HitInfo)var2.targetsStanding.get(0);
         HitInfo var6 = var2.targetsProne.isEmpty() ? null : (HitInfo)var2.targetsProne.get(0);
         if (this.isProneTargetBetter(var1, var5, var6)) {
            var5 = null;
         }

         if (!var13) {
            var1.setAimAtFloor(false);
         }

         float var7 = Float.MAX_VALUE;
         if (var5 != null) {
            if (!var13) {
               var1.setAimAtFloor(false);
            }

            var2.bAimAtFloor = false;
            var2.targetOnGround.setMovingObject((IsoMovingObject)null);
            var7 = var5.distSq;
         } else if (var6 != null && (Core.OptionAutoProneAtk || var1.bDoShove)) {
            if (!var13) {
               var1.setAimAtFloor(true);
            }

            var2.bAimAtFloor = true;
            var2.targetOnGround.setMovingObject(var6.getObject());
         }

         if (!(var7 >= var2.getWeapon(var1).getMinRange() * var2.getWeapon(var1).getMinRange()) && (var5 == null || !this.isWindowBetween(var1, var5.getObject()))) {
            if (var1.getStats().NumChasingZombies <= 1 && WeaponType.getWeaponType(var1) == WeaponType.knife) {
               var2.bCloseKill = true;
               return;
            }

            var2.bDoShove = true;
            IsoPlayer var8 = (IsoPlayer)Type.tryCastTo(var1, IsoPlayer.class);
            if (var8 != null && !var8.isAuthorizeShoveStomp()) {
               var2.bDoShove = false;
            }

            var2.bAimAtFloor = false;
            if (var1.bareHands.getSwingAnim() != null) {
               var2.useChargeDelta = 3.0F;
            }
         }

         int var14 = Core.getInstance().getKey("ManualFloorAtk");
         int var9 = Core.getInstance().getKey("Sprint");
         boolean var10 = var1.getVariableBoolean("StartedAttackWhileSprinting");
         if (Keyboard.isKeyDown(var14) && (var14 != var9 || !var10)) {
            var2.bAimAtFloor = true;
            var2.bDoShove = false;
            var1.setDoShove(false);
         }

         if (var2.getWeapon(var1).isRanged()) {
            int var11 = var2.getWeapon(var1).getRecoilDelay();
            Float var12 = (float)var11 * (1.0F - (float)var1.getPerkLevel(Perks.Aiming) / 30.0F);
            var2.recoilDelay = var12.intValue();
            var1.setVariable("singleShootSpeed", (0.8F + (float)var1.getPerkLevel(Perks.Aiming) / 10.0F) * GameTime.getAnimSpeedFix());
         }

      }
   }

 

It looks like the issue is that var1.bareHands is null, so calling null.getSwingAnim() is causing the crash.

 

I know the game has a 'bare hands' weapon, I assume this is the 'weapon' you have equipped when you have no weapon equipped - not sure how this could ever be null but that seems to be what is happening.

 

 

Link to comment
Share on other sites

14 minutes ago, Hugo Qwerty said:

The following is the function CalcAttackVars referred to in the error, decompiled with decompiler.com.

 

   public void CalcAttackVars(IsoLivingCharacter var1, AttackVars var2) {
      HandWeapon var3 = (HandWeapon)Type.tryCastTo(var1.getPrimaryHandItem(), HandWeapon.class);
      if (var3 != null && var3.getOtherHandRequire() != null) {
         InventoryItem var4 = var1.getSecondaryHandItem();
         if (var4 == null || !var4.getType().equals(var3.getOtherHandRequire())) {
            var3 = null;
         }
      }

      if (!GameClient.bClient || var1.isLocal()) {
         boolean var13 = var1.isAttackAnim() || var1.getVariableBoolean("ShoveAnim") || var1.getVariableBoolean("StompAnim");
         var2.setWeapon(var3 == null ? var1.bareHands : var3);
         var2.targetOnGround.setMovingObject((IsoMovingObject)null);
         var2.bAimAtFloor = false;
         var2.bCloseKill = false;
         var2.bDoShove = var1.bDoShove;
         if (!var13) {
            var1.setVariable("ShoveAimX", 0.5F);
            var1.setVariable("ShoveAimY", 1.0F);
            if (var2.bDoShove && var1.getVariableBoolean("isMoving")) {
               var1.setVariable("ShoveAim", true);
            } else {
               var1.setVariable("ShoveAim", false);
            }
         }

         var2.useChargeDelta = var1.useChargeDelta;
         var2.recoilDelay = 0;
         if (var2.getWeapon(var1) == var1.bareHands || var2.bDoShove || var1.isForceShove()) {
            var2.bDoShove = true;
            var2.bAimAtFloor = false;
            var2.setWeapon(var1.bareHands);
         }

         this.calcValidTargets(var1, var2.getWeapon(var1), true, var2.targetsProne, var2.targetsStanding);
         HitInfo var5 = var2.targetsStanding.isEmpty() ? null : (HitInfo)var2.targetsStanding.get(0);
         HitInfo var6 = var2.targetsProne.isEmpty() ? null : (HitInfo)var2.targetsProne.get(0);
         if (this.isProneTargetBetter(var1, var5, var6)) {
            var5 = null;
         }

         if (!var13) {
            var1.setAimAtFloor(false);
         }

         float var7 = Float.MAX_VALUE;
         if (var5 != null) {
            if (!var13) {
               var1.setAimAtFloor(false);
            }

            var2.bAimAtFloor = false;
            var2.targetOnGround.setMovingObject((IsoMovingObject)null);
            var7 = var5.distSq;
         } else if (var6 != null && (Core.OptionAutoProneAtk || var1.bDoShove)) {
            if (!var13) {
               var1.setAimAtFloor(true);
            }

            var2.bAimAtFloor = true;
            var2.targetOnGround.setMovingObject(var6.getObject());
         }

         if (!(var7 >= var2.getWeapon(var1).getMinRange() * var2.getWeapon(var1).getMinRange()) && (var5 == null || !this.isWindowBetween(var1, var5.getObject()))) {
            if (var1.getStats().NumChasingZombies <= 1 && WeaponType.getWeaponType(var1) == WeaponType.knife) {
               var2.bCloseKill = true;
               return;
            }

            var2.bDoShove = true;
            IsoPlayer var8 = (IsoPlayer)Type.tryCastTo(var1, IsoPlayer.class);
            if (var8 != null && !var8.isAuthorizeShoveStomp()) {
               var2.bDoShove = false;
            }

            var2.bAimAtFloor = false;
            if (var1.bareHands.getSwingAnim() != null) {
               var2.useChargeDelta = 3.0F;
            }
         }

         int var14 = Core.getInstance().getKey("ManualFloorAtk");
         int var9 = Core.getInstance().getKey("Sprint");
         boolean var10 = var1.getVariableBoolean("StartedAttackWhileSprinting");
         if (Keyboard.isKeyDown(var14) && (var14 != var9 || !var10)) {
            var2.bAimAtFloor = true;
            var2.bDoShove = false;
            var1.setDoShove(false);
         }

         if (var2.getWeapon(var1).isRanged()) {
            int var11 = var2.getWeapon(var1).getRecoilDelay();
            Float var12 = (float)var11 * (1.0F - (float)var1.getPerkLevel(Perks.Aiming) / 30.0F);
            var2.recoilDelay = var12.intValue();
            var1.setVariable("singleShootSpeed", (0.8F + (float)var1.getPerkLevel(Perks.Aiming) / 10.0F) * GameTime.getAnimSpeedFix());
         }

      }
   }

 

It looks like the issue is that var1.bareHands is null, so calling null.getSwingAnim() is causing the crash.

 

I know the game has a 'bare hands' weapon, I assume this is the 'weapon' you have equipped when you have no weapon equipped - not sure how this could ever be null but that seems to be what is happening.

 

 

Thank you so much for the reply, ill try using this to see if i can figure out the issue, I wonder if there is some cancellation of somekind happening as the code for my bat is just slightly tweaked code of the nail bat (that and the name of the file in debug overlaps the ingame one). Once again i appreciate the reply and will update this thread for people in the future if i find a solution.

Link to comment
Share on other sites

I BELIEVE I JUST FIXED IT LET GO,

 

So the script for the weapon I wrote had the name "item_weapons.txt"

 

and I noticed last night while skimming through logs trying to locate the issue that my mod would overwrite the vanilla game "item_weapons.txt" 

 

-so for future readers, rename your scripts so they don't match & override vanilla game scripts-

 

[24-11-23 20:45:33.459] LOG  : Mod         , 1700887533459> mod "Harp797sBarbedWireBat" overrides media/scripts/fixing.txt.
[24-11-23 20:45:33.460] LOG  : Mod         , 1700887533460> mod "Harp797sBarbedWireBat" overrides media/scripts/items_weapons.txt.
 

I cannot find this in newer logs and have not encountered a crash so far.

What I believe was happening was that whenever I go to shove or when a zombie tries doing a specific attack the game calls for bare hands for player or zombie but it returns null because my mod just overrode the in game file and only included information about my bat. This is probably why i was able to swing it withought issue, not realizing the zombie lunging or me shoving was causing the issue.

 

There were many warnings for every ingame weapon which after changing the mod script name around, went away.

 

Im super happy i got this working, this is one of the first actual mods i've created. For now im going to iron it out more and get some other features working.

 

Thanks again @Hugo Qwerty for the help, your explanation helped me understand what the problem was and i greatly appreciate it!

image.png.4f08c14232245e064072361953c1dc53.png

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...