如何处理java.lang.nullpointerexception

时间:2012-11-09 09:46:30

标签: java exception nullpointerexception

大家好,我真的被卡住了,我一直得到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.");
    }
}

3 个答案:

答案 0 :(得分:5)

 if(malfunction.getDescription() == null)

您从未在 SpaceMonkey 类中初始化 MalfunctionHandler 对象,并尝试在 handleProblem getDescription()方法>方法。在java Objects中,默认值为null,因为MalfunctionHandler出现故障;在这里为null,并且您尝试在null上访问其方法。

因为你的MalfunctionHandler是一个抽象类,用它的子类(SpaceMonkey)初始化它

 MalfunctionHandler malfunction; = new SpaceMonkey(Severity);

答案 1 :(得分:2)

错误对象 未初始化,它刚刚在Captain类中声明。

同样不建议捕获NullPointerExceptions。相反,您应该验证并对代码进行检查,以便不会产生此类异常。

答案 2 :(得分:2)

默认情况下,实例变量的对象类型为null

 MalfunctionHandler malfunction;

 MalfunctionHandler malfunction = null;

是一样的。你的课有这个问题

此处if(malfunction.getDescription() == null)并且故障为空,因此您在这里获得NPE。