6 Replies Latest reply: Sep 16, 2008 6:17 PM by 801527 RSS

    Type missmatch cannot convert from ArrayList<Estudante> to Estudiante

    843785
      Hi, I'm so newbie in this forum so i have this problem.

      I'm doing a simple MVC program in an standalone app

      My controller is named ControladorEst .it implements ActionListener in body has the following:

      if(ae.getActionCommand().equalsIgnoreCase("Buscar"))
                     {
                          Estudiante estudiante = null;
                          ArrayList<Estudiante> estEnc;
                               
                          
                          estEnc= mEst.buscarEstudiante(Integer.parseInt(vEst.getTxtCodigo()));<-- This line has the error.
      .....

      Where mEst is a reference to my model named ModeloEstudiante who has the method buscarEstudiante(int codigo) as follows:



      public ArrayList<Estudiante> buscarEstudiante(int codigo)
           {
                Estudiante estudiante = new Estudiante();
                try
                {
                     String query ="SELECT codigo FROM estudiante WHERE codigo ='"+codigo+"'";
                     Connection conn= conex();
                     PreparedStatement ps = (PreparedStatement) conn.prepareStatement(query);
                     ResultSet rset = ps.executeQuery();
                     
                     
                     System.out.println("Cedula: "+ rset.getInt("cedula"));
                     System.out.println("Nombre: "+ rset.getString("nombre"));
                     
                     if (rset.next()){
                          System.out.println("Correct query");
                          
                          }else{
                          System.out.println("bad result of query");
                          }
                     rset.previous();
                     while(rset.next())
                     {
                          System.out.println("i'm in");
                          Estudiante est = new Estudiante();
                          est.setCodigo(rset.getInt("codigo"));
                          est.setCedula(rset.getInt("cedula"));
                          est.setNombre(rset.getString("nombre"));
                          
                          estudiantes.add(est);
                          
                     }
                     
                     
                     if (rset != null)
      rset.close();
      if (ps != null)
      ps.close();
      if (conn != null)
      conn.close();
                     
                     /*estudiante.setCodigo(rset.getInt("codigo"));
                     estudiante.setCedula(rset.getInt("cedula"));
                     estudiante.setNombre(rset.getString("nombre"));
                     */
                     
                }
                catch (Exception e)
                {
                     System.out.println(" Database Exception");
                     e.printStackTrace();
                }
                return estudiantes;
           }


      Why if buscarEstudiantes return and ArrayList<Estudiantes> when i invoke this method and asign its return to mEst in the controller give me this error:"Type missmatch cannot convert from ArrayList<Estudante> to Estudiante" ? mEst is an ArrayList<Estudiante>

      That's my doubt . please give me a hand,

      Thanks
        • 1. Re: Type missmatch cannot convert from ArrayList<Estudante> to Estudiante
          800378
          in
          public ArrayList<Estudiante> buscarEstudiante(int codigo)
          where you have declare estudiantes.

          Edited by: Ravi_Gupta on Sep 15, 2008 11:51 PM
          • 2. Re: Type missmatch cannot convert from ArrayList<Estudante> to Estudiante
            843785
            sorry i forget to show you the constructor of the class ModeloEstudiante

            public class ModeloEstudiante
            {
                 private ArrayList<Estudiante> estudiantes;
                 
            public Connection conex()
            {
            ....
            }

            public ModeloEstudiante()
                 {

                      estudiantes = new ArrayList<Estudiante>();
                 }

            public ArrayList<Estudiante> buscarEstudiante(int codigo)
            {
            ...
            ...
            return estudiantes

            }

            estudiantes is an ArrayList<Estudiantes>
            • 3. Re: Type missmatch cannot convert from ArrayList<Estudante> to Estudiante
              793982
              It's really strange, because your code seems to be right.
              But it's very difficult to read it this way, please use code tags wrapping your code. Look for the CODE button when posting. (Like I did bellow)

              Also avoid to post unnecessary code like your estudiante variable, that is not used and just confuses who is reading.
              public ArrayList<Estudiante> buscarEstudiante(int codigo)
              {
              Estudiante estudiante = new Estudiante();
              ...
              Regards,
              Henrique Abreu
              • 4. Re: Type missmatch cannot convert from ArrayList<Estudante> to Estudiante
                843785
                MoleloEstudiante.java

                {public class ModeloEstudiante
                {
                    private ArrayList<Estudiante> estudiantes;

                    public ModeloEstudiante()
                    {

                        estudiantes =  new ArrayList<Estudiante>();
                    }

                public Connection conex()
                {
                Connection conn = null;
                try
                {
                Class.forName("com.mysql.jdbc.Driver");
                conn= DriverManager.getConnection("jdbc:mysql://localhost/pruebaestudiante","root","");


                }
                catch (Exception e)
                {

                }
                return conn;
                }



                public ArrayList<Estudiante> buscarEstudiante(int codigo)
                {
                Estudiante estudiante = new Estudiante();
                try
                {
                String query ="SELECT codigo FROM estudiante WHERE codigo ='"+codigo+"'";
                Connection conn= conex();
                PreparedStatement ps = (PreparedStatement) conn.prepareStatement(query);
                ResultSet rset = ps.executeQuery();
                /*System.out.println("Resultado de la consulta: "+ rset);

                System.out.println("Cedula: "+ rset.getInt("cedula"));
                System.out.println("Nombre: "+ rset.getString("nombre"));
                */
                if (rset.next()){
                System.out.println("la consulta arrojo resultados");
                //System.out.println("se paso como login: "+log);//estaba comenntado
                //System.out.println("se paso como password: "+pass);//estaba comentado
                }else{
                System.out.println("la consulta no arrojo resultados");
                }
                rset.previous();
                while(rset.next())
                {
                System.out.println("entreeeeeeeeeee");
                Estudiante est = new Estudiante();
                est.setCodigo(rset.getInt("codigo"));
                est.setCedula(rset.getInt("cedula"));
                est.setNombre(rset.getString("nombre"));

                estudiantes.add(est);

                }


                if (rset != null)
                rset.close();
                if (ps != null)
                ps.close();
                if (conn != null)
                conn.close();

                /*estudiante.setCodigo(rset.getInt("codigo"));
                estudiante.setCedula(rset.getInt("cedula"));
                estudiante.setNombre(rset.getString("nombre"));
                */

                }
                catch (Exception e)
                {
                System.out.println("Cayo en un excepcion de base de datos");
                e.printStackTrace();
                }
                return estudiantes;
                }
                }

                ControladorEst.java
                {public class ControladorEst implements ActionListener {

                    private Vista vEst;
                    private ModeloEstudiante mEst;

                    public ControladorEst(Vista vEst)
                    {
                        this.vEst = vEst;
                        mEst = new ModeloEstudiante();
                    }



                public void actionPerformed(ActionEvent ae) {
                boolean ingresado= false;
                if(ae.getActionCommand().equalsIgnoreCase("Ingresar"))
                {
                mEst.ingresarEstudiante(new Estudiante());
                ingresado= true;
                vEst.limpiar();
                if(ingresado)
                {
                JOptionPane.showMessageDialog(new JFrame(),"Codigo de estudiante con exito","informacion",JOptionPane.INFORMATION_MESSAGE);
                }

                else
                {
                JOptionPane.showMessageDialog(new JFrame(),"Codigo de estudiante no encontrado","error",JOptionPane.ERROR_MESSAGE);
                }

                }
                else
                {
                if(ae.getActionCommand().equalsIgnoreCase("Buscar"))
                {
                Estudiante estudiante = null;
                ArrayList<Estudiante> estEnc;
                //estEnc = new ArrayList<Estudiante>();


                estEnc = mEst.buscarEstudiante(Integer.parseInt(vEst.getTxtCodigo()));
                boolean encontro= false;
                for(int i= 0; i< estEnc.size();i++)
                {
                estudiante = estEnc.get(i);
                if(estudiante!=null)
                {
                vEst.setTxtCodigo(Integer.toString(estudiante.getCodigo()));
                vEst.setTxtCedula(Integer.toString(estudiante.getCedula()));
                vEst.setTxtNombre(estudiante.getNombre());
                }
                else
                {
                JOptionPane.showMessageDialog(new JFrame(),"Estudiante no encontrado","Error",JOptionPane.ERROR_MESSAGE);
                vEst.limpiar();
                }
                }


                }
                }


                These are my complete code of my controller and model, please give me a hand would you??? .I used the tag code like you told me.
                Thanks
                • 5. Re: Type missmatch cannot convert from ArrayList<Estudante> to Estudiante
                  793982
                  Thanks for trying, but you didn't used the CODE button!
                  Your code is not formatted, didn't you saw it?
                  When posting there is also a Preview tab, where you can look if its alright. ;)
                  At last you can edit the post you just made.

                  About your problem
                  I copied the code you posted and pasted on my IDE so I could read it.
                  I also compiled it and I had no problem!
                  After, of course, creating empty classes Vista and Estudiante.
                  I got no errors on line:
                  estEnc = mEst.buscarEstudiante(Integer.parseInt(vEst.getTxtCodigo()));
                  See how it shows when you use
                   tags
                  
                  This seems to be a basic mistake, you're probably leaving something simple behind.
                  Saving the file for example or something like this.
                  
                  Edited by: henrique.abreu on 16/09/2008 19:07                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
                  • 6. Re: Type missmatch cannot convert from ArrayList<Estudante> to Estudiante
                    801527
                    Hi,
                    You did not used the tag "code" in the correct way.
                    You must put your code between the tag "code".