Bonjour,

j'ai un petit probleme.
au fait j'ai deux classes categorie et livre dont chaque livre appartient à un categorie.

voilà l'erreur qui m'affiche:

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
Exception in thread "main" java.lang.NullPointerException
	at metier.CatalogueMetierImpl.getTousLivres(CatalogueMetierImpl.java:93)
	at metier.TestMetier.main(TestMetier.java:20)

les differentes classes:

class livre:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package metier;
 
import java.io.Serializable;
 
public class Livre implements Serializable {
 
	private String idLivre;
	private Categorie cat;
	private String titre;
	private String isbn;
	private String dateEdit;
	private String nomAut;
 
	public Livre() {
		super();
		// TODO Auto-generated constructor stub
	}
 
 
	public Livre(String idLivre, Categorie cat, String titre, String isbn, String dateEdit, String nomAut) {
		super();
		this.idLivre = idLivre;
		this.cat = cat;
		this.titre = titre;
		this.isbn = isbn;
		this.dateEdit = dateEdit;
		this.nomAut = nomAut;
	}
 
 
	public String getIdLivre() {
		return idLivre;
	}
	public void setIdLivre(String idLivre) {
		this.idLivre = idLivre;
	}
 
	public Categorie getCat() {
		return cat;
	}
	public void setCat(Categorie cat) {
		this.cat = cat;
	}
	public String getTitre() {
		return titre;
	}
	public void setTitre(String titre) {
		this.titre = titre;
	}
	public String getIsbn() {
		return isbn;
	}
	public void setIsbn(String isbn) {
		this.isbn = isbn;
	}
	public String getDateEdit() {
		return dateEdit;
	}
	public void setDateEdit(String dateEdit) {
		this.dateEdit = dateEdit;
	}
	public String getNomAut() {
		return nomAut;
	}
	public void setNomAut(String nomAut) {
		this.nomAut = nomAut;
	}
 
 
}
categorie
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package metier;
 
import java.io.Serializable;
 
public class Categorie  implements Serializable{
	private long reference;
	private String designation;
 
 
	public Categorie(long reference, String désignation) {
		super();
		this.reference = reference;
		this.designation = désignation;
	}
 
 
	public long getReference() {
		return reference;
	}
 
 
	public void setReference(long reference) {
		this.reference = reference;
	}
 
 
	public String getDesignation() {
		return designation;
	}
 
 
	public void setDesignation(String désignation) {
		this.designation = désignation;
	}
 
 
	public Categorie() {
		super();
	}
 
 
}
fonction pour afficher tous les livres
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
@Override
	public List<Livre> getTousLivres() {
		List<Livre> livs=new ArrayList<Livre>();
		Connection conn=SingletonConnection.getConnection();
		try {
			PreparedStatement ps=conn.prepareStatement
					("select * from livres");
			ResultSet rs =ps.executeQuery();
			while(rs.next()){
				Livre l=new Livre();
				l.setIdLivre(rs.getString("idlivre"));
				l.setTitre(rs.getString("titre"));
				l.setIsbn(rs.getString("isbn"));
				l.setDateEdit(rs.getString("datedit"));
				l.setNomAut(rs.getString("nomaut"));
				l.getCat().setReference(rs.getLong("ref_cat"));
				livs.add(l);
			}
			ps.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return livs;
	}
test:main
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
	//metier.getTousLivres();
	   System.out.println("--------------------------------------");
		List<Livre> cat1 =metier.getTousLivres();
		for(Livre c:cat1){
			System.out.println(c.getIdLivre());
		}
merci
cordialement