我如何修复这个NullExceptionPointer错误?

huangapple go评论92阅读模式
英文:

how can i fix this NullExceptionPointer error?

问题

这是来自《冒险岛》游戏的现成代码,我运行一个函数,对我有效,但对其他人无效。他们没有计时器。似乎chr.getFitness()返回null。我在Java方面不够强,如果有人能帮忙就好了。

MapleFitness脚本:

/*
    这个文件是OdinMS《冒险岛》服务器的一部分
    版权所有(C)2008 Patrick Huy <patrick.huy@frz.cc>
                   Matthias Butz <matze@odinms.de>
                   Jan Christian Meyer <vimes@odinms.de>

    本程序是自由软件:您可以根据GNU Affero通用公共许可证的第3版的条款
    由自由软件基金会发布的版本修改和/或重新发布
    本程序。您不得使用、修改或分发本程序
    GNU Affero通用公共许可证的任何其他版本。

    本程序是按"原样"提供的,不提供任何明示或暗示的保证,
    包括但不限于适销性或特定用途的暗示保证。参见
    GNU Affero通用公共许可证以获取更多详细信息。

    您应该已收到GNU Affero通用公共许可证的一份副本
    随附此程序。如果没有,请参阅<http://www.gnu.org/licenses/>。
*/

package server.events.gm;

import client.MapleCharacter;
import java.util.concurrent.ScheduledFuture;
import server.TimerManager;
import tools.MaplePacketCreator;

/**
 *
 * 作者:kevintjuh93
 */
public class MapleFitness {
       private MapleCharacter chr;
       private long time = 0;
       private long timeStarted = 0;
       private ScheduledFuture<?> schedule = null;
       private ScheduledFuture<?> schedulemsg = null;
       
       public MapleFitness(final MapleCharacter chr) {
           this.chr = chr;
           this.schedule = TimerManager.getInstance().schedule(new Runnable() {
            @Override
            public void run() {
            if (chr.getMapId() >= 109040000 && chr.getMapId() <= 109040004)
                chr.changeMap(chr.getMap().getReturnMap());
            }
           }, 900000);
       }
       
       public void startFitness() {
           chr.getMap().startEvent();
           chr.getClient().announce(MaplePacketCreator.getClock(900));
           this.timeStarted = System.currentTimeMillis();
           this.time = 900000;  
           checkAndMessage();         

           chr.getMap().getPortal("join00").setPortalStatus(true);
           chr.getClient().announce(MaplePacketCreator.serverNotice(0, "The portal has now opened. Press the up arrow key at the portal to enter."));
       }
       
        public boolean isTimerStarted() {
            return time > 0 && timeStarted > 0;
        }    
    
       public long getTime() {
           return time;
       }

       public void resetTimes() {
           this.time = 0;
           this.timeStarted = 0;
           schedule.cancel(false);
           schedulemsg.cancel(false);
       }

       public long getTimeLeft() {
           return time - (System.currentTimeMillis() - timeStarted);
       }

