NullPointer Exception during registration using the MVC standard

0

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

While registering for a publisher, I'm getting the error NullPointerException .

Using the debug in Eclipse, I came to the conclusion that by clicking the register button and triggering the event, the data contained in textfield is not being passed to the façade class and other classes, but I could not find the which 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;
}

Facade 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 (Controller) Class:

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

}
}

Class Editora (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;
   }
   }

EditoraDAO 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
}
}
    
asked by anonymous 25.10.2015 / 02:22

1 answer

2

Change this to resolve:

private Editora editora = new Fachada();

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

Depending on the case, you may have better solutions.

If it does not resolve, you should have other issues that you can not tell only with the information you have placed.

    
25.10.2015 / 02:56