Results 1 to 3 of 3
  1. #1
    OmegaZero69 is offline Member
    Join Date
    Mar 2013
    Posts
    12
    Rep Power
    0

    Default trouble with name count and array name

    I am having trouble with the array name it after everything I try to run the program and it gives me an exception java.lang.Null pointer and says its on Line 20.
    Am i missing something or is something written wrong somewhere.


    Java Code:
    import java.util.*;
    import java.io.*;
    
    public class WorldChampionWinner {
    
    	/**
    	 * @throws FileNotFoundException 
    	 * 2
    	 */
    	
    	public static void main(String[] args) throws FileNotFoundException {
    	String[]worldSeries = null ;
    	int count = 0;
    	int index =0;
    	String team;
    	Scanner keyboard = new Scanner (System.in);
    	File file = new File ("WorldSeriesWinners.txt");
    	Scanner inputFile = new Scanner (file);
    	
    	while (inputFile.hasNext()&&index<worldSeries.length){    //has the exception right here 
    		worldSeries[index]= inputFile.nextLine();
    		index++;
    	}
    	inputFile.close();
    	
    	System.out.println("Please input a team.");
    	team = keyboard.nextLine();
    	for (int i = 0;i <worldSeries.length;i++){
    		if (worldSeries.equals(team)) {
                count++;
    		}
    	}
    	if (worldSeries.equals(team)&&count ==1){
    		System.out.println("The team " + team+ " won " + count + " time.");
    	}
    	else if (worldSeries.equals(team)&&count >1){
    		System.out.println("The team " + team+ " won " + count + " times.");
    	}
    	else{
    		System.out.println("You have entered an incorrect team, please try again...");
    	}
    	
    	}
    
    }

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,193
    Rep Power
    19

    Default Re: trouble with name count and array name

    Learn how to use arrays, and note that they need to be initialized: Arrays (The Java™ Tutorials > Learning the Java Language > Language Basics)

    Also, learn how to format code for readability: Code Conventions for the Java Programming Language: Contents

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,508
    Rep Power
    5

    Default Re: trouble with name count and array name

    Actually, more to the point -- Allocated.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. Count occurances in array
    By rachem in forum New To Java
    Replies: 2
    Last Post: 11-08-2012, 05:16 PM
  2. Example Count From Array Snippet
    By Vinx in forum Reviews / Advertising
    Replies: 1
    Last Post: 05-18-2012, 05:22 AM
  3. Duplicate count in array
    By louboulos in forum New To Java
    Replies: 2
    Last Post: 05-10-2012, 05:37 PM
  4. How to count records WITHOUT an array?
    By teekei in forum New To Java
    Replies: 17
    Last Post: 07-25-2011, 12:42 PM
  5. i can do count, but having problem with array
    By judy318 in forum New To Java
    Replies: 4
    Last Post: 10-29-2009, 09:23 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •