0

こんにちは皆さん、私は本当に立ち往生しています、私は取得し続けjava.lang.NullPointerExceptionます。可能な限りあらゆる場所で処理しようとしましたが、うまくいきませんでした。宿題です。見て、フィードバックをいただければ幸いですjava.lang.NullPointerException。例外はとで発生しCaptain.handleProblem() ますMalfucntionHandler.proccessMalfunction()

    public abstract class MalfunctionHandler 
    {

        MalfunctionHandler next;
        /**
         * severity is a type of Severity 
         */
        Severity severity;

        /**
         * @param description describes the severity of the problem
         */
        String description;


        /**
         * @param f file object  that refers to the log-silver.txt
         */
        File f = new File("log-silver.txt");

        MalfunctionHandler(Severity severity)
        {
                this.severity = severity;
        }
         public String getDescription()
        {
            if(description == null)
            {
                description = "No description available. Probably serious.";
            }
            return description;
        }

        final protected void processMalfunction(Malfunction malfunction)
        {
            if (this.severity == malfunction.getSeverity())
            {
               handleProblem();
            }
            else
            {
    //            if(malfunction == null)
                next.processMalfunction(malfunction);
            }
        }
        final protected void addHandler(MalfunctionHandler next)
        {
            this.next = next;
        }
        abstract void handleProblem();

        public Severity getSeverity() 
        {
            return severity;
        }
    }


public class Malfunction 
{
    /**
     * severity is a type of Severity 
     */
    Severity severity;

    /**
     * @param description describes the severity of the problem
     */
    String description;

    Malfunction(Severity severity, String description)
    {
        this.description = description;
        this.severity = severity;
    }

    public Severity getSeverity() 
    {
        return severity;
    }

     public String getDescription()
    {
        if(description == null)
        {
            description = "No description available. Probably serious.";
        }

        return description;
    }
}

public enum Severity 
{
     TRIVIAL, LOW, MEDIUM, HIGH
}

public class SpaceMonkey extends MalfunctionHandler {

    MalfunctionHandler malfunction;

    SpaceMonkey(Severity severity)
    {
       super(severity);
    }
    @Override
    void handleProblem() 
    {
        if(malfunction.getDescription() == null)
        {
            description = "No description available. Probably serious.";
        }
           FileUtility.writeFile(f, malfunction.getDescription()); 
           FileUtility.writeFile(f, "---> Space monkey assigned to problem.");
    }
}

public class ServiceRobot extends MalfunctionHandler {


     MalfunctionHandler malfunction;

    ServiceRobot(Severity severity)
    {
        super(severity);
    }
    void handleProblem() 

    {
       if(malfunction.getDescription() == null)
        {
            description = "No description available. Probably serious.";
        }
           FileUtility.writeFile(f, malfunction.getDescription());
           FileUtility.writeFile(f, "---> Service Robot assigned to problem.");
    }

}

public class Engineer extends MalfunctionHandler
{

     MalfunctionHandler malfunction;

    Engineer(Severity severity)
    {
        super(severity);

    }

    void handleProblem() 
    {
         if(malfunction.getDescription() == null)
        {
            description = "No description available. Probably serious.";
        }
          FileUtility.writeFile(f, malfunction.getDescription());
           FileUtility.writeFile(f, "---> Engineer assigned to problem.");
    }

}

public class Captain extends MalfunctionHandler
{
     MalfunctionHandler malfunction ;

    Captain(Severity severity)
    {
        super(severity);
    }

    @Override
   void handleProblem( ) 
    {
        if(malfunction.getDescription() == null)
        {
            description = "No description available. Probably serious.";
        }
           FileUtility.writeFile(f, malfunction.getDescription());
           FileUtility.writeFile(f, "---> Captain assigned to problem.");
    }
}
4

3 に答える 3

5
 if(malfunction.getDescription() == null)

クラスSpaceMonkeyでMalfunctionHandlerオブジェクトを初期化しておらず、 handleProblemメソッドでそのgetDescription()メソッドを呼び出そうとしています。Java オブジェクトでは、デフォルト値を null として取得し、MalfunctionHandler が誤動作します。ここでは null であり、null でそのメソッドにアクセスしようとしています。

MalfunctionHandler は抽象クラスであるため、そのサブクラス (SpaceMonkey) で初期化します。

 MalfunctionHandler malfunction; = new SpaceMonkey(Severity);
于 2012-11-09T09:55:51.983 に答える
2

Malfunction オブジェクトは初期化されておらず、Captain クラスで宣言されているだけです。

また、NullPointerExceptions をキャッチすることはお勧めできません。代わりに、そのような例外が生成されないように、コードを検証してチェックを行う必要があります。

于 2012-11-09T09:56:27.107 に答える
2

デフォルトでは、インスタンス変数のオブジェクト型は null です

 MalfunctionHandler malfunction;

 MalfunctionHandler malfunction = null;

は同じです。あなたのクラスにはこの問題があります

ここif(malfunction.getDescription() == null) で誤動作はヌルなので、ここで NPE を取得しています。

于 2012-11-09T09:56:42.987 に答える