0

Hi everyone I am really stuck, I keep getting the 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. The exception occurs in Captain.handleProblem() and 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.");
    }
}
10
  • Can you post the stack trace of your NPE? Commented Nov 9, 2012 at 9:47
  • post all the error message you've got Commented Nov 9, 2012 at 9:48
  • just eddited it in the beggining before the code Commented Nov 9, 2012 at 9:49
  • 5
    You never initialise MalfunctionHandler malfunction in the Captain class, so it is null and malfunction.getDescription() will throw a NPE - there might be other NPE in your code. Commented Nov 9, 2012 at 9:50
  • 1
    @assylias.. You should post it as an answer. Commented Nov 9, 2012 at 9:51

3 Answers 3

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

you never initialized your MalfunctionHandler object in class SpaceMonkey and trying to call its getDescription() method in handleProblem method. In java Objects get default value as null your MalfunctionHandler malfunction; is null here and you are trying to access its method on null.

as your MalfunctionHandler is an abstract class, initialize it with its sub class (SpaceMonkey)

 MalfunctionHandler malfunction; = new SpaceMonkey(Severity);
Sign up to request clarification or add additional context in comments.

4 Comments

@RohitJain MalfunctionHandler is an abstract class not Malfunction :)
Sorry I mean MalfunctionHandler. malfunction is a reference of that class.
@RohitJain no worries, BTW you rock mahn .. your answers are pretty informative .. :)
But in my code it is not Malfunction malfunction and it is MalfunctionHandler malfunction
2

Malfunction object is not initialized, its just declared in the Captain class.

Also its not advisable to catch NullPointerExceptions. Instead you should validate and give checks in your code so that such exceptions won't be produced.

Comments

2

By default object type of instance variable are 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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.