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 对象中,您的 MalfunctionHandler 故障将默认值为 null;此处为 null 并且您正尝试在 null 上访问其方法。

由于您的 MalfunctionHandler 是一个抽象类,请使用其子类(SpaceMonkey)对其进行初始化

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

故障对象未初始化,它只是在 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 回答