Nullpointer Exception during registration using MVC standard

Asked

Viewed 56 times

0

I have a ready-made application that was created procedural, but for learning purposes I decided to reprogram it using object orientation and the MVC standard.

During the registration of a publisher I’m getting the error NullPointerException.

Using the debug in Eclipse, I came to the conclusion that by clicking on the registration button and trigger the event, the data contained in textfield are not being passed on to the front class and the other classes, but I could not locate what is causing this.

Registration event:

//imports   
import livraria.model.editora.Editora;
import livraria.fachada.Fachada;

//variáveis
private Fachada fachada;
private Editora editora;

//Botão salvar
    private JButton getBtn_salvar() {
    if (btn_salvar == null) {
        btn_salvar = new JButton();
        btn_salvar.setText("SALVAR");
        btn_salvar.setLocation(new Point(15, 150));
        btn_salvar.setSize(new Dimension(100, 50));
        btn_salvar.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                editora = new Editora();
                editora.setCnpj_editora(textfield_cnpj.getText());
                editora.setNome_editora(textfield_nome.getText());
                editora.setEmail_editora(textfield_email.getText());
                editora.setTelefone_editora(textfield_telefone.getText());
                fachada.insert(editora); //De acordo com o Debug, é nessa linha que ocorre a Exception

            }
        });
    }
    return btn_salvar;
}

Front class:

package livraria.fachada;

import livraria.controller.autor.AutorRN;
import livraria.model.autor.Autor;
import livraria.controller.editora.EditoraRN;
import livraria.model.editora.Editora;

public class Fachada {
public static void main(String[] args) {
    // TODO Auto-generated method stub
    }

private AutorRN autorRN;
private EditoraRN editoraRN;

public Fachada() {
    this.autorRN = new AutorRN();
    this.editoraRN = new EditoraRN();
    }

public void insert(Autor autor) {
    this.autorRN.insert(autor);
   }

public void insert(Editora editora) {
    this.editoraRN.insert(editora);
}
}

Editorarn class (Controller):

package livraria.controller.editora;

import javax.swing.JOptionPane;
import livraria.model.editora.Editora;
import livraria.model.editora.EditoraDAO;

public class EditoraRN {
private EditoraDAO dao;

public EditoraRN() {
    this.dao = new EditoraDAO();
}

public void insert(Editora editora) {
    if (editora != null) {
        dao.insert(editora);
        JOptionPane.showMessageDialog(null, "Cadastro efetuado com sucesso!");
    } else {
        JOptionPane.showMessageDialog(null, "Não foi possível cadastrar!");
    }
}

public static void main(String[] args) {
    // TODO Auto-generated method stub

}
}

Publisher Class(Model):

package livraria.model.editora;

public class Editora {

private int cod_editora;
private String cnpj_editora;
private String nome_editora;
private String email_editora;
private String telefone_editora;

public static void main(String[] args) {
    // TODO Auto-generated method stub

   }

public int getCod_editora() {
    return cod_editora;
   }

public void setCod_editora(int cod_editora) {
    this.cod_editora = cod_editora;
   }

public String getCnpj_editora() {
    return cnpj_editora;
   }

public void setCnpj_editora(String cnpj_editora) {
    this.cnpj_editora = cnpj_editora;
   }

public String getNome_editora() {
    return nome_editora;
   }

public void setNome_editora(String nome_editora) {
    this.nome_editora = nome_editora;
   }

public String getEmail_editora() {
    return email_editora;
   }

public void setEmail_editora(String email_editora) {
    this.email_editora = email_editora;
   }

public String getTelefone_editora() {
    return telefone_editora;
   }

public void setTelefone_editora(String telefone_editora) {
    this.telefone_editora = telefone_editora;
   }
   }

Editorial Class (Model):

package livraria.model.editora;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import livraria.conexao.ConexaoMYSQL;

public class EditoraDAO {
private Connection connection = null;
private PreparedStatement stm = null;

public void insert(Editora editora) {
    String sql = "INSERT INTO livraria.editora(CNPJ,nome,email,telefone) VALUES(?,?,?,?);";

    try {
        this.connection = new ConexaoMYSQL().getConnection();
        this.stm = this.connection.prepareStatement(sql);
        this.stm.setString(1, editora.getCnpj_editora());
        this.stm.setString(2, editora.getNome_editora());
        this.stm.setString(3, editora.getEmail_editora());
        this.stm.setString(4, editora.getTelefone_editora());
    } catch (SQLException e) {
        throw new RuntimeException(e);
    } finally {
        try {
            this.connection.close();
        } catch (SQLException e) {
            throw new RuntimeException();
        }
    }

}

public static void main(String[] args) {
    // TODO Auto-generated method stub
}
}
  • 1

    Where is the exception occurring? Be specific.

  • Exception occurs in 'facade.Insert(publisher);'

  • Where you are creating the variable fachada? This is the important part.

  • I edited the question with the missing items

  • Is there a place where you’re initiating it? If you don’t have it anywhere, this is the problem, you need to initialize it. You’d have to have one new Fachada(), at least.

  • Take a look at [tour]. You can accept an answer if it solved your problem. You can vote on every post on the site as well. Did any help you more? You need something to be improved?

Show 1 more comment

1 answer

2

Change this:

private Editora editora = new Fachada();

You need to initialize the object, not just declare it.

You can also choose to do this in the constructor, as was done at other points in the code.

Depending on the case can have better solutions.

If not solve should have other problems that you can not understand only with the information placed.

  • Actually, scan the code again and include the boot directly in the constructor.

Browser other questions tagged

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