我试图将项目设置为
@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.NullPointerException
引起的错误
在setItems
的字符串上。我有几个表在填充时给出相同的错误。所以我无法弄清楚出了什么问题。也许出错的原因是我试图用对象填充一些列?
UPD: 这是价格服务初始化:
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);
}
}
答案 0 :(得分:1)
您的serviceMap不包含PriceService.class,因此ServiceProvider.getService(PricesService.class)
将返回null
。然后使用此null对象将导致NullPointerException
。