user2877214
user2877214

Reputation: 3

What is wrong with my java program?

I am trying to create a program that modifies my initial story by having the three "Storyteller's" make changes to the original. However, when I try running TellStory the program only prints "nullnullnull" for the final retold story. What did I do wrong? Thank you very much for your time :)

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;
     }
}

Upvotes: 0

Views: 102

Answers (2)

Java Devil
Java Devil

Reputation: 10969

The retellStory method is never called by the StoryTeller.

Add the method retellStory to the constructor for each StoryTeller or add the call to the method after initialising each StoryTeller

Upvotes: 1

MadProgrammer
MadProgrammer

Reputation: 347334

  1. Nobody is calling the retellStory method, therefore nothing is being created...
  2. The values story1, story2 and story3 are never being assigned any values...
  3. The instance field story, which is being accessed by Storyteller1, Storyteller2, Storyteller3 is never assigned a value and will be null, causing a NullPointerException

Upvotes: 3

Related Questions