有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

读取事件时,java位置始终为空

所以我正在开发一个票务系统,但由于某种原因,我似乎无法在我的活动中找到一个位置

TicketingSystem类有一些哈希图,我可以在其中添加数据。 它还有reader方法,我遇到的问题是readEvents()

public TicketSystem() {
    queueService = new QueueService();
    users = new HashMap<>();
    locations = new HashMap<>();
    events = new HashMap<>();
    readData();
}

public void addLocation(Venue location) {
    locations.put(location.getId(), location);
}

public void addEvent(Event event) {
    events.put(event.getId(), event);
}

public static Event getEvent(String eventId) {
    return events.get(eventId);
}

public static Venue getLocation(String locationId) {
    return locations.get(locationId);
}

public void readLocations() {
    try (BufferedReader reader = new BufferedReader(new FileReader("venuedata.txt"))) {
        String line = "";
        VenueMapper mapper = new VenueMapper();
        while ((line = reader.readLine()) != null) {
            Venue venue = mapper.map(line.split(";"));
            locations.put(venue.getId(), venue);
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

public void readEvents() {
    try (BufferedReader reader = new BufferedReader(new FileReader("eventdata.txt"))) {
        String line = "";
        EventMapper mapper = new EventMapper();
        while ((line = reader.readLine()) != null) {
            Event event = mapper.map(line.split(";"));
            events.put(event.getId(), event);
            reader.readLine();
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

public void readData() {
    readEvents();
    readLocations();
}

使用EventMapper类,该类通过TicketingSystem类从文本文件中读取数据,并按“;”拆分数据[5]是我的位置ID

@Override
public Event map(String[] data) {
    String dateAndTime = data[1];
    StringBuilder date = new StringBuilder(dateAndTime.substring(0, 8));
    StringBuilder time = new StringBuilder(dateAndTime.substring(8));

    return new Event(data[0], data[2], date.toString(), time.toString(), data[3], Double.parseDouble(data[4]), data[5]);
}

}

这是我的事件类,它包含我正在使用的构造函数,该构造函数在EventMapper类中返回事件

public Event(String id, String name, String date, String time, String description,
        double price, String locationId) {
    this(name, LocalDate.parse(date, DateTimeFormatter.ofPattern("ddMMyyyy")),
            LocalTime.parse(time, DateTimeFormatter.ofPattern("HHmm")), description, price);    
    this.id = id;
    *setLocation(TicketSystem.getLocation(locationId));*
}

public void setLocation(Venue location) {
    this.location = location;
}

每次我调试时,位置列表中都会有数据,但由于某些原因,我的事件没有拾取我试图获取的位置


共 (0) 个答案