ciao!
sostanzialmente ho questo problema con spring boot, anche sto usando vaadin:
codice:
There was an exception while trying to navigate to '' with the root cause 'java.lang.NullPointerException: Cannot invoke "org.vaadin.example.services.PersonaService.getAll()" because "this.personaService" is null'
ho aggiungo nell'application proberties la connessione.
ho installato queste due dipendenze:
codice:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
questo il mio service:
codice:
package org.vaadin.example.services;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.vaadin.example.entities.Persona;
import java.util.ArrayList;
import java.util.List;
@Service
public class PersonaService {
@Autowired
private JdbcTemplate jdbcTemplate;
public List<Persona> getAll() {
try {
return jdbcTemplate.query(
"SELECT * FROM persone",
(rs, rowNum) -> new Persona(
rs.getInt("id"),
rs.getString("noma"),
rs.getString("email")
)
);
} catch (Exception e) {
return new ArrayList<>();
}
}
}
e poi nel controller:
codice:
package org.vaadin.example;
import com.vaadin.flow.component.grid.Grid;
import com.vaadin.flow.component.grid.GridVariant;
import com.vaadin.flow.component.grid.dataview.GridListDataView;
import com.vaadin.flow.component.html.Div;
import com.vaadin.flow.component.orderedlayout.FlexComponent.Alignment;
import com.vaadin.flow.component.html.Span;
import com.vaadin.flow.component.orderedlayout.HorizontalLayout;
import com.vaadin.flow.data.renderer.ComponentRenderer;
import com.vaadin.flow.router.Route;
import org.springframework.beans.factory.annotation.Autowired;
import org.vaadin.example.entities.Persona;
import org.vaadin.example.services.PersonaService;
@Route
public class MainView extends Div {
private Grid<Persona> grid;
@Autowired
private PersonaService personaService;
public MainView() {
createGrid();
add(grid);
}
private void createGrid() {
grid = new Grid<>();
grid.addThemeVariants(GridVariant.LUMO_NO_BORDER, GridVariant.LUMO_COLUMN_BORDERS);
grid.setHeight("100%");
grid.setItems(personaService.getAll());
..............
}
}
ma non riesco a capire il perchè di quell'errore!