我正在尝试做一个javaBeans组件,但它给了我这个错误:
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 ;
}
}
我有另一个类"Persona",包含她的属性、构造函数、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());
}
}
任何帮助都将不胜感激。。
我试过这段代码,它运行得很好。您可以更改,而不是我使用的SQL数据库Oracle
因此。
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 ;
}
}
方法comp.Mostrar()
返回null
。Componente
类中的lista
属性从未设置。comp.Mostrar()
方法将Persona
实例添加到Personas
类中的lista
变量中。我不确定要求是什么,但您可能需要从comp.Mostrar()
方法返回p.lista
。