我試圖做一個JavaBeans組件,但它給我這個錯誤: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;
}
}
,最後我創造另一個項目來測試組件:
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());
}
}
任何幫助將不勝感激..
的可能重複[什麼是空指針異常?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception) –