Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Please help with code. Null Pointer Exception

  1. #1
    Junior Member
    Join Date
    Sep 2013
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Post Please help with code. Null Pointer Exception

    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.


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Please help with code. Null Pointer Exception

    NullPointerExceptions are caused by:
    MyObject obj; // initialised to null
    obj.callSomeMethod();  // NPE
    What you need to do is track down what variable is still null (never assigned a value) and why. Then you can fix it yourself. The error message will tell you what line the NPE occurs on. Place a print statement before that and print out all variables to find out what is null.
    Improving the world one idiot at a time!

  3. The Following User Says Thank You to Junky For This Useful Post:

    JMBenoit18 (September 16th, 2013)

  4. #3
    Junior Member
    Join Date
    Sep 2013
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Please help with code. Null Pointer Exception

    So i have now figured that out. Thank you. But another problem I am having now is that line 73 (schemeSP = parts[1] is giving me an ArrayIndexOutOfBounds exception. Any ideas why this is happening?

  5. #4
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Please help with code. Null Pointer Exception

    It would seem that the call to split is not working and is returning an array with a single value (the entire String) at index 0. Therefore index 1 does not exist. Print out parts[0] to confirm.
    Improving the world one idiot at a time!

Similar Threads

  1. Null Pointer Exception. Why?
    By popnfresh in forum What's Wrong With My Code?
    Replies: 4
    Last Post: April 5th, 2013, 12:06 PM
  2. Annoying null pointer exception in my code
    By oyinig in forum Member Introductions
    Replies: 1
    Last Post: July 13th, 2012, 08:06 PM
  3. [SOLVED] Getting a null pointer exception.
    By Ouzi in forum What's Wrong With My Code?
    Replies: 14
    Last Post: May 16th, 2012, 11:25 AM
  4. Null Pointer Exception Help !!
    By AlterEgo1234 in forum Member Introductions
    Replies: 1
    Last Post: March 27th, 2011, 10:07 AM
  5. Null Pointer Exception
    By MysticDeath in forum Exceptions
    Replies: 2
    Last Post: October 24th, 2009, 01:49 PM