0

I have this code and I tried running a Junit test on it and I am getting a error that says nullpointerexception I am fairly new to Java so any and all help is appreciated. thank you.

/**
 * 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};
        }
}

and the Junit i am using to test is

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;
}
Dan
  • 89
  • 8
  • Please include the exception as it provides valuable information, e.g. line number where the exception occurred. – Martin4ndersen Sep 11 '16 at 06:57
  • Thanks. It says the NullPointerException happened in line 56 of IPAddress.java. Could you point out which line is line 56? Thx. – Ole V.V. Sep 11 '16 at 07:45
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it). – Ole V.V. Sep 11 '16 at 07:52

1 Answers1

1

A classic error. You have

    if (checkiftrue = true) {

This assigns true to checkiftrue and always evaluates to true. So even when IP is invalid, you go to line 56, and the exception happens. Instead do just:

    if (checkiftrue) {

(or use ==, but really, I find it more readable without).

PS I found out because my Eclipse on the line boolean checkiftrue = isValidElement(ip); said “The value of the local variable checkiftrue is not used”. Such warnings are often very helpful if you take the time to find out what they mean.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
  • You’re welcome. Tricky to spot, obvious once you’ve seen it. In hindsight, another way you might have discovered was from the stacktrace: it says the method was called from line 92, and you might or might not have been able to tell than when called from line 92 it was not supposed to have a valid IP address and hence not supposed to go into the `if`part of the if-else statement. – Ole V.V. Sep 11 '16 at 09:26
  • The most important thing about this answer: turn on all the compiler warnings! – Robert Sep 11 '16 at 13:55