I have two string arrays in ar1 and ar2, and I am reading the input from file and storing in arrays , ar1 contains
Cat
Lam
Orange
Kam
Ramveer
None
Tue
Apple
ar2 contains
Dog
elephant
Kam
Monday
Parrot
Queen
Ramveer
Tuesday
Xmas
I am trying to sort the arrays in alphabetical order, and i am using Array.sort() , but getting the exception
Exception in thread "main" java.lang.NullPointerException
at java.util.ComparableTimSort.binarySort(ComparableTimSort.java:232)
at java.util.ComparableTimSort.sort(ComparableTimSort.java:176)
at java.util.ComparableTimSort.sort(ComparableTimSort.java:146)
at java.util.Arrays.sort(Arrays.java:472)
at CompareArrays.pr1(CompareArrays.java:51)
at CompareArrays.main(CompareArrays.java:86)
Java Result: 1 BUILD SUCCESSFUL (total time: 0 seconds)
Code
File file1= new File("C:\\Users\\Ramveer\\Desktop\\updates\\f1.txt");
File file2=new File("C:\\Users\\Ramveer\\Desktop\\updates\\f2.txt");
Scanner sc1=new Scanner(file1);
Scanner sc2=new Scanner(file2);
while(sc1.hasNextLine()){
ar1[c1]=sc1.nextLine();
c1++;
}
while(sc2.hasNextLine()){
ar2[c2]=sc2.nextLine();
c2++;
}
Arrays.sort(ar1);
for(int k=0;k<c1;k++){
System.out.println(ar1[k]);}
}
Any help would be great. Thanks!
c1not initialized?..