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.");
}
}
MalfunctionHandler malfunctionin theCaptainclass, so it is null andmalfunction.getDescription()will throw a NPE - there might be other NPE in your code.