0

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.

lfoste17
  • 11
  • 1
  • 2
    Look at the stack trace. Which function does the null pointer exception happen in? Have you included the code for that function in your question? – tgdavies Aug 14 '21 at 04:40
  • @tgdavies The breakpoint occurs when trying to access that ArrayList to build a string and write to file. It seems that by trying to skip the iteration in the for loop if the max and min attribute values are the same somehow produces a completely empty arraylist, thus causing a nullpointerexception. If I don't perform this check and remove the continue statement everything else works as intended. – lfoste17 Aug 14 '21 at 05:00
  • You're using the new *helpful* Java that tells you exactly *what* is null. – chrylis -cautiouslyoptimistic- Aug 14 '21 at 05:01
  • Seriously, you should be able to diagnose and fix this yourself. Read the linked question and the answers (e.g. this one: https://stackoverflow.com/a/24347569/139985) and apply what you have learned to >your< problem. StackOverflow is not a debugging service ... and we can't debug it for you because we can't see all of the relevant code. If you don't understand what the debugger is telling you, try and work it out from first principles; e.g. why does `arraylist` have contain a `NormalizedData` instance with a `null` value for its "normalizedValues" field? – Stephen C Aug 15 '21 at 07:45

0 Answers0