填充空指针异常 java fx table

null pointer exception on filling java fx table

我正在尝试将项目设置为

@FXML
    private TableView<Price> pricesTable;

其中定义了以下列:

 @FXML
    private TableColumn<Price, Long> priceId;
    @FXML
    private TableColumn<Price, Good> priceGood;
    @FXML
    private TableColumn<Price, MeasurementUnit> priceMeasurement_unit;
    @FXML
    private TableColumn<Price, TypeOfPrice> type_of_price;
    @FXML
    private TableColumn<Price, Long> price;

使用这样的代码:

private void updatePrices() throws SQLException{
        pricesPane.setCenter(pricesTable);
        pricesTable.setItems(FXCollections.observableArrayList(pricesService.getPrices()));
        pricesTable.getSortOrder().add(priceId);
    }

其中 pricesService 如下:

public class PricesServiceImpl implements PricesService {
    private final Dao<Price, Long> pricesDao = DaoFactory.getDao(Price.class);
    private Logger log = LogManager.getLogger(PricesServiceImpl.class);

    @Override
    public List<Price> getPrices() throws SQLException {
        try {
            List<Price> prices = pricesDao.queryForAll();
            return prices;
        } catch (SQLException e) {
            log.log(Level.DEBUG, e.getMessage());
            e.printStackTrace();
        }
        return Collections.emptyList();
    }
    @Override
    public void removePrice(Price price){
        try{
            pricesDao.delete(price);
        }catch (SQLException e){
            log.log(Level.ERROR, e.getMessage());
        }
    }
    @Override
    public void savePrice(Long id, Good good, MeasurementUnit measurementUnit, TypeOfPrice typeOfPrice, Long priceValue){
        Price price = new Price();
        price.setId(id);
        price.setGood(good);
        price.setMeasurementUnit(measurementUnit);
        price.setPrice(priceValue);
        price.setTypeOfPrice(typeOfPrice);
        try {
            pricesDao.create(price);
        }catch (SQLException e){
            log.log(Level.ERROR, e.getMessage());
        }

    }

}

在编译时,我不断收到由以下原因引起的错误:java.lang.NullPointerExceptionsetItems 的字符串上。我有几个表格在填写时给出了相同的错误。所以我不知道出了什么问题。也许错误的原因是我试图用对象填充某些列?

更新: 这是价格服务初始化:

private PricesService pricesService = ServiceProvider.getService(PricesService.class);

serviceProvider 如下:

public class ServiceProvider {
    private ServiceProvider() {}
    private static Map<Class<? extends Service>, Service> serviceMap = new HashMap<>();
    static {
        serviceMap.put(AgentsService.class, new AgentsServiceImpl());
        serviceMap.put(GoodsService.class, new GoodsServiceImpl());
    }
    @SuppressWarnings("unchecked")
    public static <Type> Type getService(Class<Type> type) {
        return (Type) serviceMap.get(type);
    }

}

您的 serviceMap 不包含 PricesService.class,因此,ServiceProvider.getService(PricesService.class) 将 return null。然后使用此空对象将导致 NullPointerException.