java.lang.Nullpointerexception - Having to add a user to the database but points out this exception

Asked

Viewed 68 times

0

Follow the code of the connection to the database, the user class and the code already in jframe:

   /*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package bancodedados;

-----------------------------
Classe do banco de dados:

import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author gustavo-beserra
 */
public class BancoDeDados {

    private static Connection myConn = null;
    private String Driver = "com.mysql.jdbc.driver";
    private final String url = "jdbc:mysql://localhost:3306/";   
    private final String login = "root";
    private final String pass = "root";

    private User user;
    private User password;

    public void conexao(){
        try {
            Class.forName(Driver);
            myConn = DriverManager.getConnection(url, login, pass);
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            try {
                myConn.close();
            } catch (Exception ex) {
                System.out.println("Houve um erro no fechamento da conexão"); 
                ex.printStackTrace();
           }
        }
    }

    public void adcionar(String user, String password){
        String sql = "INSERT INTO loginteste (user, password) values(?, ?)";
        try{
            PreparedStatement ps = myConn.prepareStatement(sql);
            ps.setString(1, user);
            ps.setString(2, password);
            ps.executeUpdate();

            JOptionPane.showMessageDialog(null, "Usuario cadastrado com sucesso!");

        }catch(Exception ex){
            ex.printStackTrace();
        }
    }
}

User class:

package bancodedados;

public class User {

    private String user;
    private String password;

     public String getUser(){
        return user;
    }

    public void setUser(String user){
        this.user = user;
    }

    public String getPassword(){
        return password;
    }

    public void setPassword(String password){
        this.password = password;
    }

}

Code in the jframe:

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    // TODO add your handling code here:

    u1.setUser(jtfUser.getText());
    u1.setPassword(jpPass.getText());

    bd1.adcionar(u1.getUser(), u1.getPassword());

}  

Error after pressing add button:

    java.lang.NullPointerException
    at bancodedados.BancoDeDados.adcionar(BancoDeDados.java:47)
    at bancodedados.Teste.jButton1ActionPerformed(Teste.java:92)
    at bancodedados.Teste.access$000(Teste.java:12)
    at bancodedados.Teste$1.actionPerformed(Teste.java:48)
    at 
  javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
    at
 javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
    at 
 javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
    at 
 javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
    at 
 javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListe ner.java:252)
    at java.awt.Component.processMouseEvent(Component.java:6533)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
    at java.awt.Component.processEvent(Component.java:6298)
    at java.awt.Container.processEvent(Container.java:2238)
    at java.awt.Component.dispatchEventImpl(Component.java:4889)
    at java.awt.Container.dispatchEventImpl(Container.java:2296)
    at java.awt.Component.dispatchEvent(Component.java:4711)
    at 
 java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4897)
    at 
 java.awt.LightweightDispatcher.processMouseEvent(Container.java:4534)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4475)
    at java.awt.Container.dispatchEventImpl(Container.java:2282)
    at java.awt.Window.dispatchEventImpl(Window.java:2746)
    at java.awt.Component.dispatchEvent(Component.java:4711)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:760)
    at java.awt.EventQueue.access$500(EventQueue.java:97)
    at java.awt.EventQueue$3.run(EventQueue.java:709)
    at java.awt.EventQueue$3.run(EventQueue.java:703)
    at java.security.AccessController.doPrivileged(Native Method)
    at 
 java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
    at 
 java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:90)
    at java.awt.EventQueue$4.run(EventQueue.java:733)
    at java.awt.EventQueue$4.run(EventQueue.java:731)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:730)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:205)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
java.lang.NullPointerException
    at bancodedados.BancoDeDados.adcionar(BancoDeDados.java:47)
    at bancodedados.Teste.jButton1ActionPerformed(Teste.java:92)
    at bancodedados.Teste.access$000(Teste.java:12)
    at bancodedados.Teste$1.actionPerformed(Teste.java:48)
  • Did you call the connection method before you called add? Prompted the database class in jframe?

  • Provide a [mcve] so that it is possible to analyze the code better. It is not possible to execute these excerpts and the boot class is incomplete.

  • At what point do you call the method conexao() ? Another thing: which is line 47 in the method adicionar()?

  • See the Victor hints in the linked question for a similar problem and also the hints of the other linked question about the main reasons that pop nullpointer. If doubt persists, edit the question and provide a [mcve] so that it is possible to analyze the code better.

No answers

Browser other questions tagged

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