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);
}
}