简体   繁体   English

如何处理java.lang.nullpointerexception

[英]how to handle java.lang.nullpointerexception

Hi everyone I am really stuck, I keep getting the java.lang.NullPointerException . 大家好,我真的很困惑,我一直在获取java.lang.NullPointerException I tried to handle it in every possible place but I am not doing it successfully. 我尝试在所有可能的地方进行处理,但未成功完成。 It is homework.If you can look and give some feedback about the java.lang.NullPointerException it would be great. 这是家庭作业。如果您可以看一下并提供有关java.lang.NullPointerException反馈,那就太好了。 The exception occurs in Captain.handleProblem() and MalfucntionHandler.proccessMalfunction() 该异常发生在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.");
    }
}
 if(malfunction.getDescription() == null)

you never initialized your MalfunctionHandler object in class SpaceMonkey and trying to call its getDescription() method in handleProblem method. 你从来没有初始化的MalfunctionHandler对象类SpaceMonkey并试图调用它的getDescription()handleProblem方法的方法。 In java Objects get default value as null your MalfunctionHandler malfunction; 在Java对象中,将默认值设置为null会导致MalfunctionHandler故障; is null here and you are trying to access its method on null. 在此处为null,而您尝试在null上访问其方法。

as your MalfunctionHandler is an abstract class, initialize it with its sub class (SpaceMonkey) 由于您的MalfunctionHandler是抽象类,请使用其子类(SpaceMonkey)对其进行初始化

 MalfunctionHandler malfunction; = new SpaceMonkey(Severity);

Malfunction object is not initialized, its just declared in the Captain class. 故障对象未初始化,只是在Captain类中声明。

Also its not advisable to catch NullPointerExceptions. 同样不建议捕获NullPointerExceptions。 Instead you should validate and give checks in your code so that such exceptions won't be produced. 相反,您应该验证并在代码中进行检查,以免产生此类异常。

By default object type of instance variable are null 默认情况下,实例变量的对象类型为null

 MalfunctionHandler malfunction;

and

 MalfunctionHandler malfunction = null;

are same. 都一样 Your classes has this problem 您的班级有这个问题

here if(malfunction.getDescription() == null) and malfunction is null so you are getting NPE here. if(malfunction.getDescription() == null)并且故障为null,那么您将在此处获得NPE。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM