Hello. I am having a problem with my code where I keep getting Null Pointer Exceptions and also I am having trouble with my file intake. This is my code minus the printlns at the end
import java.io.*; import java.util.Scanner; public class P1 { public static void main(String[] args){ int fileLinesCt=0, totalLinesCt=0; int httpCt=0, httpsCt=0, ftpCt=0, otherSchemeCt=0; int eduCt=0, orgCt=0, comCt=0, otherDomainCt=0; Scanner scan = null; String url = null, scheme = null, schemeSP = null, domain = null; File file = new File("input"); try { scan = new Scanner(file); } catch (FileNotFoundException e) { e.printStackTrace(); } Scanner scan2 = new Scanner(System.in); while (!"end".equals(scan.next()) || (!"end".equals(scan2.next()))){ if (scan.hasNext() == true) url = scan.nextLine(); String[] parts = url.split(":"); scheme = parts[0]; schemeSP = parts[1]; if ("http".equals(scheme)) httpCt++; if ("https".equals(scheme)) httpsCt++; if ("ftp".equals(scheme)) ftpCt++; else otherSchemeCt++; for (int j=0; j<schemeSP.length(); j++) if (schemeSP.charAt(j) == '.') domain = schemeSP.substring(j); if ("edu".equals(domain)) eduCt++; if ("org".equals(domain)) orgCt++; if ("com".equals(domain)) comCt++; else otherDomainCt++; fileLinesCt++; totalLinesCt++; }
Now what the basis of the assignment is is that i have to take files that include urls that will be automatically tested to my code and run through them counting the number of times different schemes appear (http,https) and count the number of times different domains appear(.com,.edu). Now I do not know what the names of these files will be and this seems to be a problem I am having. Also, Null Pointer Exception is appearing on line 19 which is the first line of the while loop. Also, a FileNotFound exception is appearing on line 13 whick is the one that says scan = new Scanner(file);.
Any help would be great.