-2

I have 2 lists-

[(Tandoor, Indian), (Biryani, Indian), (Pizza, Italian)]

{ in the above, left item is a food item of cuisine type on right}

and

[(John, Indian), (Mary, Indian), (Sam, Italian)]

{ in the above, left person has choice of cuisine type on right}

I need to print out the contents of the lists as John Tandoor John Biryani Mary Tandoor Mary Biryani Sam Pizza

The APIs for initializing the 2 given lists, printing the output and handling Pair objects (comparing, accessing key and value) are given.

Why am I getting nullpointerexception in the following -

package a1;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;
import java.util.Scanner;

public class main {
    static class Pair implements Comparable<Pair>{
        String pairKey;
        String pairValue;

        Pair(String pairKey, String pairValue) {
            this.pairKey = pairKey;
            this.pairValue = pairValue;
        }

        public String getPairKey() {
            return pairKey;
        }

        public String getPairValue() {
            return pairValue;
        }

        public String toString() {
            return getPairKey() + " " + getPairValue();
        }

        @Override
        public int compareTo(Pair pair) {
            int compare = pairKey.compareTo(pair.getPairKey());
            if(compare == 0) {
                compare = pairValue.compareTo(pair.getPairValue());
            }
            return compare;
        }
    }
    static List<Pair> solve(List<Pair> lunchMenuPairs, List<Pair> teamCuisinePreferencePairs){
        List<Pair> result= null;
        for(Pair first : teamCuisinePreferencePairs){
            for(Pair name : lunchMenuPairs)
            {
                String a1 = first.getPairValue();
                String a2 = name.getPairValue();
                String a3 = first.getPairKey();
                String a4 = name.getPairKey();
                if(first.compareTo(name)==0)
                {
                    System.out.println(a3);
                    System.out.println(a4);
                    Pair pair = new Pair(a3,a4);
                    result.add(pair);
                }
            }
        }
        return result;
    }

    private static void readAndSetParameters(List<Pair> lunchMenuPairs, List<Pair> teamCuisinePreferencePairs) {
        int lunchMenuPairCount = -1;
        int teamCuisinePreferencePairCount = -1;

        String tempOption = null, tempOptionValue = null;;
        try(Scanner in = new Scanner(System.in)) {
            lunchMenuPairCount = in.nextInt();
            while(lunchMenuPairCount > 0) {
                lunchMenuPairCount--;

                tempOption = in.next();
                tempOptionValue = in.next();
                Pair pair = new Pair(tempOption, tempOptionValue);
                lunchMenuPairs.add(pair);
            }

            teamCuisinePreferencePairCount = in.nextInt();
            while(teamCuisinePreferencePairCount > 0) {
                teamCuisinePreferencePairCount--;

                tempOption = in.next();
                tempOptionValue = in.next();
                Pair pair = new Pair(tempOption, tempOptionValue);
                teamCuisinePreferencePairs.add(pair);
            }
        }
    }

    private static void print(List<?> resultPairs) {
        for (Object pair : resultPairs) {
            System.out.println(pair);
        }
    }

    public static void main(String[] args){
        List<Pair> lunchMenuPairs = new LinkedList<>();
        List<Pair> teamCuisinePreferencePairs = new LinkedList<>();
        readAndSetParameters(lunchMenuPairs, teamCuisinePreferencePairs);
        List<Pair> result = solve( lunchMenuPairs, teamCuisinePreferencePairs );
        Collections.sort(result);
        print(result);
    }
}
Mrinmay
  • 41
  • 7
  • 1
    What line causes the NPE? Use a debugger and set a break point to figure out the problem. – Code-Apprentice Jun 17 '17 at 06:17
  • Don't know. *Where* are you getting NullPointerException? – Andreas Jun 17 '17 at 06:17
  • 3
    [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/q/218384/5221149) – Andreas Jun 17 '17 at 06:19
  • 2
    [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173/5221149) – Andreas Jun 17 '17 at 06:19
  • 1
    [How to create a Minimal, **Complete**, and Verifiable example](https://stackoverflow.com/help/mcve). E.g. this means to change the code to run with specific values that cause the problem, rather than asking user for input, especially since you haven't shown how the input should be provided, in to cause the problem. – Andreas Jun 17 '17 at 06:20

1 Answers1

1

Change

static List<Pair> solve(List<Pair> lunchMenuPairs, List<Pair> teamCuisinePreferencePairs){
    List<Pair> result= null;
    ...

to

static List<Pair> solve(List<Pair> lunchMenuPairs, List<Pair> teamCuisinePreferencePairs){
    List<Pair> result= new ArrayList<>();
    ...
  • Console-3 John Indian Mary Indian Sam Italian 3 Tandoor Indian Biryani Indian Pizza Italian Exception in thread "main" java.lang.NullPointerException at java.util.Collections.sort(Collections.java:141) at a1.main.main(main.java:99) – Mrinmay Jun 17 '17 at 14:15