0

I'm trying to set items to the

@FXML
    private TableView<Price> pricesTable;

where the following columns defined:

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

using such code:

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

where pricesService is the following:

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

    }

}

On compilation I'm constantly getting an error caused by: java.lang.NullPointerException on string with setItems. I have several tables that gives the same error on their filling. So I cant figure out what is wrong. Maybe the reason of the error is that I'm trying to fill some columns with objects?

UPD: here is prices service init:

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

serviceProvider is the following:

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

}
4
  • 1
    How are you initiating the pricesService? Commented May 11, 2015 at 8:05
  • @UlukBiy Updated question, added pricesService init and service provider code Commented May 11, 2015 at 8:11
  • 1
    Your serviceMap does not contain PricesService.class so, ServiceProvider.getService(PricesService.class) will return null. Then if you try to use this null object you will get NPE. Commented May 11, 2015 at 8:37
  • @UlukBiy oh, how could I've miss this. thnks Commented May 11, 2015 at 8:41

1 Answer 1

1

Your serviceMap does not contain PricesService.class so, ServiceProvider.getService(PricesService.class) will return null. Then using this null object will result to NullPointerException.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.