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