0

This is a simple program using Hash maps where it utilizes a ranking system for names for baby boys and girls from a file with name ex: "babynameranking2008.txt". I am receiving a null pointer exception in my switch case for my variable ranking,

The error is only occurring in the variable for each switch case for boy and girl in for example: "ranking=boys[index].get(name);". If I make ranking equal to 0 it removes the exception.

public class Lab08 {
    @SuppressWarnings("unchecked")
    private static Map<String, Integer>[] boys = new HashMap[10];
    @SuppressWarnings("unchecked")
    private static Map<String, Integer>[] girls = new HashMap[10];

    public static void main(String[] args) throws IOException {
        Scanner input = new Scanner(System.in);
        String answer;
        readNames(); // method to read the info from the files and add to our Map array                     
        // Get user input and continue until done

        do {
            System.out.print("ENter a year (2008-2017): ");
            int year = input.nextInt();input.nextLine();
            System.out.print("Boy or girl? ");
            String sex = input.nextLine().toLowerCase();
            System.out.print("ENter a name: ");
            String name = input.nextLine();

            sex = sex.toLowerCase();
            int index = 0;
            int ranking = 0;
            switch (sex) {
                // display the output based on the sex (boy or girl)
                case "boy":
                   index = year - 2008;
                   ranking = boys[index].get(name);
                   System.out.printf("The Boy named %s is ranked %d in %d \n",name,ranking,year);                  
                   break;

                case "girl":
                   index=year-2008;
                   ranking=girls[index].get(name);
                   System.out.printf("The Boy named %s is ranked %d in %d \n",name,ranking,year);
                   break;

                default:
                   System.out.println("sex should be boy or girl");
                   break;
            }
            //input.close();
            System.out.print("Do you want check another name (yes or no)?");
            answer = input.nextLine().toLowerCase();
        } while (answer.equals("yes"));
    } // end main

    //read information from each file and add to appropriate Map array
    public static void readNames() throws IOException {
        String fixedName="babynameranking";
        int year=2008;
        String type=".txt";

        File infile;
        for(int i=0;i<10;++i) {
            boys[i] = new HashMap<>();
            girls[i] = new HashMap<>();             
        }           

            for(int i=0;i<=9;++i) {
              // construct the file name
            String filename = fixedName+Integer.toString(year)+type;// construct the file name ;
            infile = new File(filename);

            Scanner in = new Scanner(infile);
            while(in.hasNext()) {
                // read info from the file and add to Map arrays
                int ranking=in.nextInt();
                   String boyName=in.next();
                   in.next();

                   String girlName=in.next();
                   in.next();

                 //put in map
                   boys[i].put(boyName, ranking);
                   girls[i].put(girlName, ranking);

            } // end while
            in.close();

            year=year+1;

        } // end for loop
    } // end readNames()

} // end Lab08

Amaury
  • 1
  • 2
  • a) What are you trying to do `new HashMap[10];` b) `String sex = input.nextLine().toLowerCase(); sex=sex.toLowerCase();` - why? – Scary Wombat Apr 26 '19 at 05:51
  • Please provide the code of the method which builds your `Map`s - `readNames`. PS - I would suggest using `Map>` instead of `Map[]` – Rann Lifshitz Apr 26 '19 at 05:52
  • The issue is happening because you have declared `ranking` as `int` and when you have the `HashMap` return an `Integer(wrapper of int)`. So when you call `boys[index].get(name)` and if the return value of `get(name)` is `null`, it try's to convert `null` to `int` which it cannot resulting in nullpointerexception Declare `ranking` as `Integer` and the issue should be resolved – Ashishkumar Singh Apr 26 '19 at 05:57
  • I have added my readNames method for anyone that was asking – Amaury Apr 26 '19 at 06:01
  • @Amaury : Since you are accessing your `Map`s with provided inputs, you will occasionally encounter inputs which are not mapped. Replace using the `get` method on your `Map`s with `getOrDefault`. The default value should obviously represent a non-ranked name. No more null values to avoid :) – Rann Lifshitz Apr 26 '19 at 06:05

1 Answers1

-1

The problem is you did not include the path to your files. Thus, throws FileNotFoundException .

Insert the path to your files here:

 String filename = fixedName + Integer.toString(year) + type;

Change it to Something like this:

String filename="/Users/../Desktop/babynameranking.txt";

And also you do not need these variable:

String fixedName="babynameranking";
int year=2008;
String type=".txt";
Liam Wilson
  • 127
  • 7