Page 1 of 1

My code throws a "java.util.ArrayList.get(int)" is null. How can I fix this. Here's my code: import java.util.ArrayList;

Posted: Mon Jul 11, 2022 9:54 am
by answerhappygod
My code throws a "java.util.ArrayList.get(int)" is null. How canI fix this.
Here's my code:
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
public class Main {
private static final int RUNS_UP = 1;
private static final int RUNS_DN = -1;
public static void main(String[] args) {
// instantiate an object of the Mainclass and call run on it
Main mainObject = new Main();
mainObject.run();
}
// Create run method that throwsFileNotFoundException.
public void run() {
ArrayList<Integer> inputList =new ArrayList<>();
try {
inputList =readInputFile("C:\\Users\\HP\\Desktop\\ASU2021Classes\\CSE205\\Projects\\SloydBmulamaJchern34-P01\\p01-in.txt");
} catch (FileNotFoundException e){
System.out.println("Oops, could notopen 'p01-in.txt' for reading. The program is ending");
System.exit(-100);
}
ArrayList<Integer>listRunsUpCount = new ArrayList<>();
ArrayList<Integer>listRunsDownCount = new ArrayList<>();
listRunsUpCount =findRuns(inputList, RUNS_UP);
listRunsDownCount =findRuns(inputList, RUNS_DN);
ArrayList<Integer>listRunsCount = mergeLists(listRunsUpCount, listRunsDownCount);
try {
writeOutputFile("C:\\Users\\HP\\Desktop\\ASU2021Classes\\CSE205\\Projects\\SloydBmulamaJchern34-P01\\p01-runs.txt",listRunsCount);
} catch (FileNotFoundException e){
System.out.println("Oops, could notopen 'p01-runs.txt' for writing. The program is ending");
System.exit(-200);}
}
// Method to calculate runs up and runs down.
public ArrayList<Integer>findRuns(ArrayList<Integer> pList, int pDir) {
ArrayList<Integer>listRunsCount = new ArrayList<>(Arrays.asList(newInteger[pList.size()]));
int i = 0;
int k = 0;
while (i < pList.size() - 1){
if (pDir == RUNS_UP&& pList.get(i) <= pList.get(i + 1)) {
k++;
} else if (pDir ==RUNS_DN && pList.get(i) >= pList.get(i + 1)) {
k++;
} else {
if (k !=0) {
listRunsCount.set(k, listRunsCount.get(k) + 1);
k = 0;
}
}
i += 1;
}
if (k != 0) {
listRunsCount.set(k,listRunsCount.get(k) + 1);
}
return listRunsCount;
}
// This method merges the runsUp and runsDownarrays.
public ArrayList<Integer>mergeLists(ArrayList<Integer> pListRunsUpCount,ArrayList<Integer> pListRunsDnCount) {
ArrayList<Integer>listRunsCount = new ArrayList<>(Arrays.asList(newInteger[pListRunsUpCount.size()]));
for (int i = 0; i <pListRunsUpCount.size() - 1; i++) {
int sum =pListRunsUpCount.get(i) + pListRunsDnCount.get(i);
listRunsCount.set(i,sum);
}
return listRunsCount;
}
// This method creates ArrayList object.
public static ArrayList<Integer>arrayListCreate(int pSize, int pInitValue) {
ArrayList<Integer> list = newArrayList<>();
for (int i = 0; i < pSize; i++){
list.add(pInitValue);
}
return list;
}//????
// This method creates an output file
public void writeOutputFile(String pFilename,ArrayList<Integer> pListRuns) throws FileNotFoundException{
PrintWriter output = newPrintWriter(pFilename);
output.write(String.format("runs_total: %d\n",pListRuns.stream().reduce(0, Integer::sum)));
for (int i = 1; i <pListRuns.size(); i += 1) {
output.write(String.format("runs_%d: %d\n", i,pListRuns.get(i)));
}
output.close();
}
// This method reads the input file
public ArrayList<Integer>readInputFile(String pFilename) throws FileNotFoundException {
File file = new File(pFilename);
try (Scanner fileScanner = newScanner(file)) {
ArrayList<Integer> inputList = new ArrayList<>();
while(fileScanner.hasNextInt()) {
inputList.add(fileScanner.nextInt());
}
return inputList;
}
}
}