gpt4 book ai didi

java - 填充 java fx 表时出现空指针异常

转载 作者:行者123 更新时间:2023-12-01 11:34:14 25 4
gpt4 key购买 nike

我正在尝试将项目设置为

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

}

最佳答案

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

关于java - 填充 java fx 表时出现空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30162413/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com