D
D
dc65k2021-11-23 15:03:20
Java
dc65k, 2021-11-23 15:03:20

How to fix NullPointerException?

Hello everyone, how can I fix the NullPointerException in the following example?

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

Answer the question

In order to leave comments, you need to log in

2 answer(s)
U
User700, 2021-11-23
@User700

This means that at some stage mapFrom does not contain an element with the route.getTo() key. And .get(0) cannot be taken from null. Try to go instead of Map'a List'ov to multimap, when one key can have several values ​​respectively. Or check and exit the loop when mapFrom.getTo returns null.

J
Jacen11, 2021-11-23
@Jacen11

Exception in thread "main" java.lang.NullPointerException
write boring chains like if((route.getTo()!=null && mapFrom.get(route.getTo())!=
null
) and it says exactly where it takes off

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question