Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Recipe Book Help again

  1. #1
    Member
    Join Date
    Feb 2012
    Location
    West USA
    Posts
    67
    My Mood
    Inspired
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Recipe Book Help again

    Hey guys,

    If you saw my post earlier, I'm making a recipe book. It'll basically create recipes (that the user enters stuff for) then you can search for them and stuff.

    My problem is that when I search for something (by entering "search" then "title") after I've created a recipe, it comes up with a null pointer exception:

    Exception in thread "main" java.lang.NullPointerException
    at RecipeBookMain.SearchTitle(RecipeBookMain.java:158 )
    at RecipeBookMain.SearchFor(RecipeBookMain.java:132)
    at RecipeBookMain.RunCommand(RecipeBookMain.java:56)
    at RecipeBookMain.main(RecipeBookMain.java:22)

    Here is my RecipeBookMain:

    import java.util.Scanner;
    import java.io.*;

    public class RecipeBookMain {
     
    	static boolean hasRunBefore = false;
    	static Recipe[] recipes = new Recipe[99];
    	static int currentRecipeNumber = 0; /* This Number is always 1 less than the actual number of recipes */
     
    	static Scanner scan = new Scanner(System.in);
     
    	public static void main(String args[]) throws IOException {
     
    		BufferedReader saveFile = new BufferedReader(new FileReader("Recipe Number.txt"));
    		/* Debug */
    		currentRecipeNumber = saveFile.read();
    		//System.out.print(currentRecipeNumber);
    		/* End Debug */
     
    		System.out.print("Welcome to RBook! This program is a recipe book thats helps you put your recipes into an electronic format, and sort them so you can find them later. If you would like to view a tutorial, please enter 'Tutorial'. Otherwise, please enter a command.");
     
    			RunCommand();
     
     
    	}
     
    	public static void Tutorial() throws IOException {
     
    		System.out.println("\nRBook uses a command system. If you would like to enter a recipe, enter 'recipe' on the main screen then follow the prompts to enter your recipe. When you save a recipe, there is a Notepad file with your recipe in it if you would like to move it without moving the program. If you would like to search for recipes, enter 'search'. It will then ask if you would like to search for names or tags. Tags are used when you want to have a certain category for your recipe, like vegetarian, etc. If you want to reset your recipe book, enter 'reset'.");
    		RunCommand();
     
    	}
     
    	public static void RunCommand() throws IOException {
     
    		String input;
     
    		System.out.print("\nWhat would you like to do?");
    		input = scan.nextLine();
     
    		switch(input.toLowerCase()) {
     
    		case "tutorial":
    			Tutorial();
    			RunCommand();
     
    		case "recipe":
    			EnterRecipe();
     
    		case "clear":
    			Clear();
     
     
     
    		case "search":
    			SearchFor();
     
    		}
     
    	}
     
    	public static void EnterRecipe() throws IOException {
     
    		String title;
    		String tag;
    		String author;
    		String recipe;
     
    		System.out.print("Title?");
    		title = scan.nextLine();
     
    		System.out.print("Tag?");
    		tag = scan.nextLine();
     
    		System.out.print("Author?");
    		author = scan.nextLine();
     
    		System.out.print("Recipe?");
    		recipe = scan.nextLine();
     
    		recipes[currentRecipeNumber] = new Recipe(title, tag, author, recipe);
    		//System.out.print(recipes[currentRecipeNumber].name); /* This is a debug line */
    		System.out.print("Your recipe has been entered.\n");
    		currentRecipeNumber++;
    		FileWriter saveFile = new FileWriter("Recipe Number.txt");
    		saveFile.write(currentRecipeNumber);
    		saveFile.close();
     
    		FileWriter recipeFile = new FileWriter(title + ".txt");
    		recipeFile.write("Title: " + title + "\n");
    		recipeFile.write("Tag: " + tag + "\n");
    		recipeFile.write("Author: " + author + "\n");
    		recipeFile.write("Recipe: " + recipe + "\n");
    		recipeFile.close();
     
    		RunCommand();
     
    	}
     
    	public static void Clear() throws IOException {
     
    		for(currentRecipeNumber = currentRecipeNumber; currentRecipeNumber >= 0; currentRecipeNumber--) {
     
    			recipes[currentRecipeNumber] = null;
    			if(currentRecipeNumber - 1 < 0) {
     
    				break;
     
    			}
     
    		}
     
    		currentRecipeNumber = 0;
    		FileWriter saveFile = new FileWriter("Recipe Number.txt");
    		saveFile.write(currentRecipeNumber);
    		saveFile.close();
    		System.out.print("Your recipe book has been cleared.");
    		RunCommand();
     
    	}
     
    	public static void SearchFor() throws IOException {
     
    		System.out.print("What would you like to search for? If you would like to go back to the main menu, enter 'main'.");
     
    		String input;
    		input = scan.next();
     
    		switch(input) {
     
    		case "title":
    			SearchTitle();
     
    		case "tag":
    			//SearchTag();
     
    		case "author":
    			//SearchAuthor();
     
    		case "main":
    			RunCommand();
     
    		}
     
    	}
     
    	public static void SearchTitle() throws IOException {
     
    		System.out.print("Enter your search: ");
    		String input;
    		input = scan.next();
     
    		int[] results = new int[100];
    		int resultNumber = 0;
     
    		for(int par1 = currentRecipeNumber; par1 >= 0; par1--) {
     
    			if(recipes[par1].title == input) {
     
    				results[resultNumber] = par1;
    				resultNumber++;
     
    			}
     
    		}
     
    		System.out.print("Your search returned " + resultNumber + " results: ");
     
    		for(int par2 = resultNumber; par2 <= 0; par2--) {
     
    			System.out.print(recipes[results[par2]].title);
     
    		}
     
    	}
     
    }

    Am I mis-referencing or something? Am I missing something?

    Thanks guys.

    -Silent


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Recipe Book Help again

    Exception in thread "main" java.lang.NullPointerException
    at RecipeBookMain.SearchTitle(RecipeBookMain.java:158 )
    There is a variable with a null value on line 158. Look at line 158 in the your source and see what variable is null. Then backtrack in the code to see why that variable does not have a valid value.
    If you can not tell which variable it is, add a println just before line 158 and print out the values of all the variables on that line.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Feb 2012
    Location
    West USA
    Posts
    67
    My Mood
    Inspired
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Recipe Book Help again

    Hmm, ok, so that means that the title for recipe[par1] (the current recipe being evaluated) does not have anything stored. But that's not possible, unless par1 = 0 before we start, because there has to be some recipe being evaluated. And in order for that to happen, there would have had to be a recipe stored, which was created earlier.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Recipe Book Help again

    What variable has the null value?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Feb 2012
    Location
    West USA
    Posts
    67
    My Mood
    Inspired
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Recipe Book Help again

    recipe[par1].title

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Recipe Book Help again

    recipe[par1].title
    Where does the code assign values to the contents of the recipe array?
    Defining an array of objects creates empty slots with null values. The code needs to assign values to the slots as needed.
    Does the slot at par1 contain an object or is it still null?
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Feb 2013
    Location
    Germany
    Posts
    27
    Thanks
    0
    Thanked 5 Times in 5 Posts

    Default Re: Recipe Book Help again

    I m not sure, cause i cannot debug the code due to the missing Recipse class(I know it's easy to implement but it's late

    but I think the start value of currentRecipeNumber is wrong. You increment it after an input. Initializing value is zero, array index is also zero, everything is ok, but the loop starts now at 1.

    I suggest to initialize the currentRecipeNumber with -1 and increment it before you handle the input values.

Similar Threads

  1. Book
    By delhiris1 in forum Android Development
    Replies: 3
    Last Post: May 2nd, 2013, 08:02 AM
  2. Recipe Book: Arrays and Objects/Types
    By SilentNite17 in forum Object Oriented Programming
    Replies: 2
    Last Post: February 27th, 2013, 11:24 PM
  3. Which book out of these two shall I get?
    By spect4cle in forum The Cafe
    Replies: 1
    Last Post: May 5th, 2012, 06:24 PM
  4. BOOK
    By princz in forum Member Introductions
    Replies: 2
    Last Post: July 28th, 2011, 04:23 AM
  5. Java Book
    By stefanos in forum Java Theory & Questions
    Replies: 5
    Last Post: September 22nd, 2010, 01:38 PM