0

I am building a search for a school project and I have been stuck on this all day. I have tried everything I can think of. I must be missing something simple somewhere, just not sure what at this point.

I am getting the Exception in thread "main" java.lang.NullPointerException at line 56 which is the first IF inside of the FOR loop. This only happens if the search doesnt find anything. Everything else seems to be working fine.

Thanks in advance to anyone who gives this a go.

else if ( menuSelect == 3)
        {
            do
            {
                Display.search(fullLine);
                continueSearch = 1;
                search = input.nextLine();
                if (search.length() == 10 && search.matches(regex))
                {
                    search = search.replaceFirst("(\\d{3})(\\d{3})(\\d+)", "($1) $2-$3"); // format phone number
                } // end IF search match regex (phone format)

                for (int index = 0; index < MAX && continueSearch == 1; index++)
                {
                    if (leadArray[index].getName().equalsIgnoreCase(search)
                            || leadArray[index].getAddress().equalsIgnoreCase(search) 
                            || leadArray[index].getPhone().equalsIgnoreCase(search) 
                            || leadArray[index].getEmail().equalsIgnoreCase(search))
                    {

                        Pages.viewLead(listSize, noteListSize, index, noteArray, leadArray);
                        menuSelect = 0;
                        continueSearch = 0;
                    } // end IF search matches data params
                    else if (leadArray[index].getName().contains(search) 
                            || leadArray[index].getAddress().contains(search) 
                            || leadArray[index].getPhone().contains(search) 
                            || leadArray[index].getEmail().contains(search))
                    {                   
                        Pages.searchAllLeads(listSize, noteListSize, search, noteArray, leadArray);
                        menuSelect = 0;
                        continueSearch = 0;
                    } // end IF search contains data params 
                    else if (search.equals("00"))
                    {
                        continueSearch = 0;
                        Pages.mainMenu(listSize, noteListSize, noteArray, leadArray);
                    } // end IF exit search 00              
                } // end FOR array Loop
                System.out.println("\nYour search returned (0) results.\n");
            } while (!"00".equals(search) ); // end search loop
            Pages.mainMenu(listSize, noteListSize, noteArray, leadArray);
        } // end IF menuSelect 3 (search)
Josh
  • 1
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Ken Y-N Dec 10 '18 at 03:26
  • Please learn how to use your debugger, as it should show you exactly which value is null. Also, look at (and post here) the full stack trace, as that will give further information about where the null is. (And as a side note, use `Boolean`, not 0 and 1, please!) – Ken Y-N Dec 10 '18 at 03:29
  • My bad I finally figured it out. I'm sorry guys, I was putting the debugger break point on the wrong line. I'm first year in Computer Science and we didn't learn how to use the debugger yet (having to figure it out on my own). – Josh Dec 10 '18 at 03:46

1 Answers1

0

FOR loop was using MAX constant rather than listSize variable. Nothing you guys could have known without the full code. I also updated to boolean for continueSearch (thanks @Ken Y-N).

else if ( menuSelect == 3)
        {
            do
            {
                Display.search(fullLine);
                continueSearch = true;
                search = input.nextLine();
                if (search.length() == 10 && search.matches(regex))
                {
                    search = search.replaceFirst("(\\d{3})(\\d{3})(\\d+)", "($1) $2-$3"); // format phone number
                } // end IF search match regex (phone format)

                for (int index = 0; index < listSize && continueSearch == true; index++)
                {
                    if (leadArray[index].getName().equalsIgnoreCase(search)
                            || leadArray[index].getAddress().equalsIgnoreCase(search) 
                            || leadArray[index].getPhone().equalsIgnoreCase(search) 
                            || leadArray[index].getEmail().equalsIgnoreCase(search))
                    {

                        Pages.viewLead(listSize, noteListSize, index, noteArray, leadArray);
                        menuSelect = 0;
                        continueSearch = false;
                    } // end IF search matches data params
                    else if (leadArray[index].getName().contains(search) 
                            || leadArray[index].getAddress().contains(search) 
                            || leadArray[index].getPhone().contains(search) 
                            || leadArray[index].getEmail().contains(search))
                    {                   
                        Pages.searchAllLeads(listSize, noteListSize, search, noteArray, leadArray);
                        menuSelect = 0;
                        continueSearch = false;
                    } // end IF search contains data params 
                    else if (search.equals("00"))
                    {
                        continueSearch = false;
                        Pages.mainMenu(listSize, noteListSize, noteArray, leadArray);
                    } // end IF exit search 00              
                } // end FOR array Loop
                System.out.println("\nYour search returned (0) results.\n");
            } while (!"00".equals(search) ); // end search loop
            Pages.mainMenu(listSize, noteListSize, noteArray, leadArray);
        } // end IF menuSelect 3 (search)
Josh
  • 1