Nullpointerexception when generating graph

Asked

Viewed 55 times

-1

I’m creating a graph that pulls data from the database, but at the time of generating the graph gives the following error:

Exception in thread "AWT-Eventqueue-0" java.lang.Nullpointerexception

The first line you accuse in error is within the method createDataset (is the first line inside the "of". That is the method:

private XYDataset createDataset(){
    XYSeries series = new XYSeries("Teste");
    conex.conectar();

    try{
    do{
        double rcv = conex.rs.getDouble("rcv_arena_verde"); 
        double rth = conex.rs.getDouble("rth_arena_verde"); 
    series.add(rcv, rth);

    } while (conex.rs.next());
    }catch (SQLException ex){ 
        JOptionPane.showMessageDialog(null, "Erro ao preencher grafico \n " +ex.getMessage());
    }

    XYSeriesCollection dataset = new XYSeriesCollection();
    dataset.addSeries(series); 
    return dataset;
} 

The methods/parameters of connection with the bank are correct, after all I use them in other classes in the same project and I have no problems. Am I using/calling them incorrectly? I have tried several things and the error persists.

EDIT

Other parts of the class for a better understanding of it:

public class GraficosArenaProceso extends javax.swing.JFrame {

ConexaoBanco conex = new ConexaoBanco();

ModeloBeansPruebasArenaEnVerde mod = new ModeloBeansPruebasArenaEnVerde();
DaoPruebasArenaEnVerde control = new DaoPruebasArenaEnVerde();


 public GraficosArenaProceso() {
    initComponents();
    initUI();
}

   private void initUI(){
    XYDataset dataset = createDataset();
    JFreeChart chart = createChart(dataset);

    ChartPanel chartPanel = new ChartPanel(chart);

    // [...] Aspectos visuais do gráfico           

}

Main method:

    public static void main(String args[]) {

    SwingUtilities.invokeLater(() -> {
        GraficosArenaProceso graficoProceso = new GraficosArenaProceso();
        graficoProceso.setVisible(true);
    } );
  • Note that conex is not null, but conex.rs is. This object may not have been created correctly.

  • Actually Fabius, the "conex.rs" was not initialized. How should I do this? I understood the concept behind having to start the conex.rs, after all it is really null, but I lost myself in the correct way to initialize it.

1 answer

0

As placed by @Fábio conex.rs is null. See the method that initializes the object rs. In the same private Xydataset createDataset() class, see if at any time the conex.rs is actually initialized.

For clearer verification, compare with other methods from the beginning to the connecter call.rs.getDouble("rcv_arena_green");

If you want more help, please share the whole class.

  • It really went unnoticed and the "conex.rs" was not initialized. But how should I do it? I confess that I got lost there. I have an "executaSQL" method (it is in the connection class with the database and executes the query), would it have any relation? Anyway I edited the main post with other parts of the class and I keep trying here. Abs!

  • Did you manage to initialize? What was the problem?

  • I managed, in the createDataset method, under "conex.connect()", I put my method that executes queries conex.executaSQL(select...) and then it worked.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.