空指针异常错误,不确定原因

时间:2016-02-19 06:25:29

标签: java string nullpointerexception

所以我必须创建一个方法,将输入字符串分成第一个/中间/姓氏,计算创建的“学生”数等,然后我必须创建一个测试这些方法的类。

public void setName(String newName)
{
    String[] nameInput = newName.split(" ");
    if(nameInput.length == 0)
    {
        System.out.println("Error, please enter at least two names.");
        newName = null;
    }
    else if(nameInput.length == 1)
    {
        firstName = nameInput[0];
        middleName = "";
        lastName = nameInput[1];
        newName = firstName +  lastName;
    }
    else if(nameInput.length == 2)
    {
        firstName = nameInput[0];
        middleName = nameInput[1];
        lastName = nameInput[2];
        newName = firstName + middleName + lastName;

    }

    else
    {
        System.out.println("Error! You can only enter up to three names.");

    }
}
public String getName()
{
    if (middleName == null)
    {
        return firstName + " " + lastName;
    }
    else
        return firstName + " " + middleName + " " + lastName;
}

public String getId()
{
    return identifier = generateID();
}

@Override

public String toString()
{
    return getName() + "\n" + "(" + generateID() + ")";
}


private String generateID()
{
    return UUID.randomUUID().toString();
}

这就是我测试代码的方式:

public static void testStudent()
{
    System.out.println("Trying to create testStudent1 with a single name...");
    testStudent1 = new Student("A");
    System.out.println("testStudent1.toString() is " + testStudent1.toString());
    System.out.println("testStudent1.getFirstName() is " + testStudent1.getFirstName());
    System.out.println("testStudent1.getMiddleName() is " + testStudent1.getMiddleName());
    System.out.println("testStudent1.getLastName() is " + testStudent1.getLastName());

    System.out.println("Trying to create testStudent2 with two names...");
    testStudent1 = new Student("A B");
    System.out.println("testStudent2.toString() is " + testStudent2.toString());
    System.out.println("testStudent2.getFirstName() is " + testStudent2.getFirstName());
    System.out.println("testStudent2.getMiddleName() is " + testStudent2.getMiddleName());
    System.out.println("testStudent2.getLastName() is " + testStudent2.getLastName());

    System.out.println("Trying to create testStudent3 with three names...");
    testStudent1 = new Student("A B C");
    System.out.println("testStudent3.toString() is " + testStudent3.toString());
    System.out.println("testStudent3.getFirstName() is " + testStudent3.getFirstName());
    System.out.println("testStudent3.getMiddleName() is " + testStudent3.getMiddleName());
    System.out.println("testStudent3.getLastName() is " + testStudent3.getLastName());

}

当我为具有2个名字的学生测试toString时,我一直遇到空指针异常,我不知道为什么。

3 个答案:

答案 0 :(得分:0)

编辑:问题在于testStudent方法中的testStudent()变量。

System.out.println("Trying to create testStudent1 with a single name...");
testStudent1 = new Student("A");
System.out.println("testStudent1.toString() is " + testStudent1.toString());
System.out.println("testStudent1.getFirstName() is " + testStudent1.getFirstName());
System.out.println("testStudent1.getMiddleName() is " + testStudent1.getMiddleName());
System.out.println("testStudent1.getLastName() is " + testStudent1.getLastName());

System.out.println("Trying to create testStudent2 with two names...");
Student testStudent2 = new Student("A B");
System.out.println("testStudent2.toString() is " + testStudent2.toString());
System.out.println("testStudent2.getFirstName() is " + testStudent2.getFirstName());
System.out.println("testStudent2.getMiddleName() is " + testStudent2.getMiddleName());
System.out.println("testStudent2.getLastName() is " + testStudent2.getLastName());

System.out.println("Trying to create testStudent3 with three names...");
Student testStudent3 = new Student("A B C");
System.out.println("testStudent3.toString() is " + testStudent3.toString());
System.out.println("testStudent3.getFirstName() is " + testStudent3.getFirstName());
System.out.println("testStudent3.getMiddleName() is " + testStudent3.getMiddleName());
System.out.println("testStudent3.getLastName() is " + testStudent3.getLastName());

由于您正在重新使用testStudent1变量创建Student类的新 Object 而不使用它们来调用getter函数,因此它会抛出一个NPE testStudent2testStudent3个变量。

<小时/> 回答旧问题:问题在于您的while声明。它永远不会停止。

您可以通过对{String}执行nameInput.length来查找计数。

应该是这样的:

String[] nameInput = newName.split(" ");
if (nameInput.length == 1)
{
   System.out.println("Error, please enter at least two names.");
   newName = null;
}
else if (nameInput.length == 2)
{ 
  ...
}
else if (nameInput.length == 3)
{ 
  ...
}
else 
{
  ...
}

答案 1 :(得分:0)

您可以使用StringTokenizer课程来帮助您。

import java.util.StringTokenizer
StringTokenizer test = new StringTokenizer("An example string");
while (test.hasMoreTokens()) {
   System.out.println(test.nextToken());
}

输出:

An
example
string.

countTokens()方法可以提供字符串在处理之前提供的令牌数。这样你就会知道你是否有中间名。

答案 2 :(得分:0)

请检查trim()方法

public static void getName(String newName) {
        newName = newName.trim();
        String fullName = null;
        String[] nameInput = newName.split(" ");
        switch (nameInput.length) {
        case 2:
            fullName = mergeName(nameInput[0], "", nameInput[1]);
            break;
        case 3:
            fullName = mergeName(nameInput[0], nameInput[1], nameInput[2]);
            break;
        default:
            System.out.println("Error, please enter at least two names.");
            break;
        }
        System.out.println(fullName);
    }

    public static String mergeName(String firstName, String middleName,
            String lastName) {
        String name = firstName+" " + middleName+" " + lastName;
        return name;
    }