Hello,
Please forgive me as I think there is quite a lot I don’t yet understand, so any pointer would be helpful. I am trying to write a data provider for a Grid that loads data lazily from a Spring data table. My classes (shortened) are:
@IdClass(MenuKey.class)
@Entity
@Table(name="MYMENUP")
public class Menu {
@Id
@Column(name = "MENU", nullable = false)
private String name;
@Id
@Column(name = "SEQN", nullable = false)
private int sequence;
@Column(name = "MNTX", nullable = false)
private String description;
@Column(name = "OPTP", nullable = false)
private String type;
@Column(name = "OPVN", nullable = false)
private String program;
//--------------------------------------------------------------------------
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
// etc.
}
There is a MenuKey class as well.
public interface MenuRepository extends PagingAndSortingRepository<Menu, MenuKey> {
List<Menu> findByNameOrderBySequence(String name);
}
@Service
public class MenuService {
private final MenuRepository menuRepository;
//--------------------------------------------------------------------------
@Autowired
public MenuService(MenuRepository menuRepository) {
this.menuRepository = menuRepository;
}
//--------------------------------------------------------------------------
public int count() {
long cc = menuRepository.count();
int count = (int)cc;
return(count);
}
//--------------------------------------------------------------------------
public Stream<Menu> getPage(int offset, int limit) {
Pageable pageable = PageRequest.of(offset, limit);
Page<Menu> page = menuRepository.findAll(pageable);
Stream<Menu> stm = page.stream();
return(stm);
}
}
And finally the data provider:
public class MenuDataProvider<F> extends AbstractDataProvider<Menu, F> {
private static final long serialVersionUID = 0L;
@Autowired
private MenuService menuService;
//--------------------------------------------------------------------------
public MenuDataProvider() {
}
//--------------------------------------------------------------------------
@Override
public Stream<Menu> fetch(Query<Menu, F> query) {
int limit = query.getLimit();
int offset = query.getOffset();
Stream<Menu> stm = menuService.getPage(offset, limit);
return (stm);
}
//--------------------------------------------------------------------------
@Override
public boolean isInMemory() {
return false;
}
//--------------------------------------------------------------------------
@Override
public int size(Query<Menu, F> query) {
int count = menuService.count(); // NPE here
return (count);
}
}
What happens when I run it is that I get a null pointer exception on the line just above:
int count = menuService.count();
because menuService is null. I am trying to set/use the data provider with:
grid.setDataProvider(new MenuDataProvider<>());
I’m sure it is obvious why it doesn’t work, but I can’t work out how I should fix it, or rather, what is the problem with the pattern I am trying to use. I hope that makes sense.
Thanks, Mark.