Results 1 to 12 of 12
  1. #1
    alphasil is offline Member
    Join Date
    Jan 2012
    Posts
    33
    Rep Power
    0

    Default Problem with DAL and BLL classes

    Hi

    i have 3 classes ( users, userDAL and UsersBLL)

    Everytime i trying to add record to my db i have null in all fields

    This is my UsersDAL method

    Java Code:
        public void criarUser(Users us) throws SQLException {
            String sqlCommand =
                    "INSERT INTO USERS (UTLIZADOR, PASS, EMAIL, CARGO) VALUES('"
                    + this.nome + "', '" + this.pass + "', '" + this.email + "', '" + this.cargo + "')";
            this.dbo.executeSQL(sqlCommand);
        }
    My UsersBLL

    Java Code:
    public class UsersBLL {
        
        UsersDAL us;
        DBAccessObj obj;
           
        public UsersBLL(){
            
            if(us == null){
                us = new UsersDAL();
            }
            
        }
        
        public void criarUser(Users users) throws SQLException{
            
            try
            {
                us.criarUser(users);
            }
            catch (Exception ex)
            {
                //tratamento de erro
                //tratarErro(ex);
     
                //erro para a camada superior.
                throw ex;
            }
        }
    and finally what i use to add records

    Java Code:
    private void btNovoActionPerformed(java.awt.event.ActionEvent evt) {                                       
            // TODO add your handling code here:
            String mens;
            
            UsersBLL b = new UsersBLL();
            try {
                txt_nome.getText();
                txt_pass.getText();
                txt_email.getText();
                combo.getSelectedItem().toString();         
                int resposta = JOptionPane.showConfirmDialog(null, "Os dados estão corretos?", "Aviso", JOptionPane.OK_CANCEL_OPTION);
                if (resposta == JOptionPane.OK_OPTION) {
                    b.criarUser(users);
                    mens = "Informação do User criada com sucesso!";
                    //javax.swing.JOptionPane.MessageDialog(null, mens, "Informação", javax.swing.JOptionPane.OK_OPTION);
                } else if (resposta == JOptionPane.CANCEL_OPTION) {
                }
    Is anythhing wrong in this code??

    Thnaks

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,610
    Rep Power
    5

    Default Re: Problem with DAL and BLL classes

    Here is a snippet of your code:

    Java Code:
    UsersDAL us;DBAccessObj obj; 
           
    
    public UsersBLL(){
       if(us == null){
           us = new UsersDAL();
       }
    }
    Checking for us == null is not necessary since this is a constructor and the default value for all instance references is null. It sounds like you expect us to not be null sometimes as though it was defined earlier. That will not work here. I presume the arguments to your SQL command are null because they haven't been initialized anywhere. Perhaps you meant for UsersDAL to be declared static and act as a singleton. I would need to see the UsersDAL class to provide more help.

    Regards,
    Jim
    Last edited by jim829; 06-03-2013 at 04:54 PM. Reason: YAT
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,963
    Rep Power
    8

    Default Re: Problem with DAL and BLL classes

    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  4. #4
    alphasil is offline Member
    Join Date
    Jan 2012
    Posts
    33
    Rep Power
    0

    Default Re: Problem with DAL and BLL classes

    Hi Jim

    Thanks for your reply

    This is my UserDAL class

    Java Code:
    public class UsersDAL {
    
        private int idUser;
        private String nome, pass, email, cargo;
        //
        private List<Users> user = new ArrayList<>();
        private DBAccessObj dbo;
    
        public UsersDAL() {
            this.dbo = new DBAccessObj();
            this.dbo.openConnection();
        }
    
        public List<Users> getUsers() {
            return this.user;
        }
    
        public void criarUser(Users us) throws SQLException {
            String sqlCommand =
                    "INSERT INTO USERS (UTLIZADOR, PASS, EMAIL, CARGO) VALUES('"
                    + this.nome + "', '" + this.pass + "', '" + this.email + "', '" + this.cargo + "')";
            this.dbo.executeSQL(sqlCommand);
        }
    
        public void retrieveUser(int id) throws SQLException {
            String sqlCommand =
                    "SELECT UTLIZADOR, PASS, EMAIL, CARGO FROM USERS WHERE ID = '" + id + "'";
    
            ResultSet user = this.dbo.executeQuery(sqlCommand);
    
            if (user.next()) {
                this.idUser = id;
                this.nome = user.getString("UTLIZADOR");
                this.pass = user.getString("PASS");
                this.email = user.getString("EMAIL");
                this.cargo = user.getString("CARGO");
            }
        }
    
        public void updateUser() {
            String sqlCommand =
                    "UPDATE USERS SET UTLIZADOR = '" + this.nome + "', PASS = '" + this.pass + "', EMAIL = '"
                    + this.email + "', CARGO = '" + this.cargo + "' WHERE ID = '" + this.idUser + "'";
    
            this.dbo.executeSQL(sqlCommand);
        }
    
        public void deleteUser() {
            String sqlCommand = "DELETE FROM USERS WHERE ID = '" + this.idUser + "'";
            this.dbo.executeSQL(sqlCommand);
        }
    
        public static ResultSet listaUsers(DBAccessObj dbo){
            String sqlCommand = "SELECT IDUSERS, UTLIZADOR, PASS, EMAIL, CARGO FROM USERS";
            ResultSet users;
            dbo.openConnection();
            users = dbo.executeQuery(sqlCommand);
            return users;
        }
        
      
       public Boolean checkLogin(String uname,String pwd) throws SQLException
        {
            PreparedStatement pst;
            pst = dbo.openConnection().prepareStatement("select * from USERS where UTLIZADOR=? and PASS=?");
            ResultSet rs;
            try {
                            
                pst.setString(1, uname);
                pst.setString(2, pwd); 
                rs=pst.executeQuery();
                if(rs.next())
                {
                    return true;
                }
                else
                {
                    return false;
                }
            } catch (Exception e) {
                System.out.println("Erro na validação"+e);
                return false;
            }
        }
    }

    and my UsersBLL class

    Java Code:
    public class UsersBLL {
        
        UsersDAL us;
        DBAccessObj obj;
           
        public UsersBLL(){
            
            if(us == null){
                us = new UsersDAL();
            }
            
        }
        
        public void criarUser(Users users) throws SQLException{
            
            try
            {
                us.criarUser(users);
            }
            catch (Exception ex)
            {
                //tratamento de erro
                //tratarErro(ex);
     
                //erro para a camada superior.
                throw ex;
            }
        }
    
        
        public void updateUsers(Users users) throws SQLException{
            
            if((users.user.length()== 0) && (users.pass.length()==0) && (users.email.length()==0) || users.cargo.equals("Escolha uma opção"))
            {
                throw new EmptyStackException();      
            }
           
            us.updateUser();
        }
        
        public ResultSet listaUsers(DBAccessObj dbo) throws SQLException {      
            try{
           
            }catch(Exception e){
               System.err.println("Caught IOException: " + e.getMessage()); 
            }
            return us.listaUsers(dbo);
        }
        
        public void apagaUser() throws SQLException {
            
            try{
               
                us.deleteUser();
            }catch(Exception e){
               System.err.println("Caught IOException: " + e.getMessage()); 
            }    
        }
        
    }
    Thank you.

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,610
    Rep Power
    5

    Default Re: Problem with DAL and BLL classes

    In your actionPerformed method you have the following:

    Java Code:
    txt_nome.getText();
    txt_pass.getText();
    txt_email.getText();
    How do you get them to the User class to be added? I recommend placing some print statements throughout your program to determine where variables are and are not getting set.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  6. #6
    alphasil is offline Member
    Join Date
    Jan 2012
    Posts
    33
    Rep Power
    0

    Default Re: Problem with DAL and BLL classes

    Hi Jim

    Thanks again

    I put some print statement in the actionperformed before calling the method b.criaUsers(users);

    i have the correct values

    Nome: fren
    Pass: 123
    Email: g@gmail.com
    Cargo: Docente

    Can you explain how can i pass those values the the BLL class and then to the Dal class to create that record? I thought it was just call the method.

    Sorry

  7. #7
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,610
    Rep Power
    5

    Default Re: Problem with DAL and BLL classes

    Here is part of your code:

    Java Code:
    public void updateUsers(Users users) throws SQLException{         
            if((users.user.length()== 0) && (users.pass.length()==0) && (users.email.length()==0) || users.cargo.equals("Escolha uma opção"))
            {
                throw new EmptyStackException();      
            }
            us.updateUser();
        }
    The values define in the users class is never assigned to like named fields in the UsersDAL class so they remain null. You need to pass the reference of users to UsersDAL like this:

    us.updateUser(users);

    and then use those values to write to the DB. Or somehow assign the values from users to the UsersDAL fields before calling us.updateUsers();

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  8. #8
    alphasil is offline Member
    Join Date
    Jan 2012
    Posts
    33
    Rep Power
    0

    Default Re: Problem with DAL and BLL classes

    Thank Jim

    I have made some changes, but if i call the UserDAL class, it works, if i call from UsersBLL doesn't

    My Users class it that one

    Java Code:
    
    public class Users {
        
        int idUser;
        String user;
        String pass;
        String email;
        String cargo;
        
        public Users(int idUser, String user, String pass, String email, String cargo){
           
            this.idUser = idUser;
            this.user = user;
            this.pass = pass;
            this.email = email;
            this.cargo = cargo;
        }
    
        public int getIdUser() {
            return idUser;
        }
    
        public void setIdUser(int idUser) {
            this.idUser = idUser;
        }
    
        public String getUser() {
            return user;
        }
    
        public void setUser(String user) {
            this.user = user;
        }
    
        public String getPass() {
            return pass;
        }
    
        public void setPass(String pass) {
            this.pass = pass;
        }
    
        public String getEmail() {
            return email;
        }
    
        public void setEmail(String email) {
            this.email = email;
        }
    
        public String getCargo() {
            return cargo;
        }
    
        public void setCargo(String cargo) {
            this.cargo = cargo;
        }
            
    }
    The createUser in userDAL is this one.

    Java Code:
    public class UsersDAL {
    
        private int idUser;
      
        //
        Users us;
        private DBAccessObj dbo;
    
        public UsersDAL() {
            this.dbo = new DBAccessObj();
            this.dbo.openConnection();
        }
    
        public void criarUser(Users users) throws SQLException {
            String sqlCommand =
                    "INSERT INTO USERS (UTLIZADOR, PASS, EMAIL, CARGO) VALUES('"
                    + users.user + "', '" + users.pass + "', '" + users.email + "', '" + users.cargo + "')";
            this.dbo.executeSQL(sqlCommand);
        }
    The BLL as suggested

    Java Code:
    public class UsersBLL {
               
        UsersDAL userDal = new UsersDAL();
        public UsersBLL(){
            
        }
        
        public void criarUser(Users users) throws SQLException{
            
            try
            {
                userDal.criarUser(users);
                
            }
            catch (Exception ex)
            {
                //tratamento de erro
                //tratarErro(ex);
     
                //erro para a camada superior.
                throw ex;
            }
        }
    But now i have nullPoint instead of records null in my db

    The problem should be here

    Java Code:
    private void btNovoActionPerformed(java.awt.event.ActionEvent evt) {                                       
            // TODO add your handling code here:
            String mens;
            UsersBLL b = new UsersBLL();
            users.setUser(this.txt_nome.getText()); //Assign values of txt_nome to users in Users class then when i call the BLL class should record??
            users.setPass(this.txt_pass.getText());
            users.setEmail(this.txt_email.getText());
            users.setCargo(this.combo.getSelectedItem().toString());
            try {   
                b.criarUser(users);
            } catch (SQLException ex) {
                Logger.getLogger(ListUsers.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    I don't get it honestly

  9. #9
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,610
    Rep Power
    5

    Default Re: Problem with DAL and BLL classes

    You are going back and forth between classes and only showing segments of your code so its hard to know what is going on. In the btNovoActionPerformed method are you certain users is non-null? Also, in each of your classes, you may want to override toString to display useful info when you print out the class. For example in Users:

    Java Code:
    Users users = new Users(....);
    
    public String toString() {
       return "[" + user + "," + pass + "," + email +"," + cargo + "]";
    }
    
    // Then you can do
    
    System.out.println(users);
    Then print out the class at the beginning of each entry in a method where it is used. Sometimes it helps to write a small static class to assist.

    Java Code:
    public class Debug {
        public static info(String msg, Users user) {
             System.out.println(msg);
             System.out.println(user);
        }
    }
    Usage:
    Java Code:
    Debug.info("entered method UserDAL.criarUser", users);
    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  10. #10
    alphasil is offline Member
    Join Date
    Jan 2012
    Posts
    33
    Rep Power
    0

    Default Re: Problem with DAL and BLL classes

    Quote Originally Posted by jim829 View Post
    You are going back and forth between classes and only showing segments of your code so its hard to know what is going on. In the btNovoActionPerformed method are you certain users is non-null?
    Jim
    Maybe it's better to show my full code.

    But the main problem is here:
    Java Code:
     users.setUser(this.txt_nome.getText()); //ERROR STAR HERE - at forms.ListUsers.btNovoActionPerformed(ListUsers.java:298)
     users.setPass(this.txt_pass.getText());
     users.setEmail(this.txt_email.getText());
     users.setCargo(this.combo.getSelectedItem().toString());
    I have this error
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at forms.ListUsers.btNovoActionPerformed(ListUsers.ja va:298)
    at forms.ListUsers.access$100(ListUsers.java:21)
    at forms.ListUsers$3.actionPerformed(ListUsers.java:1 89)

    Users class
    Java Code:
    public class Users {
        
        int idUser;
        String user;
        String pass;
        String email;
        String cargo;
        
        public Users(int idUser, String user, String pass, String email, String cargo){
           
            this.idUser = idUser;
            this.user = user;
            this.pass = pass;
            this.email = email;
            this.cargo = cargo;
        }
    
        public int getIdUser() {
            return idUser;
        }
    
        public void setIdUser(int idUser) {
            this.idUser = idUser;
        }
    
        public String getUser() {
            return user;
        }
    
        public void setUser(String user) {
            this.user = user;
        }
    
        public String getPass() {
            return pass;
        }
    
        public void setPass(String pass) {
            this.pass = pass;
        }
    
        public String getEmail() {
            return email;
        }
    
        public void setEmail(String email) {
            this.email = email;
        }
    
        public String getCargo() {
            return cargo;
        }
    
        public void setCargo(String cargo) {
            this.cargo = cargo;
        }
              
    }
    UsersDAL class
    Java Code:
    public class UsersDAL {
    
        private int idUser;
        private String nome, pass, email, cargo;
        //
        Users us;
        private DBAccessObj dbo;
    
        public UsersDAL() {
            this.dbo = new DBAccessObj();
            this.dbo.openConnection();
        }
    
        public void criarUser(Users users) throws SQLException {
            String sqlCommand =
                    "INSERT INTO USERS (UTLIZADOR, PASS, EMAIL, CARGO) VALUES('"
                    + users.user + "', '" + users.pass + "', '" + users.email + "', '" + users.cargo + "')";
            this.dbo.executeSQL(sqlCommand);
        }
    
        public void retrieveUser(int id) throws SQLException {
            String sqlCommand =
                    "SELECT UTLIZADOR, PASS, EMAIL, CARGO FROM USERS WHERE ID = '" + id + "'";
    
            ResultSet user = this.dbo.executeQuery(sqlCommand);
    
            if (user.next()) {
                this.idUser = id;
                this.nome = user.getString("UTLIZADOR");
                this.pass = user.getString("PASS");
                this.email = user.getString("EMAIL");
                this.cargo = user.getString("CARGO");
            }
        }
    
        public void updateUser(Users users) {
            String sqlCommand =
                    "UPDATE USERS SET UTLIZADOR = '" + users.user + "', PASS = '" + users.pass + "', EMAIL = '"
                    + users.email + "', CARGO = '" + users.cargo + "' WHERE ID = '" + users.user + "'";
    
            this.dbo.executeSQL(sqlCommand);
        }
    
        public void deleteUser(Users users) {
            String sqlCommand = "DELETE FROM USERS WHERE ID = '" + users.idUser + "'";
            this.dbo.executeSQL(sqlCommand);
        }
    
        public static ResultSet listaUsers(DBAccessObj dbo){
            String sqlCommand = "SELECT IDUSERS, UTLIZADOR, PASS, EMAIL, CARGO FROM USERS";
            ResultSet users;
            dbo.openConnection();
            users = dbo.executeQuery(sqlCommand);
            return users;
        }
        
      
       public Boolean checkLogin(String uname,String pwd) throws SQLException
        {
            PreparedStatement pst;
            pst = dbo.openConnection().prepareStatement("select * from USERS where UTLIZADOR=? and PASS=?");
            ResultSet rs;
            try {
                            
                pst.setString(1, uname);
                pst.setString(2, pwd); 
                rs=pst.executeQuery();
                if(rs.next())
                {
                    return true;
                }
                else
                {
                    return false;
                }
            } catch (Exception e) {
                System.out.println("Erro na validação"+e);
                return false;
            }
        }
    }
    UsersBLL

    Java Code:
    public class UsersBLL {
               
        UsersDAL userDal = new UsersDAL();
        public UsersBLL(){
            
        }
        
        public void criarUser(Users users) throws SQLException{
            
            try
            {
                userDal.criarUser(users);
                
            }
            catch (Exception ex)
            {
                //tratamento de erro
                //tratarErro(ex);
     
                //erro para a camada superior.
                throw ex;
            }
        }
    
        public void updateUsers(Users users) throws SQLException{
            
            if((users.user.length()== 0) && (users.pass.length()==0) && (users.email.length()==0) || users.cargo.equals("Escolha uma opção"))
            {
                throw new EmptyStackException();      
            }
           
            userDal.updateUser(users);
        }
        
        public ResultSet listaUsers(DBAccessObj dbo) throws SQLException {      
            try{
           
            }catch(Exception e){
               System.err.println("Caught IOException: " + e.getMessage()); 
            }
            return userDal.listaUsers(dbo);
        }
        
        public void apagaUser(Users users) throws SQLException {
            
            try{
               
                userDal.deleteUser(users);
            }catch(Exception e){
               System.err.println("Caught IOException: " + e.getMessage()); 
            }    
        }
       
    }
    And finally the class where i have the form to see the records or to add new records

    ListUsers
    Java Code:
    public class ListUsers extends javax.swing.JPanel {
    
        private JPanel previousPanel = null;
        JFrame parent = null;
        private int idUsers = 0;
        private int linhaSeleccionada = -1;
        private DBAccessObj dbo;
        private Users users;
    
      
    
        public ListUsers(JFrame fparent, JPanel previousPanel, DBAccessObj dbo, int idUsers) {
            initComponents();
            this.parent = fparent;
            this.previousPanel = previousPanel;
            this.dbo = dbo;
            this.idUsers = idUsers;
    
            this.dbo.openConnection();
    
            ResultSet rsUser;
            try {
                UsersBLL us = new UsersBLL();
                rsUser = us.listaUsers(dbo);
                limparJTable();
                if (rsUser != null) {
                    javax.swing.table.DefaultTableModel model1 = (javax.swing.table.DefaultTableModel) tabelaUsers.getModel();
                    while (rsUser.next()) {
                        String aNome = ((String) rsUser.getString("UTLIZADOR"));
                        String aPass = ((String) rsUser.getString("PASS"));
                        String aEmail = ((String) rsUser.getString("EMAIL"));
                        String aCargo = ((String) rsUser.getString("CARGO"));
                        model1.addRow(new Object[]{aNome, aPass, aEmail, aCargo});
                    }
                }
            } catch (SQLException e) {
                System.out.println("Erro..." + e.getMessage());
                javax.swing.JOptionPane.showMessageDialog(null, "ERRO: " + e.getMessage(), "Aviso", javax.swing.JOptionPane.ERROR_MESSAGE);
            }
        }
    
        public void limparJTable() {
            javax.swing.table.DefaultTableModel model2 = (javax.swing.table.DefaultTableModel) tabelaUsers.getModel();
            model2.setRowCount(0);
        }
    
        /**
         * This method is called from within the constructor to initialize the form.
         * WARNING: Do NOT modify this code. The content of this method is always
         * regenerated by the Form Editor.
         */
        @SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
        private void initComponents() {
    
            jTextField2 = new javax.swing.JTextField();
            jScrollPane1 = new javax.swing.JScrollPane();
            tabelaUsers = new javax.swing.JTable();
            jPanel1 = new javax.swing.JPanel();
            jLabel1 = new javax.swing.JLabel();
            jLabel2 = new javax.swing.JLabel();
            jLabel3 = new javax.swing.JLabel();
            jLabel4 = new javax.swing.JLabel();
            txt_nome = new javax.swing.JTextField();
            txt_email = new javax.swing.JTextField();
            txt_pass = new javax.swing.JPasswordField();
            combo = new javax.swing.JComboBox();
            jLabel5 = new javax.swing.JLabel();
            idText = new javax.swing.JTextField();
            btNovo = new javax.swing.JButton();
            btUpdate = new javax.swing.JButton();
            btDel = new javax.swing.JButton();
            btVoltar = new javax.swing.JButton();
            btLimpar = new javax.swing.JButton();
    
            jTextField2.setText("jTextField2");
    
            setPreferredSize(new java.awt.Dimension(600, 400));
    
            tabelaUsers.setModel(new javax.swing.table.DefaultTableModel(
                new Object [][] {
    
                },
                new String [] {
                    "Nome", "Password", "Email", "Cargo"
                }
            ) {
                boolean[] canEdit = new boolean [] {
                    false, false, false, false
                };
    
                public boolean isCellEditable(int rowIndex, int columnIndex) {
                    return canEdit [columnIndex];
                }
            });
            tabelaUsers.addMouseListener(new java.awt.event.MouseAdapter() {
                public void mouseClicked(java.awt.event.MouseEvent evt) {
                    tabelaUsersMouseClicked(evt);
                }
            });
            jScrollPane1.setViewportView(tabelaUsers);
    
            jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createTitledBorder(""), "Dados"));
    
            jLabel1.setText("Nome:");
    
            jLabel2.setText("Pass:");
    
            jLabel3.setText("Email:");
    
            jLabel4.setText("Cargo:");
    
            combo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Escolha uma Opção", "Admin", "Docente", "Funcionário" }));
    
            jLabel5.setText("ID:");
    
            javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
            jPanel1.setLayout(jPanel1Layout);
            jPanel1Layout.setHorizontalGroup(
                jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jLabel3)
                        .addComponent(jLabel4)
                        .addComponent(jLabel2)
                        .addComponent(jLabel1)
                        .addComponent(jLabel5))
                    .addGap(18, 18, 18)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(txt_nome, javax.swing.GroupLayout.PREFERRED_SIZE, 230, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(txt_email)
                            .addComponent(combo, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(txt_pass, javax.swing.GroupLayout.PREFERRED_SIZE, 230, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(0, 0, Short.MAX_VALUE)))
                        .addComponent(idText, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            );
            jPanel1Layout.setVerticalGroup(
                jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup()
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel5)
                        .addComponent(idText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 10, Short.MAX_VALUE)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(txt_nome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel1))
                    .addGap(18, 18, 18)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(txt_pass, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel2))
                    .addGap(18, 18, 18)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(txt_email, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel3))
                    .addGap(18, 18, 18)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(combo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel4))
                    .addContainerGap())
            );
    
            btNovo.setText("Novo");
            btNovo.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    btNovoActionPerformed(evt);
                }
            });
    
            btUpdate.setText("Update");
            btUpdate.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    btUpdateActionPerformed(evt);
                }
            });
    
            btDel.setText("Apagar");
    
            btVoltar.setText("Voltar");
            btVoltar.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    btVoltarActionPerformed(evt);
                }
            });
    
            btLimpar.setText("Limpar");
            btLimpar.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    btLimparActionPerformed(evt);
                }
            });
    
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
            this.setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 443, Short.MAX_VALUE)
                        .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 42, Short.MAX_VALUE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(btLimpar)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(btNovo, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(btUpdate, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(btVoltar, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(btDel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                    .addGap(30, 30, 30))
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                    .addGap(27, 27, 27)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(btLimpar)
                            .addGap(80, 80, 80)
                            .addComponent(btNovo)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 72, Short.MAX_VALUE)
                            .addComponent(btUpdate))
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(0, 0, Short.MAX_VALUE)))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(btDel)
                            .addGap(53, 53, 53)
                            .addComponent(btVoltar))
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 158, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGap(31, 31, 31))
            );
        }// </editor-fold>                        
    
        private void tabelaUsersMouseClicked(java.awt.event.MouseEvent evt) {                                         
            // TODO add your handling code here:
            int linha = tabelaUsers.getSelectedRow();
            String dadosTabela = (tabelaUsers.getModel().getValueAt(linha, 0).toString());
            ResultSet rsUser;
            try {
                UsersBLL us = new UsersBLL();
                rsUser = us.listaUsers(dbo);
                limparJTable();
                if (rsUser != null) {
                    javax.swing.table.DefaultTableModel model1 = (javax.swing.table.DefaultTableModel) tabelaUsers.getModel();
                    while (rsUser.next()) {
                        String aId = ((String) rsUser.getString("IDUSERS"));
                        idText.setText(aId);
                        idText.setEditable(false);
                        String aNome = ((String) rsUser.getString("UTLIZADOR"));
                        txt_nome.setText(aNome);
                        String aPass = ((String) rsUser.getString("PASS"));
                        txt_pass.setText(aPass);
                        String aEmail = ((String) rsUser.getString("EMAIL"));
                        txt_email.setText(aEmail);
                        String aCargo = ((String) rsUser.getString("CARGO"));
                        combo.setSelectedItem(aCargo);
                        model1.addRow(new Object[]{aNome, aPass, aEmail, aCargo});
                    }
                }
            } catch (SQLException e) {
                System.out.println("Erro..." + e.getMessage());
                javax.swing.JOptionPane.showMessageDialog(null, "ERRO: " + e.getMessage(), "Aviso", javax.swing.JOptionPane.ERROR_MESSAGE);
            }
        }                                        
    
        private void btNovoActionPerformed(java.awt.event.ActionEvent evt) {                                       
            // TODO add your handling code here:
            String mens;
            UsersDAL userDal = new UsersDAL();
            UsersBLL b = new UsersBLL();
            users.setUser(this.txt_nome.getText());
            users.setPass(this.txt_pass.getText());
            users.setEmail(this.txt_email.getText());
            users.setCargo(this.combo.getSelectedItem().toString());
            System.out.print(users.getUser());
            try {   
                b.criarUser(users);
            } catch (SQLException ex) {
                Logger.getLogger(ListUsers.class.getName()).log(Level.SEVERE, null, ex);
            }
        }                                      
    
        private void btVoltarActionPerformed(java.awt.event.ActionEvent evt) {                                         
            // TODO add your handling code here:
            ((RequestHome) this.parent).switchPanels((JPanel) this, previousPanel);
        }                                        
    
        private void btLimparActionPerformed(java.awt.event.ActionEvent evt) {                                         
            // TODO add your handling code here:
            txt_nome.setText("");
            txt_pass.setText("");
            txt_email.setText("");
            combo.setSelectedIndex(0);
        }                                        
    
        private void btUpdateActionPerformed(java.awt.event.ActionEvent evt) {                                         
            // TODO add your handling code here:
            
        }                                        
    
        // Variables declaration - do not modify                     
        private javax.swing.JButton btDel;
        private javax.swing.JButton btLimpar;
        private javax.swing.JButton btNovo;
        private javax.swing.JButton btUpdate;
        private javax.swing.JButton btVoltar;
        private javax.swing.JComboBox combo;
        private javax.swing.JTextField idText;
        private javax.swing.JLabel jLabel1;
        private javax.swing.JLabel jLabel2;
        private javax.swing.JLabel jLabel3;
        private javax.swing.JLabel jLabel4;
        private javax.swing.JLabel jLabel5;
        private javax.swing.JPanel jPanel1;
        private javax.swing.JScrollPane jScrollPane1;
        private javax.swing.JTextField jTextField2;
        private javax.swing.JTable tabelaUsers;
        private javax.swing.JTextField txt_email;
        private javax.swing.JTextField txt_nome;
        private javax.swing.JPasswordField txt_pass;
        // End of variables declaration                   
    }
    This is all the class, you are right about beeing null, but i don't get why....

    The problem is in the ListUsers for sure on the above lines i told you.

    Thanks for all your help Jim

  11. #11
    alphasil is offline Member
    Join Date
    Jan 2012
    Posts
    33
    Rep Power
    0

    Default Re: Problem with DAL and BLL classes

    Hi Jim

    Quote Originally Posted by jim829 View Post
    You are going back and forth between classes and only showing segments of your code so its hard to know what is going on. In the btNovoActionPerformed method are you certain users is non-null? Also, in each of your classes, you may want to override toString to display useful info when you print out the class. For example in Users:

    Java Code:
    Users users = new Users(....);
    
    public String toString() {
       return "[" + user + "," + pass + "," + email +"," + cargo + "]";
    }
    
    // Then you can do
    
    System.out.println(users);
    Then print out the class at the beginning of each entry in a method where it is used. Sometimes it helps to write a small static class to assist.

    Java Code:
    public class Debug {
        public static info(String msg, Users user) {
             System.out.println(msg);
             System.out.println(user);
        }
    }
    Usage:
    Java Code:
    Debug.info("entered method UserDAL.criarUser", users);
    Regards,
    Jim
    Where do i piut this?? in each class?

  12. #12
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,610
    Rep Power
    5

    Default Re: Problem with DAL and BLL classes

    Nope. It is a public class. And you don't need an instance to call it since it is a static method. You will need to import it though. And the toString method should be overridden for each class. The details of the returned string are dependent on what you want to convey. In fact, I recommend changing the info signature to:

    Java Code:
    public static info (String msg, Object obj) {
        System.out.println(msg);
        System.out.println(obj);
    }
    Then you can use it for any class instance.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. Problem with method and classes
    By picasso88 in forum New To Java
    Replies: 3
    Last Post: 01-29-2012, 09:46 PM
  2. Classes Problem
    By FreeQy in forum New To Java
    Replies: 5
    Last Post: 11-05-2011, 03:37 PM
  3. Problem with classes
    By liakos in forum NetBeans
    Replies: 14
    Last Post: 03-10-2011, 07:34 PM
  4. inner classes problem
    By smallmos1 in forum New To Java
    Replies: 7
    Last Post: 11-18-2010, 03:07 PM
  5. problem with using classes and output
    By sjaakie in forum New To Java
    Replies: 3
    Last Post: 10-10-2010, 01:56 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •