0

i am trying to do a javaBeans component but it give me this error:

Exception in thread "main" java.lang.NullPointerException
    at probarcomponente.ProbarComponente.main(ProbarComponente.java:37)
Java Result: 1

The component:

public class Componente implements Serializable {
    private List<Persona> lista;


    public Componente() {
    }


    public class Personas {

        List<Persona> lista = new LinkedList<Persona>();

    }

    public List <Persona> Mostrar() {

        try {
            Connection conexion = DriverManager.getConnection("jdbc:mysql://192.168.100.128/mibase", "asis", "titanic24");
            java.sql.Statement comando = conexion.createStatement();
            String sql = "select * from dades_pers";
            ResultSet result = comando.executeQuery(sql);

            while (result.next()) {

                String nif = result.getString(1);
                String nom = result.getString(2);
                String cognoms = result.getString(3);
                String tel = result.getString(4);

            Personas p = new Personas();

            // Inserción en la lista 
            p.lista.add(new Persona(nif,nom,cognoms,tel)); 


            }  

        } catch (SQLException ex) {
            System.out.println("Error al mostrar datos");
            System.out.println(ex.getMessage());

        }
        return lista ;

    }

}

I have another class "Persona" with her attributes,constructor ,getters and setters.

public class Persona {
   private String nif ;
   private String nom;
   private String cognoms;
   private String telf;

    public Persona(String nif, String nom, String cognoms, String telf) {
        this.nif = nif;
        this.nom = nom;
        this.cognoms = cognoms;
        this.telf = telf;
    }

    public String getNif() {
        return nif;
    }

    public String getNom() {
        return nom;
    }

    public String getCognoms() {
        return cognoms;
    }

    public String getTelf() {
        return telf;
    }

    public void setNif(String nif) {
        this.nif = nif;
    }

    public void setNom(String nom) {
        this.nom = nom;
    }

    public void setCognoms(String cognoms) {
        this.cognoms = cognoms;
    }

    public void setTelf(String telf) {
        this.telf = telf;
    }




}

and finally i create another project to test the component:

public class ProbarComponente {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws SQLException {

        Componente comp = new Componente();

        List<Persona> lista = new LinkedList<Persona>();
        lista= comp.Mostrar();
        System.out.print("Tamaño de la lista es"+lista.size());

    }
}

Any help will be appreciated..

user3325719
  • 75
  • 4
  • 14
  • possible duplicate of [What is a Null Pointer Exception?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception) – Luiggi Mendoza Apr 30 '14 at 14:56

2 Answers2

1

I have tried this code and it works fine. Instead of SQL database i have used Oracle, you can change

that accordingly.

public class Componente implements Serializable {
            private static final long serialVersionUID = 1L;

            List<Persona> lista = new LinkedList<Persona>();

            public Componente() {
            }


    public List <Persona> Mostrar() {

        try {
            Class.forName("oracle.jdbc.driver.OracleDriver");
            Connection conexion = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe", "system", "oracle");
            Statement comando = conexion.createStatement();
            String sql = "select * from dades_pers";
            ResultSet result = comando.executeQuery(sql);

            while (result.next()) {

                String nif = result.getString(1);
                String nom = result.getString(2);
                String cognoms = result.getString(3);
                String tel = result.getString(4);

                lista.add(new Persona(nif,nom,cognoms,tel));
            }  

        } catch (SQLException | ClassNotFoundException ex) {
            System.out.println("Error al mostrar datos");
            System.out.println(ex.getMessage());

        }
        return lista ;
    }
}
Aman
  • 979
  • 3
  • 10
  • 23
  • It works with JDBC too ,the key was declare up the List lista = new LinkedList() .Thank you very much for your help . – user3325719 Apr 30 '14 at 18:00
0

The method comp.Mostrar() is returning null. The lista property in the Componente class is never set. The comp.Mostrar() method is adding Persona instances to the lista variable in the Personas class. I am not sure what the requirement is, but you probably need to return p.lista from the comp.Mostrar() method.

Priyesh
  • 2,041
  • 1
  • 17
  • 25