Question: I have this code. But when i run it, it says Exception in thread main java.lang.NullPointerException. Can someone please fix this? import java.io.BufferedReader; import java.io.FileReader;

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

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 arraylist = new ArrayList(); LinkedList linkedlist = new LinkedList();

BufferedReader br = new BufferedReader(new FileReader("C:\\Users\\Nick\\Documents\\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 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 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); } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!