       public void checkAndMessage() {
           this.schedulemsg = TimerManager.getInstance().register(new Runnable() {
            @Override
            public void run() {
                if (chr.getFitness() == null) {
                    resetTimes();
                }
            if (chr.getMap().getId() >= 109040000 && chr.getMap().getId() <= 109040004) {
             if (getTimeLeft() > 9000 && getTimeLeft() < 11000) {
                 chr.getClient().announce(MaplePacketCreator.serverNotice(0, "You have 10 sec left. Those of you unable to beat the game, we hope you beat it next time! Great job everyone!! See you later~"));
             } else if (getTimeLeft() > 99000 && getTimeLeft() < 101000) {
                 chr.getClient().announce(MaplePacketCreator.serverNotice(0, "Alright, you don't have much time remaining. Please hurry up a little!"));
             } else if (getTimeLeft() > 239000 && getTimeLeft() < 241000) {
                 chr.getClient().announce(MaplePacketCreator.serverNotice(0, "The 4th stage is the last one for [The Maple Physical Fitness Test]. Please don't give up at the last minute and try your best. The reward is waiting for you at the very top!"));
             } else if (getTimeLeft() > 299000 && getTimeLeft() < 301000) {
                 chr.getClient().announce(MaplePacketCreator.serverNotice(0, "The 3rd stage offers traps where you may see them, but you won't be able to step on them. Please be careful of them as you make your way up."));
             } else if (getTimeLeft() > 359000 && getTimeLeft() < 361000) {
                 chr.getClient().announce(MaplePacketCreator.serverNotice(0, "For those who have heavy lags, please make sure to move slowly to avoid falling all the way down because of lags."));
             } else if (getTimeLeft() > 499000 && getTimeLeft() < 501000) {
                 chr.getClient().announce(MaplePacketCreator.serverNotice(0, "Please remember that if you die during the event, you'll be eliminated from the game. If you're running out of HP, either take a potion or recover HP first before moving on."));
             } else if (getTimeLeft() > 599000 && getTimeLeft() < 601000) {
                 chr.getClient().announce(MaplePacketCreator.serverNotice(0, "The most important thing you'll need to know to avoid the bananas thrown by the monkeys is *Timing* Timing is everything in this!"));
             } else if (getTimeLeft() > 659000 && getTimeLeft() < 661000) {
                 chr.getClient().announce(MaplePacketCreator.serverNotice(0, "The 2nd stage offers monkeys throwing bananas. Please make sure to avoid them by moving along at just the right timing."));
             } else if (getTimeLeft() > 699000 && getTimeLeft() < 701000) {
                 chr.getClient().announce(MaplePacketCreator.serverNotice(0, "Please remember that if you die during the event, you'll be eliminated from the game. You still have plenty of time left, so either take a potion or recover HP first before moving on."));
             } else if (getTimeLeft() > 779000 && getTimeLeft() < 781000) {
                 chr.getClient().announce(MaplePacketCreator.serverNotice(0, "Everyone that clears [The Maple Physical Fitness Test] on time will be given an item, regardless of the order of finish, so just relax, take your time, and clear the 4 stages."));
             } else if (getTimeLeft() > 839000 && getTimeLeft() < 841000

<details>
<summary>英文:</summary>

Hey this is ready made code from a Maple Story game
That I run a function for me it works for others does not
They do not get a timer. seems chr.getFitness() returning null 
I&#39;m not strong in java I would love if someone would help.
[Error 1 img][1]

 Also they cant pass last stage in game
[Error 2 img][2]


  [1]: https://i.stack.imgur.com/xGNBt.png
  [2]: https://i.stack.imgur.com/3XP28.png
MapleFitness Script
&lt;!-- begin snippet: js hide: false console: true babel: false --&gt;

&lt;!-- language: lang-js --&gt;

    /*
    	This file is part of the OdinMS Maple Story Server
        Copyright (C) 2008 Patrick Huy &lt;patrick.huy@frz.cc&gt;
    		       Matthias Butz &lt;matze@odinms.de&gt;
    		       Jan Christian Meyer &lt;vimes@odinms.de&gt;

        This program is free software: you can redistribute it and/or modify
        it under the terms of the GNU Affero General Public License as
        published by the Free Software Foundation version 3 as published by
        the Free Software Foundation. You may not use, modify or distribute
        this program under any other version of the GNU Affero General Public
        License.

        This program is distributed in the hope that it will be useful,
        but WITHOUT ANY WARRANTY; without even the implied warranty of
        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
        GNU Affero General Public License for more details.

        You should have received a copy of the GNU Affero General Public License
        along with this program.  If not, see &lt;http://www.gnu.org/licenses/&gt;.
    */

    package server.events.gm;

    import client.MapleCharacter;
    import java.util.concurrent.ScheduledFuture;
    import server.TimerManager;
    import tools.MaplePacketCreator;

    /**
     *
     * @author kevintjuh93
     */
    public class MapleFitness {
           private MapleCharacter chr;
           private long time = 0;
           private long timeStarted = 0;
           private ScheduledFuture&lt;?&gt; schedule = null;
           private ScheduledFuture&lt;?&gt; schedulemsg = null;
           
           public MapleFitness(final MapleCharacter chr) {
               this.chr = chr;
               this.schedule = TimerManager.getInstance().schedule(new Runnable() {
                @Override
                public void run() {
                if (chr.getMapId() &gt;= 109040000 &amp;&amp; chr.getMapId() &lt;= 109040004)
                    chr.changeMap(chr.getMap().getReturnMap());
                }
               }, 900000);
           }
           
           public void startFitness() {
               chr.getMap().startEvent();
               chr.getClient().announce(MaplePacketCreator.getClock(900));
               this.timeStarted = System.currentTimeMillis();
               this.time = 900000;  
               checkAndMessage();         

               chr.getMap().getPortal(&quot;join00&quot;).setPortalStatus(true);
               chr.getClient().announce(MaplePacketCreator.serverNotice(0, &quot;The portal has now opened. Press the up arrow key at the portal to enter.&quot;));
           }
           
            public boolean isTimerStarted() {
                return time &gt; 0 &amp;&amp; timeStarted &gt; 0;
            }    
        
           public long getTime() {
               return time;
           }

           public void resetTimes() {
               this.time = 0;
               this.timeStarted = 0;
               schedule.cancel(false);
               schedulemsg.cancel(false);
           }

           public long getTimeLeft() {
               return time - (System.currentTimeMillis() - timeStarted);
           }

           public void checkAndMessage() {
               this.schedulemsg = TimerManager.getInstance().register(new Runnable() {
                @Override
                public void run() {
                    if (chr.getFitness() == null) {
                        resetTimes();
                    }
                if (chr.getMap().getId() &gt;= 109040000 &amp;&amp; chr.getMap().getId() &lt;= 109040004) {
                 if (getTimeLeft() &gt; 9000 &amp;&amp; getTimeLeft() &lt; 11000) {
                     chr.getClient().announce(MaplePacketCreator.serverNotice(0, &quot;You have 10 sec left. Those of you unable to beat the game, we hope you beat it next time! Great job everyone!! See you later~&quot;));
                 } else if (getTimeLeft() &gt; 99000 &amp;&amp; getTimeLeft() &lt; 101000) {
                     chr.getClient().announce(MaplePacketCreator.serverNotice(0, &quot;Alright, you don&#39;t have much time remaining. Please hurry up a little!&quot;));
                 } else if (getTimeLeft() &gt; 239000 &amp;&amp; getTimeLeft() &lt; 241000) {
                     chr.getClient().announce(MaplePacketCreator.serverNotice(0, &quot;The 4th stage is the last one for [The Maple Physical Fitness Test]. Please don&#39;t give up at the last minute and try your best. The reward is waiting for you at the very top!&quot;));
                 } else if (getTimeLeft() &gt; 299000 &amp;&amp; getTimeLeft() &lt; 301000) {
                     chr.getClient().announce(MaplePacketCreator.serverNotice(0, &quot;The 3rd stage offers traps where you may see them, but you won&#39;t be able to step on them. Please be careful of them as you make your way up.&quot;));
                 } else if (getTimeLeft() &gt; 359000 &amp;&amp; getTimeLeft() &lt; 361000) {
                     chr.getClient().announce(MaplePacketCreator.serverNotice(0, &quot;For those who have heavy lags, please make sure to move slowly to avoid falling all the way down because of lags.&quot;));
                 } else if (getTimeLeft() &gt; 499000 &amp;&amp; getTimeLeft() &lt; 501000) {
                     chr.getClient().announce(MaplePacketCreator.serverNotice(0, &quot;Please remember that if you die during the event, you&#39;ll be eliminated from the game. If you&#39;re running out of HP, either take a potion or recover HP first before moving on.&quot;));
                 } else if (getTimeLeft() &gt; 599000 &amp;&amp; getTimeLeft() &lt; 601000) {
                     chr.getClient().announce(MaplePacketCreator.serverNotice(0, &quot;The most important thing you&#39;ll need to know to avoid the bananas thrown by the monkeys is *Timing* Timing is everything in this!&quot;));
                 } else if (getTimeLeft() &gt; 659000 &amp;&amp; getTimeLeft() &lt; 661000) {
                     chr.getClient().announce(MaplePacketCreator.serverNotice(0, &quot;The 2nd stage offers monkeys throwing bananas. Please make sure to avoid them by moving along at just the right timing.&quot;));
                 } else if (getTimeLeft() &gt; 699000 &amp;&amp; getTimeLeft() &lt; 701000) {
                     chr.getClient().announce(MaplePacketCreator.serverNotice(0, &quot;Please remember that if you die during the event, you&#39;ll be eliminated from the game. You still have plenty of time left, so either take a potion or recover HP first before moving on.&quot;));
                 } else if (getTimeLeft() &gt; 779000 &amp;&amp; getTimeLeft() &lt; 781000) {
                     chr.getClient().announce(MaplePacketCreator.serverNotice(0, &quot;Everyone that clears [The Maple Physical Fitness Test] on time will be given an item, regardless of the order of finish, so just relax, take your time, and clear the 4 stages.&quot;));
                 } else if (getTimeLeft() &gt; 839000 &amp;&amp; getTimeLeft() &lt; 841000) {
                     chr.getClient().announce(MaplePacketCreator.serverNotice(0, &quot;There may be a heavy lag due to many users at stage 1 all at once. It won&#39;t be difficult, so please make sure not to fall down because of heavy lag.&quot;));
                 } else if (getTimeLeft() &gt; 869000 &amp;&amp; getTimeLeft() &lt; 871000) {
                     chr.getClient().announce(MaplePacketCreator.serverNotice(0, &quot;[MapleStory Physical Fitness Test] consists of 4 stages, and if you happen to die during the game, you&#39;ll be eliminated from the game, so please be careful of that.&quot;));
                 }
                } else {
                 resetTimes();
                }
                }
               }, 5000, 29500);
           }
           // 14:30 [Notice][MapleStory Physical Fitness Test] consists of 4 stages, and if you happen to die during the game, you&#39;ll be eliminated from the game, so please be careful of that.
           // 14:00 [Notice]There may be a heavy lag due to many users at stage 1 all at once. It won&#39;t be difficult, so please make sure not to fall down because of heavy lag.
           // 13:00 [Notice]Everyone that clears [The Maple Physical Fitness Test] on time will be given an item, regardless of the order of finish, so just relax, take your time, and clear the 4 stages.
           // 11:40 [Notice]Please remember that if you die during the event, you&#39;ll be eliminated from the game. You still have plenty of time left, so either take a potion or recover HP first before moving on.
           // 11:00 [Notice]The 2nd stage offers monkeys throwing bananas. Please make sure to avoid them by moving along at just the right timing.
           // 10:00 [Notice]The most important thing you&#39;ll need to know to avoid the bananas thrown by the monkeys is *Timing* Timing is everything in this!
           // 8:20 [Notice]Please remember that if you die during the event, you&#39;ll be eliminated from the game. If you&#39;re running out of HP, either take a potion or recover HP first before moving on.
           // 6:00 [Notice]For those who have heavy lags, please make sure to move slowly to avoid falling all the way down because of lags.
           // 5:00 [Notice]The 3rd stage offers traps where you may see them, but you won&#39;t be able to step on them. Please be careful of them as you make your way up.
           // 4:00 [Notice]The 4th stage is the last one for [The Maple Physical Fitness Test]. Please don&#39;t give up at the last minute and try your best. The reward is waiting for you at the very top!
           // 1:40 [Notice]Alright, you don&#39;t have much time remaining. Please hurry up a little!
           // 0:10 [Notice]You have 10 sec left. Those of you unable to beat the game, we hope you beat it next time! Great job everyone!! See you later~
    }


&lt;!-- end snippet --&gt;

MapleCharacter script:

    //EVENTS
    private byte team = 0;
    private MapleFitness fitness;
    private MapleOla ola;
    private long snowballattack;
    
    public byte getTeam() {
        return team;
    }

    public void setTeam(int team) {
        this.team = (byte) team;
    }

    public MapleOla getOla() {
        return ola;
    }

    public void setOla(MapleOla ola) {
        this.ola = ola;
    }

    public MapleFitness getFitness() {
        return fitness;
    }

    public void setFitness(MapleFitness fit) {
        this.fitness = fit;
    }

    public long getLastSnowballAttack() {
        return snowballattack;
    }

    public void setLastSnowballAttack(long time) {
        this.snowballattack = time;
    }
    

</details>


# 答案1
**得分**: 0

检查MapleFitness是否为null如果是则在返回之前创建对象如下所示

```java
public MapleFitness getFitness() {

    if(fitness == null)
        fitness = new MapleFitness();

    return fitness;
}
英文:

Check if the MapleFitness is null and if it is, then create the object before returning like this

public MapleFitness getFitness() {
if(fitness == null)
fitness = new MapleFitness();
return fitness;
}

huangapple
  • 本文由 发表于 2020年7月22日 01:49:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/63020226.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定