当查询无结果时,自定义生成的搜索会出现NullPointException

时间:2018-12-10 03:21:55

标签: for-loop search equals contains do-while

我正在搜索学校项目,这整天都被困住了。我已经尝试了所有我能想到的。我一定在某个地方缺少一些简单的东西,只是不确定现在要做什么。

我在第56行的线程“ main” java.lang.NullPointerException 中得到了 Exception,这是FOR循环中的第一个IF。仅在搜索找不到任何内容时才会发生这种情况。其他一切似乎都正常。

在此先感谢所有尝试的人。

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)

1 个答案:

答案 0 :(得分:0)

FOR循环使用MAX常量而不是listSize变量。如果没有完整的代码,你们将一无所知。我也将continueSearch更新为布尔值(感谢@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)