Всем привет, как в следующем примере исправить NullPointerException?
// Exception in thread "main" java.lang.NullPointerException
route = mapFrom.get(route.getTo()).get(0);
package main.java;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;
enum Properties {
FROM, TO,
}
public class Solution {
public Map<String, List<Ticket>> fromEntries(List<Ticket> tickets, Properties prop) {
/*
return tickets.stream()
.collect(Collectors.groupingBy(Ticket::getFrom, Collectors.mapping(Function.identity(), Collectors.toList())));
*/
Function<Ticket, String> method = null;
if (prop == Properties.FROM) {
method = Ticket::getFrom;
} else if (prop == Properties.TO) {
method = Ticket::getTo;
}
return tickets.stream()
.collect(Collectors.groupingBy(Objects.requireNonNull(method),
Collectors.mapping(Function.identity(), Collectors.toList())));
}
public List<Ticket> sortingTickets(List<Ticket> tickets) {
Map<String, List<Ticket>> mapFrom = fromEntries(tickets, Properties.FROM);
System.out.println("mapFrom " + mapFrom);
Map<String, List<Ticket>> mapTo = fromEntries(tickets, Properties.TO);
System.out.println("mapTo " + mapTo);
List<Ticket> sorted = new ArrayList<>();
List<Ticket> routeList = tickets.stream().filter(element -> !mapTo.containsKey(element.getFrom())).collect(Collectors.toList());
// System.out.println("route " + route); // [Ticket{from='Череповец', to='Москва'}]
Ticket route = routeList.get(0);
System.out.println(route); // Ticket{from='Череповец', to='Москва'}
while (route != null) {
System.out.println("route " + route);
/*
route Ticket{from='Череповец', to='Москва'}
route Ticket{from='Москва', to='С.Петербург'}
route Ticket{from='С.Петербург', to='Минск'}
route Ticket{from='Минск', to='Киев'}
route Ticket{from='Киев', to='Новосибирск'}
*/
sorted.add(route);
// Exception in thread "main" java.lang.NullPointerException
route = mapFrom.get(route.getTo()).get(0);
}
System.out.println("sorted " + sorted);
// return sorted;
return tickets;
}
}
class Ticket {
private String from;
private String to;
public Ticket(String from, String to) {
this.from = from;
this.to = to;
}
public String getFrom() {
return from;
}
public void setFrom(String from) {
this.from = from;
}
public String getTo() {
return to;
}
public void setTo(String to) {
this.to = to;
}
@Override
public String toString() {
return "Ticket{" +
"from='" + from + '\'' +
", to='" + to + '\'' +
'}';
}
}
class Main {
public static void main(String[] args) {
List<Ticket> tickets = new ArrayList<>();
Ticket ticket1 = new Ticket("С.Петербург", "Минск");
Ticket ticket2 = new Ticket("Киев", "Новосибирск");
Ticket ticket3 = new Ticket("Череповец", "Москва");
Ticket ticket4 = new Ticket("Минск", "Киев");
Ticket ticket5 = new Ticket("Москва", "С.Петербург");
tickets.add(ticket1);
tickets.add(ticket2);
tickets.add(ticket3);
tickets.add(ticket4);
tickets.add(ticket5);
System.out.println(new Solution().sortingTickets(tickets));
}
}