ArrayList:线程"main"java.lang.NullPointerException中的异常

-2 java exception arraylist

当我尝试添加人时,我仍然有问题.该代码被认为是检测无效的名称和生日,并且不会将此人添加到personList中.但是,因为在代码读取之前我无法获得生日,所以只能在循环结束时添加person对象.当我尝试测试添加一个invlid人名和/或生日时,会出现问题.表明:

无法添加人员:名称无效!!

新纪录:

姓名:null"

生日:未知的DOB

已被添加!

Exception in thread "main" java.lang.NullPointerException
at Instruction.readInstruction(Instruction.java:272) 
at EPB.main(EPB.java:12)





else if(words[0].equalsIgnoreCase("add"))
                {   
                    Person p = new Person();

                    String s1 = line.substring(words[0].length()).trim();
                    String[] s2 = s1.split(";");
                    if(s2.length>=2)// s2[0] = " name Testing Three" s2[1] = " birthday 13-05-1981" and s2[2]=" address.."
                    {
                        for(int i=0; i<s2.length;i++)
                        {
                            String s3 = s2[i].trim(); // "delete the leading space" s2[0] = "name Testing Three" s2[1] = "birthday 13-05-1981"
                            String[] s4 = s3.split("\\s+"); //s4[0] = name; s4[1] = Testing; s4[2]=Three 


                            if(s4.length>=2) // s2[1]=birthday 13-05-1986 only has length of 2
                            {
                                if(s4[0].equalsIgnoreCase("name"))
                                {
                                        //System.out.println(s4[1]);
                                    String name="";
                                    if(Functions.nameValidation(s4[1]))
                                    {
                                        for(int j=2;j<s4.length;j++)
                                        {
                                            name = s4[1] + " " + s4[j];
                                        }
                                        if(Functions.nameValidation(name))
                                        {
                                            p.setName(name);
                                        }
                                        else                                                
                                        {
                                            System.out.println("Failed to add person: Invalid name!");
                                            break;
                                        }

                                    }                                           
                                    else
                                    {
                                        System.out.println("Failed to add person: Invalid name!!");
                                        break;
                                    }
                                }//end of word equals to name
                                    //-----------------------------------------------------------------
                                else if(s4[0].equalsIgnoreCase("birthday") && s4.length ==2)
                                {
                                    if(Functions.dateValidation(s4[1]))
                                    {
                                        try 
                                        {
                                            p.setBirthdayString(s4[1]);
                                        } 
                                        catch (ParseException e) 
                                        {
                                                //e.printStackTrace();
                                        }
                                    }
                                    else
                                    {
                                        System.out.println("Failed to add person: Invalid Birthday Format");
                                        break;
                                    }
                                }
                                    //end of word equals to birthday
                                    //-----------------------------------------------------------------
                        boolean notFound = false;
                        for(Person p1: personList)
                        {
                            if(p1.getName().equals(p.getName()))
                            {
                                if(p1.getBirthdayString().equals(p.getBirthdayString()))    
                                {
                                    System.out.println("Information in record" +"\n" + "name: "+ p.getName() + "\n" + "Birthday: " + p.getBirthdayString() + "\n" +"has been updated");
                                    p1.setEmail(p.getEmail());
                                    p1.setPhone(p.getPhone());
                                    p1.setAddress(p.getAddress());

                                    notFound = true;
                                }
                            }
                        }
                        if (!notFound)
                        {
                            if(Functions.nameValidation(p.getName()) && Functions.dateValidation(p.getBirthdayString()))
                            {
                                System.out.println("New record: " +"\n"+"Name: " + p.getName() + "\""+ "\n"+ "Birthday: " + p.getBirthdayString() + "\nhas been added!");
                                    personList.add(p);

                            }
                            FileIO.outData(personList, outputFileName);     
                        }
                        System.out.println();
                }
Run Code Online (Sandbox Code Playgroud)

Jon*_*eet 8

这就是问题:

for(Person p1: personList)
{
    ...
    personList.add(p);
}
Run Code Online (Sandbox Code Playgroud)

在迭代它时,您无法修改集合.最简单的修复可能是在迭代时建立一个单独的列表personList,然后在以后添加所有内容:

List<Person> newPeople = new ArrayList<Person>();
for(Person p1: personList)
{
    ...
    newPeople.add(p);
}
personList.addAll(newPeople);
Run Code Online (Sandbox Code Playgroud)

(正如彼得指出,你可能不希望添加这个反正 -但是这就是为什么你要除外.)