gpt4 book ai didi

JavaBeans 组件 java.lang.NullPointerException

转载 作者:行者123 更新时间:2023-12-01 13:03:20 25 4
gpt4 key购买 nike

我正在尝试做一个 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());

}
}

任何帮助将不胜感激..

最佳答案

我已经尝试过这段代码并且工作正常。我使用的是 Oracle,而不是 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 ;
}
}

关于JavaBeans 组件 java.lang.NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23391435/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com