I have the following function to perform normalization operation of some data.
public static void normalization(NormalizedData dataObject) {
// Create new arraylist<String> to store the normalized values.
ArrayList<String> normalizedValues = new ArrayList<>();
// init variables.
double normalizedValue =0;
int attributeValue = 0;
// Get the minimum and maximum attribute values, and the attribute type.
int minimumValue = dataObject.getMinimumAttributeValue();
int maximumValue = dataObject.getMaximumAttributeValue();
String dataAttributeType = dataObject.getDataAttributeType();
// Iterate through the individual strings.
for (String attribute:dataObject.getData()) {
if (maximumValue == minimumValue) {
continue;
}
else if (maximumValue <=1) {
normalizedValues.add(attribute);
}
// Check if the attribute is numerical to perform normalization operation.
else if (dataAttributeType.equals("n")) {
// Cast string to integer.
attributeValue = Integer.parseInt(attribute);
// Normalization operation on integers, cast to double.
normalizedValue = (double) (attributeValue - minimumValue) / (maximumValue - minimumValue);
// Add the string value of the normalization operation to the new arraylist, format to 3 decimal places.
normalizedValues.add(String.format("%.3f",normalizedValue));
}
// Else if the attribute is categorical, just add to the list without any operation.
else if (dataAttributeType.equals("c")) {
normalizedValues.add(attribute);
}
// Add the newly created arraylist of normalizedValues to the dataObject.
dataObject.setNormalizedValues(normalizedValues);
}
}
The function is called in a for loop to iterate through an ArrayList of objects.
for (NormalizedData dataObject:normalizedDataObjectArrayList) {
normalization(dataObject);
}
If the minimum and maximum attribute values of the data object are the same, the current iteration should be skipped. When trying to do this comparing the min max and having a continue statement, no values are ever added to the dataObject, and when the next block of code is executed to write the ArrayList of normalized values from the Data Object to a .txt file, I get the following error.
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "java.util.List.get(int)" because "stringArrayList" is null
at Normalization.buildData(Normalization.java:129)
at Normalization.generateNormalizedData(Normalization.java:72)
at Task1.mainMenu(Task1.java:46)
at Task1.main(Task1.java:22)
The actual breakpoint occurs when trying to then build a string to write to a .txt file with this function.
public static String buildData(ArrayList<NormalizedData> arrayList) {
StringBuilder stringBuilder = new StringBuilder();
// Init counter.
int i = 0;
// Do while loop
do {
// Iterate through each dataObject in the ArrayList
for (NormalizedData dataObject : arrayList) {
// Create a list of strings for the data of the current object.
List<String> stringArrayList = dataObject.getNormalizedValues();
// Append the string at index i for the current data object.
stringBuilder.append(stringArrayList.get(i)).append(" ");
}
// Add a new line.
stringBuilder.append("\n");
// Increase counter.
i++;
}
// Loop through entire arrayList size.
while (i <= arrayList.size());
// Return string that was created, ready to write to file.
return String.valueOf(stringBuilder);
}
This breakpoint only occurs when I try to skip iterations where the maximum and minimum attributes are the same by adding the following to the normalization function. It seems trying to skip an iteration in the for loop somehow results in the ArrayList being null.
if (maximumValue == minimumValue) {
continue;
}
Everything works as intended until I try to do this check.