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

标签 java javafx nullpointerexception

我正在尝试将项目设置为

@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/

相关文章:

java - 在这种情况下我怎样才能最好地应用Hibernate-Search?

JAVA:文件输入流和文件输出流

android - 应用程序因自定义 ListView 中的 NullPointerException 而崩溃

java - 找不到 NullPointerException

java - 线程 "main"java.lang.NullPointerException 中的异常 - 程序包

java - 带 Selenium 的 Chrome 驱动程序 : "no suitable constructor found for RemoteWebDriver"

java - Tomcat - 上下文初始化失败

java - 在 html 中嵌入 Javafx

css - 使用自定义 CSS 时,JavaFX 表上的滚动条会留下空白

java - 如何指定希望工具栏中的元素居中?