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 8 of 8

Thread: Programme producing NullPointerException

  1. #1
    Junior Member
    Join Date
    Mar 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Programme producing NullPointerException

    Am new to programming and am trying to create a system that is similar to a train timetable system.
    I want the user to be able to enter the stops between two stations ( from a preset list of 5 stations ) and this be saved as an array list, and this array list be saved accordingly within a 2D array so if they were to look in [1][2] there would be an arraylist of stops between station 1 and 2.

    I used this to initialise the array of arraylists (in a class i created called stops)
    ArrayList<String>[][] stopsArray= (ArrayList<String>[][])new ArrayList[5][5];
     
    	public void stops(){
    		for (int i = 0; i<5; i++){
    			for (int j=0; j<5; j++){
    				stopsArray[i][j]= new ArrayList<String>();
    			}
    		}
    	}

    Then I use this code to set the stops
     
    	public void setNoStops(int stopsNo){
    		noOfStops = stopsNo;
    	}
     
    	public void setStops(int start, int end ){
    		for (int i=0; i< noOfStops ; i++){
    			System.out.println("Print Stop Number"+(i+1)+" :");
    			stopsArray[start][end].add(scanner.nextLine());
    		}
    	}
    The bottom written line of code is mentioned in the null pointer error I recieve

    Then in my main programme I declare the new object and use these methods
    the method options displays the list of five stations with a number reference

     
    System.out.println("Please choose a start station :");
    						options();
    						place1 = scanner.nextInt();
    						System.out.println();
     
    						System.out.println("Please choose an end station :");
    						options();
    						place2 = scanner.nextInt();
    						System.out.println();
     
    						System.out.println("Enter number of stops");
    						stopsno = scanner.nextInt();	
    						mystops.setNoStops(stopsno);
    						mystops.setStops(place1-1,place2-1);

    mystops is the object I created to store the information.

    when compiling I recieve no errors
    when I run the program it runs smoothly, asks for the number of stops, allows the user to enter a number, then says enter stop 1, I enter a word and the program crashes giving the error java.lang.NullPointerException

    can someone please explain where my program is going wrong?
    thanks!


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: Programme producing NullPointerException

    Please post the full text of the error message.
    To fix a NPE, look at the line where the error occurs, find the variable with the null value and backtrack to find out why that variable does not have a valid value.

    For debugging 2D arrays, the Arrays class's deepToString() method will format the array's contents for printing.
    System.out.println("an ID "+ java.util.Arrays.deepToString(theArrayName));
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Mar 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Programme producing NullPointerException

    Exception in thread "main" java.lang.NullPointerException
    at myCourseworkPackage.stops.setStops(stops.java:26)
    at train.main(train.java:179)

    Thats the full error message
    line 26 of stops is the
    stopsArray[start][end].add(scanner.nextLine());

    and line 179 of main is
    mystops.setStops(place1-1,place2-1);

    as seen in the code of my original post

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: Programme producing NullPointerException

    Did you try printing out the contents of the array to see if it had any null entries?
    Also does scanner have a valid non-null value? Print it to see.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Mar 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Programme producing NullPointerException

    I have printed the array out because what I am trying to do is have the user input an arraylist into various array values. For example for [1][3]'s value I would try add the places "derby" and "nottingham" as an arraylist?

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: Programme producing NullPointerException

    Not sure what the last post has to do with finding the variable with the null value.
    Have you found the variable with the null value yet?
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Mar 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Programme producing NullPointerException

    I used the code you gave me and it is saying all the values are null which I am guessing is the problem?
    I want the users input to change the value from null to the input?
    Could you explain why my code isn't doing that?

    Thank you for helping out, I'm new at this and finding it hard to get my head around

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: Programme producing NullPointerException

    it is saying all the values are null
    Yes that would give you a NPE.

    Does the code assign a value to the contents of the array BEFORE trying to use its contents?
    Make sure it is given a value before trying to use it.

    I want the users input to change the value from null to the input?
    The array is supposed to contain ArrayList objects which will initially be empty, not null. The code tries to add the user's input to the ArrayList. However the array has null values not ArrayList objects.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Can someone programme for me?
    By chemical_dog in forum Paid Java Projects
    Replies: 9
    Last Post: November 7th, 2013, 04:37 PM
  2. Drawing to a Java BufferedImage's graphics producing inaccurate results.
    By tmetcalfe in forum What's Wrong With My Code?
    Replies: 0
    Last Post: July 19th, 2012, 06:54 PM
  3. Array code not producing, thoughts?
    By r19ecua in forum What's Wrong With My Code?
    Replies: 3
    Last Post: May 6th, 2011, 05:37 PM

Tags for this Thread