Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I have this code. But when i run it, it says Exception in thread \"main\" java.l

ID: 3740589 • Letter: I

Question

I have this code. But when i run it, it says Exception in thread "main" java.lang.NullPointerException. Can someone please fix this and print the updated fixed code solution?

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.lang.instrument.Instrumentation;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;

public class ArrayLinkedListExamples {
private static Instrumentation instrumentation;
public static void main(String args[]) throws IOException {

// Creating an empty Array and Linked list
ArrayList<String> arraylist = new ArrayList<String>();
LinkedList<String> linkedlist = new LinkedList<String>();

BufferedReader br = new BufferedReader(new FileReader("C:midData.csv"));

String strLine;
//Read File Line By Line
while (!((strLine = br.readLine()) == null)) {
arraylist.add(strLine);
linkedlist.add(strLine);
}
br.close();

long size = instrumentation.getObjectSize(arraylist);
System.out.println("Bytes used by Arraylist object: " + size);
size = instrumentation.getObjectSize(linkedlist);
System.out.println("Bytes used by Arraylist object: " + size);

long startl = 0, countl = 0;
for (Iterator<String> it = linkedlist.iterator(); it.hasNext();) {
String str = it.next();
String [] arrOfStr = str.split(",");
if(Integer.parseInt(arrOfStr[1]) > 55){
countl++;
}
}

long timel = System.nanoTime() - startl;
long starta = 0, counta = 0;
for (Iterator<String> it = arraylist.iterator(); it.hasNext();) {
String str = it.next();
String [] arrOfStr = str.split(",");
if(Integer.parseInt(arrOfStr[1]) > 55){
counta++;
}
}
long timea = System.nanoTime() - starta;
System.out.println("Count and CPU time for LinkedList:");
System.out.println("Count of the number of records that have the Age > 55:"+countl);
System.out.println("The CPU time used for this operation in nano second:"+timel);
System.out.println("Count and CPU time for ArrayList:");
System.out.println("Count of the number of records that have the Age > 55:"+counta);
System.out.println("The CPU time used for this operation in nano second:"+timea);
}
}

Explanation / Answer

Here is the issue

==> This Line==> long size = instrumentation.getObjectSize(arraylist);



Reason:

private static Instrumentation instrumentation;

instrumentation is null so hence you are getting null pointer exception

PLEASE RATE if helpful. Thanks let me know if there is any concern.