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]);
}
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;
}