2013-10-14 78 views
0

我試圖通過讓三個「講故事的人」對原始文件進行更改來創建一個修改我的初始故事的程序。但是,當我嘗試運行TellStory時,程序僅打印出「nullnullnull」作爲最終的重播故事。我做錯了什麼?非常感謝您的寶貴時間:)我的java程序有什麼問題?

public class TellStory{ 
public String story; 
public static void main(String[]args){ 
    String story="The three little pigs went shopping for lettuce. When they arrived at   the grocery store, they found they had forgotten their money. They asked the manager if they could have some credit, but he refused. The three little pigs saw their friend, Mr. Turtle, and he agreed to give them a loan. They went home happy and had a wonderful meal!"; 
    System.out.println("Welcome to my Story"); 
    System.out.println(""); 
    System.out.println("Initial Story:"); 
    System.out.println(story); 
    System.out.println(""); 

    Storyteller1 Frank=new Storyteller1(); 
    Storyteller2 John=new Storyteller2(); 
    Storyteller3 Jake=new Storyteller3(); 

    String storyRetoldByFrank=Frank.getStory1(); 
    String storyRetoldByJohn=John.getStory2(); 
    String storyRetoldByJake=Jake.getStory3(); 

    String FinalRetoldStory=storyRetoldByFrank+storyRetoldByJohn+storyRetoldByJake; 

    System.out.println("Final Retold Story:"); 
    System.out.println(FinalRetoldStory); 
    } 
} 

public class Storyteller1 extends TellStory{ 
public String story1; 
public String retellStory(String story1){ 
    int startIndex1=story.indexOf("three little pigs went"); 
    String subStr1=story.substring(startIndex1,startIndex1+17); 
    String subStr2=story.replaceAll(subStr1,"four chickens"); 

    int startIndex2=story.indexOf("lettuce. When"); 
    String subStr3=story.substring(startIndex2,startIndex2+8); 
    String subStr4=story.replaceAll(subStr3,"raisins"); 

    int startIndex3=story.indexOf("grocery store, they"); 
    String subStr5=story.substring(startIndex3,startIndex3+14); 
    String subStr6=story.replaceAll(subStr5,"mall"); 

    int startIndex4=story.indexOf("money. They"); 
    String subStr7=story.substring(startIndex4,startIndex4+6); 
    String subStr8=story.replaceAll(subStr7,"friend"); 

    story1=subStr2+" went shopping for "+subStr4+". "+"When they arrived at the "+subStr6+", they found they had forgotten their "+subStr8+"."; 
    return story1; 
    } 
    public String getStory1(){ 
    return story1; 
    } 
} 

public class Storyteller2 extends TellStory{ 
public String story2; 
public String retellStory(String story2){ 
    int startIndex5=story.indexOf("manager"); 
    String subStr9=story.substring(startIndex5,startIndex5+8); 
    String subStr10=story.replaceAll(subStr9,"security guard"); 

    int startIndex6=story.indexOf("some credit"); 
    String subStr11=story.substring(startIndex6,startIndex6+12); 
    String subStr12=story.replaceAll(subStr11,"a phone"); 

    story2="They asked the "+subStr11+" if they could have "+subStr11+", but he refused."; 
    return story2; 
    } 
    public String getStory2(){ 
    return story2; 
    } 
} 

public class Storyteller3 extends TellStory{ 
public String story3; 
public String retellStory(String story3){ 
    int startIndex7=story.indexOf("friend"); 
    String subStr13=story.substring(startIndex7,startIndex7+7); 
    String subStr14=story.replaceAll(subStr13,"nemesis"); 

    int startIndex8=story.indexOf("Mr. Turtle"); 
    String subStr15=story.substring(startIndex8,startIndex8+11); 
    String subStr16=story.replaceAll(subStr15,"Fearsome Fox"); 

    int startIndex9=story.indexOf("loan"); 
    String subStr17=story.substring(startIndex9,startIndex9+5); 
    String subStr18=story.replaceAll(subStr17,"ride"); 

    int startIndex10=story.indexOf("happy and"); 
    String subStr19=story.substring(startIndex10,startIndex10+10); 
    String subStr20=story.replaceAll(subStr19,"with Fearsome Fox - he"); 

    story3="The three little pigs saw their "+subStr14+","+subStr16+" and he agreed to  give them a "+subStr18+". They went home "+subStr20+"had a wonderful meal!"; 
    return story3; 
    } 
    public String getStory3(){ 
    return story3; 
    } 
} 
+1

標題後添加調用方法應該是錯誤或缺陷,而不是一個籠統的說法,詢問「有什麼不好?」。 – IHazABone

回答

3
  1. 沒有人調用retellStory方法,因此正在創建什麼...
  2. story1story2story3永遠不會被分配任何值。 ..
  3. 的實例字段story,正在由Storyteller1Storyteller2訪問,Storyteller3從未分配了值並且將null,引起NullPointerException
+0

'main'中的局部變量'story'獨立於_field_'story'。 –

+0

@JoopEggen但它永遠不會傳遞給任何子類,並且子類顯式引用'story',因此它會拋出'NullPointerExceptions',因爲'TellStory @ story'永遠不會被初始化。實際上,大多數情況下,子類甚至不會引用傳遞給它們的參數... – MadProgrammer

+0

是的,我認爲問題在於理解_language concepts_。彷彿有人想要填補領域故事,story1等 –

1

retellStory方法永遠不會被StoryTeller調用。

方法retellStory添加到構造每個StoryTeller 或初始化每個StoryTeller