Exception in thread "main" java.lang.Nullpointerexception - CRUD Java Mysql

Asked

Viewed 102 times

0

I’m trying to make a CRUD, when I come across this mistake and I don’t know how to fix it.

Error:

Exception in thread "main" java.lang.Nullpointerexception at view.ProdutView.(Productoview.java:29) at main.Run.main(Run.java:8)

is a jFrame, code below:

package view;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JTable;
import java.awt.Color;
import javax.swing.JButton;
import javax.swing.border.LineBorder;

import dao.ProdutoDAO;
import table.ProdutoTableModel;
import javax.swing.table.DefaultTableModel;
import javax.swing.border.BevelBorder;

public class ProdutoView extends javax.swing.JFrame {
    private JTextField tfCodigo;
    private JTextField tfDescricao;
    private JTextField tfPreco;
    private JTextField tfPesquisarDescricao;    
    private JTable tbProduto;

public ProdutoView() {
    setResizable(false);
    setTitle("Produto");
    setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);      
    setBounds(650, 400, 450, 375);
    getContentPane().setLayout(null);
    tbProduto.setModel(new ProdutoTableModel(new ProdutoDAO().listarTodos()));

    JLabel lblNewLabel = new JLabel("C\u00F3digo:");
    lblNewLabel.setBounds(58, 50, 46, 14);
    getContentPane().add(lblNewLabel);

    JLabel lblNewLabel_1 = new JLabel("Descri\u00E7\u00E3o:");
    lblNewLabel_1.setBounds(58, 75, 63, 14);
    getContentPane().add(lblNewLabel_1);

    JLabel lblNewLabel_2 = new JLabel("Pre\u00E7o:");
    lblNewLabel_2.setBounds(58, 100, 46, 14);
    getContentPane().add(lblNewLabel_2);

    tfCodigo = new JTextField();
    tfCodigo.setBounds(147, 47, 198, 20);
    getContentPane().add(tfCodigo);
    tfCodigo.setColumns(10);

    tfDescricao = new JTextField();
    tfDescricao.setBounds(147, 72, 198, 20);
    getContentPane().add(tfDescricao);
    tfDescricao.setColumns(10);

    tfPreco = new JTextField();
    tfPreco.setBounds(147, 97, 198, 20);
    getContentPane().add(tfPreco);
    tfPreco.setColumns(10);

    JButton btLimpar = new JButton("Limpar");
    btLimpar.setBounds(58, 135, 89, 23);
    getContentPane().add(btLimpar);

    JButton btExcluir = new JButton("Excluir");
    btExcluir.setBounds(157, 135, 89, 23);
    getContentPane().add(btExcluir);

    JButton btSalvar = new JButton("Salvar");
    btSalvar.setBounds(256, 135, 89, 23);
    getContentPane().add(btSalvar);

    JLabel lblNewLabel_3 = new JLabel("Pesquisar (Descri\u00E7\u00E3o):");
    lblNewLabel_3.setBounds(58, 258, 188, 14);
    getContentPane().add(lblNewLabel_3);

    tfPesquisarDescricao = new JTextField();
    tfPesquisarDescricao.setBounds(58, 279, 322, 20);
    getContentPane().add(tfPesquisarDescricao);
    tfPesquisarDescricao.setColumns(10);

    tbProduto = new JTable();
    tbProduto.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null));
    tbProduto.setBounds(60, 237, 320, -54);
    getContentPane().add(tbProduto);




    }
}

package main;

import view.ProdutoView;

public class Run {

    public static void main(String[] args) {
        new ProdutoView().setVisible(true);
    }
}

O projeto como um todo

Janela

1 answer

2


This little guy is not instantiated or null.

tbProduto.setModel(new ProdutoTableModel(new ProdutoDAO().listarTodos()));

Install it will work! I hope I’ve helped.

  • Excuse my lack of understanding. But I’m trying to learn on my own and I have no one to turn to and ask my questions. 'tbProduct.setModel(new Productotablemodel(new Productodao().listTodos());' But in this line there is much, what I need to instantiate? setModel is a method and within comes the parameters that are my Productotablemodel classes and within that Product and listAll that is also a method ?

  • Your tbProduct is void or not instantiated. You need to use new to instantiate it, for example. tbProduct = new Jtable(parameters); From a read on this material that will help you: https://www.devmedia.com.br/jtable-utilizando-componente-em-interfaces-graficas-swing/28857 If it helps, mark the answer as correct. Capricha!

  • At the end of the code there was: tbProduct = new Jtable(); Just like you said, so I moved the line that was making an error for after tbProduct had already been instantiated and the code ran smoothly. Thanks for the help, it worked. I’ll check the material you sent so I understand better.

Browser other questions tagged

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