2014-04-30 2 views
0

내가 JavaBeans 컴퍼넌트을하려고하지만 나에게이 오류 제공 :자바 빈즈 컴포넌트 java.lang.NullPointerException이

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

구성 요소 :

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 ; 

    } 

} 

내가 다른 클래스가 "페르소나"과를 그녀의 속성, 생성자, getter 및 setter.

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; 
    } 




} 

하고 마침내는 컴포넌트를 테스트하는 다른 프로젝트를 생성 ..

+0

의 중복 가능성 [null 포인터 예외 무엇입니까?] (http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception를) 그것은 너무 JDBC와 함께 작동 –

답변

0

있어서 comp.Mostrar()null

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()); 

    } 
} 

어떤 도움이 이해 될 것이다 복귀된다. Componente 클래스의 lista 속성은 절대로 설정되지 않습니다. comp.Mostrar() 메서드는 Personas 클래스의 lista 변수에 Persona 인스턴스를 추가합니다. 요구 사항이 무엇인지 확실하지 않지만 메서드에서 p.lista을 반환해야 할 수 있습니다.

1

이 코드를 시도한 결과 잘되었습니다. SQL 데이터베이스 대신 오라클을 사용하여 변경할 수 있습니다.

그에 따라 데이터베이스를 변경할 수 있습니다.

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 ; 
    } 
} 
+0

, 열쇠는 목록을 선언했다 lista = new LinkedList (). 대단히 감사합니다. – user3325719

관련 문제