2012-11-09 30 views
0

大家好我真的被卡住了,我不斷收到java.lang.NullPointerException。我試圖在每個可能的地方處理它,但我沒有成功。這是家庭作業。如果你可以看看並給出一些關於java.lang.NullPointerException的反饋,那會很好。發生 異常在Captain.handleProblem()MalfucntionHandler.proccessMalfunction()如何處理java.lang.nullpointerexception

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."); 
    } 
} 
+0

你可以發佈NPE的堆棧跟蹤嗎? –

+0

發佈所有錯誤信息 – Drogba

+0

只是在代碼開始前編輯它 –

回答

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

你從來沒有初始化的MalfunctionHandler對象類SpaceMonkey,並試圖調用其getDescription()在handleProblem方法方法。在Java中,對象的默認值爲null,您的MalfunctionHandler故障;在這裏爲null,你試圖在null上訪問它的方法。

爲您MalfunctionHandler是一個抽象類,其子類(SpaceMonkey)

MalfunctionHandler malfunction; = new SpaceMonkey(Severity); 
+0

如果您注意到MalfunctionHandler是抽象類。 –

+0

沒有它。公開課故障 – PermGenError

+0

@RohitJain MalfunctionHandler是一個抽象類,不是故障:) – PermGenError

2

故障對象沒有初始化,它只是在船長類中聲明初始化。

此外它不建議捕獲NullPointerExceptions。相反,您應該驗證並在您的代碼中進行檢查,以避免產生此類異常。

2

通過實例變量的缺省對象類型都爲空

MalfunctionHandler malfunction; 

MalfunctionHandler malfunction = null; 

相同。你的班級有這個問題

這裏if(malfunction.getDescription() == null)和故障爲空,所以你在這裏得到NPE。

相關問題