我有这段代码,尝试在其上运行Junit测试,但收到一条错误消息,指出nullpointerexception我对Java还是很陌生,因此感谢所有帮助。谢谢。

/**
 * If the ip address from the String passed is valid,
 * sets the instance variable parts to store it as 4 integer values.
 * For example, if ip = "192.000168.0.0000001", parts should become {192,168,0,1}.
 * If the ip address passed is invalid, parts should become {0,0,0,0}
 *
 * remember to reset the instance array parts before you do anything else
 * @param ip
 */
public void setParts(String ip) {
    boolean checkiftrue = isValidElement(ip);
    String[] IPString = ip.split("\\.");

    if (checkiftrue = true) {
        for (int i = 0; i < IPString.length; i++)
        parts[i] = Integer.valueOf(IPString[i]);
    }
        else {
            parts = new int[]{0,0,0,0};
        }
}


我用来测试的Junit是

public void testSetPartsString() {
    correct1.setParts("12.14.16.18");
    int[] a = correct1.getParts();
    assertEquals(4, a.length);
    assertEquals(12, a[0]);
    assertEquals(14, a[1]);
    assertEquals(16, a[2]);
    assertEquals(18, a[3]);

    correct1.setParts("-12.14.16.18");
    a = correct1.getParts();
    assertEquals(4, a.length);
    assertEquals(0, a[0]);
    assertEquals(0, a[1]);
    assertEquals(0, a[2]);
    assertEquals(0, a[3]);

    correct1.setParts("255.255.255.255");
    a = correct1.getParts();
    assertEquals(4, a.length);
    assertEquals(255, a[0]);
    assertEquals(255, a[1]);
    assertEquals(255, a[2]);
    assertEquals(255, a[3]);

    correct1.setParts("12.314.16.18");
    a = correct1.getParts();
    assertEquals(4, a.length);
    assertEquals(0, a[0]);
    assertEquals(0, a[1]);
    assertEquals(0, a[2]);
    assertEquals(0, a[3]);

    correct1.setParts("255.255.255.255");

    correct1.setParts("12.16.18");
    a = correct1.getParts();
    assertEquals(4, a.length);
    assertEquals(0, a[0]);
    assertEquals(0, a[1]);
    assertEquals(0, a[2]);
    assertEquals(0, a[3]);

    correct1.setParts("255.255.255.255");

    correct1.setParts("12.16.18.20.22");
    a = correct1.getParts();
    assertEquals(4, a.length);
    assertEquals(0, a[0]);
    assertEquals(0, a[1]);
    assertEquals(0, a[2]);
    assertEquals(0, a[3]);
}


Error is here

    public static boolean isValidElement(String token) {
try {
        String[] validString = token.split("\\.");
    if (validString.length != 4) return false;
    for (String checkvalidstring: validString ) {
        int validstringchecker = Integer.parseInt(checkvalidstring);
        if ((validstringchecker < 0) || (validstringchecker > 255)) return false;
        System.out.println(validstringchecker);
    }
    } catch (NumberFormatException errorcheck){
        return false;
    }
return true;
}

最佳答案

一个经典的错误。你有

    if (checkiftrue = true) {


这为checkiftrue分配了true,并且始终求值为true。因此,即使IP无效,您也可以转到第56行,并且会发生异常。而是只做:

    if (checkiftrue) {


(或使用==,但实际上,如果没有,我会觉得它更具可读性)。

PS我发现是因为我的Eclipse在boolean checkiftrue = isValidElement(ip);行上说“未使用局部变量checkiftrue的值”。如果您花时间了解它们的含义,那么此类警告通常会非常有帮助。

10-06 14:02