problem with java.lang.Nullpointerexception

Asked

Viewed 232 times

0

Well, I’m trying to popular a Jtable with database data, but I have always found the same java.lang.Nullpointerexception Exception.

It should work as follows:

user clicks the update button and the program pulls the information from DB, then allocates it in a table.

Note: I already managed to use this code in another program and it worked without errors.

Exception in thread "AWT-Eventqueue-0" java.lang.Nullpointerexception AT dao.documentDAO.Selectfull(documentDAO.java:39) At view.cadastro_documento$3.actionPerformed(cadastro_documento.java:115)

these are the classes I created

cadastral

package View;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import DAO.UsuarioDAO;
import DAO.documentoDAO;
import Model.Usuario;
import Model.documento;

import javax.swing.JButton;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import java.awt.event.ActionListener;
import java.util.List;
import java.awt.event.ActionEvent;

public class cadastro_documento extends JFrame {

    private JPanel contentPane;
    private JTextField textField;
    private JTable table;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    cadastro_documento frame = new cadastro_documento();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public cadastro_documento() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);

        JMenuBar menuBar = new JMenuBar();
        setJMenuBar(menuBar);

        JMenu mnArquivos = new JMenu("Arquivos");
        menuBar.add(mnArquivos);

        JMenuItem mntmSair = new JMenuItem("Sair");
        mntmSair.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                menu menu = new menu();
                menu.setVisible(true);
                dispose();
            }
        });
        mnArquivos.add(mntmSair);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        JLabel lblTipoDocumento = new JLabel("Tipo Documento");
        lblTipoDocumento.setBounds(10, 11, 101, 14);
        contentPane.add(lblTipoDocumento);

        textField = new JTextField();
        textField.setBounds(121, 8, 149, 20);
        contentPane.add(textField);
        textField.setColumns(10);

        JLabel lblJCadastrados = new JLabel("J\u00E1 Cadastrados");
        lblJCadastrados.setBounds(10, 72, 94, 14);
        contentPane.add(lblJCadastrados);

        JPanel panel = new JPanel();
        panel.setBounds(10, 92, 414, 158);
        contentPane.add(panel);
        panel.setLayout(null);

        table = new JTable();
        table.setModel(new DefaultTableModel(
            new Object[][] {},
            new String[] {
                "ID Documento", "Descri\u00E7\u00E3o"
            }
        ));
        table.setBounds(10, 141, 394, -129);
        panel.add(table);

        JButton btnCadastrar = new JButton("Cadastrar");
        btnCadastrar.setBounds(10, 38, 105, 23);
        contentPane.add(btnCadastrar);

        JButton btnAtualizar = new JButton("Atualizar");
        btnAtualizar.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {

                documentoDAO u = new documentoDAO();
                String[] colunas = { "idTipoDoc", "Tipo"};
                DefaultTableModel dados = new DefaultTableModel(colunas, 2);
                List<documento> documentos = u.SelectFull();

                for (documento documento : documentos) {
                    dados.addRow(new String[] { "" + documento.getIdTipoDoc(), documento.getTipo()});
                }

                table.setModel(dados);

            }
        });
        btnAtualizar.setBounds(163, 39, 101, 23);
        contentPane.add(btnAtualizar);
    }
}

document

package Model;

public class documento {

    private  int idTipoDoc;
    private  String Tipo;

    public  int getIdTipoDoc() {
        return idTipoDoc;
    }

    public void setIdTipoDoc(int idTipoDoc) {
        this.idTipoDoc = idTipoDoc;
    }

    public  String getTipo() {
        return Tipo;
    }

    public void setTipo(String tipo) {
        Tipo = tipo;
    }

    public void add(documento doc) {
        // TODO Auto-generated method stub

    }

}

documentDAO

package DAO;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import Model.documento;
import Model.Usuario;

public class documentoDAO {

    public boolean acesso;
    private Connection con = null;

    public void documentoDAO() {
        this.con = new conexaoDAO().getConexao();
    }



    public void Inserir(documento doc) {
        String sql = "INSERT INTO tipodoc(idTipoDoc, Tipo)VALUES (null,?)";
        try {
            PreparedStatement stmt = con.prepareStatement(sql);
            stmt.setString(1, doc.getTipo());
            stmt.execute();
            stmt.close();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public List<documento> SelectFull() {
        String sql = "SELECT * FROM tipodoc";
        try {
            List<documento> documentos = new ArrayList<documento>();
            PreparedStatement stmt = con.prepareStatement(sql);
            ResultSet result = stmt.executeQuery();
            while (result.next()) {
                documento doc = new documento();
                doc.setIdTipoDoc(result.getInt("idTipoDoc"));
                doc.setTipo(result.getString("Tipo"));
                doc.add(doc);
            }
            result.close();
            stmt.close();
            return documentos;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

}

2 answers

1

The mistake is in:

public void documentoDAO() {
    this.con = new conexaoDAO().getConexao();
}

You created a method, not a builder, do:

public documentoDAO() {
    this.con = new conexaoDAO().getConexao();
}

Java constructors must have the same class name and must not have the declared return type

0


In the class documentoDAO in the method SelectFull you use the reference con on the line Preparedstatement stmt = con.prepareStatement(sql); But this con is only filled when you call the method public void documentoDAO() of the same class.

In class cadastro_documento you call the method SelectFull without calling the documentoDAO()before, soon the con will be null and this error will happen;

Before that line List<documento> documentos = u.SelectFull(); place the call from the other method, something like u.documentoDAO(); List<documento> documentos = u.SelectFull();

Maybe your idea is that this guy was called whenever the object had been created "builder" in this case you have to take the void method so that it is called by java when you use the new.

Browser other questions tagged

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