我正在尝试创建一个程序,通过让三个“讲故事者”对原作进行更改来修改我的初始故事。但是,当我尝试运行TellStory时,程序只会打印“nullnullnull”以表示最终的重述故事。我做错了什么?非常感谢您的时间:))
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;
}
}
答案 0 :(得分:3)
retellStory
方法,因此没有创建任何内容...... story1
,story2
和story3
分配任何值...... story
,Storyteller1
,Storyteller2
访问的实例字段Storyteller3
永远不会分配值,而是null
,导致NullPointerException
答案 1 :(得分:1)
retellStory
永远不会调用StoryTeller
方法。
将方法retellStory
添加到每个StoryTeller
的构造函数中
或者在初始化每个StoryTeller