Bonsoir,
J'ai un problème que je n'arrive pas à résoudre en fait j'ai une classe personne et deux sous classes fils teacher et Student. une 3 ème classe université qui contient un tableau qui contient 3 teachers et 3 student puis je veux afficher les détails du tableau
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
package test;
 
public abstract class Person {
	protected String name;
	protected int age;
	public Person(String n, int a){
		name=n;
		age=a;
 
	}
	public String getName(){
		return name;
	}
	public int getAge(){
		return age;
	}
	public void setName(String n){
		name=n;
	}
	public void setAge(int a){
		age=a;
	}
	public abstract void print();
 
 
}
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
package test;
 
public class Teacher extends Person {
private float salary;
public Teacher(String n, int a, float s){
	super(n,a);
	salary=s;
}
public float getSalary(){
	return salary;
}
public void setSalary(float s){
	salary=s;
}
public void print(){
	name=getName();
	age=getAge();
	System.out.println("The name of Teacher is "+name+"\n"+" the age of the teacher is  "+ age+"\n"+"The salary of the teacher is "+salary);
 
}
}
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
package test;
 
public class Student extends Person {
	private int id;
	public Student(String name,int age, int id){
		super(name,age);
		this.id=id;
	}
	public int getId(){
		return id;
	}
	public void setId(int i){
		id=i;
	}
	public void print(){
		System.out.println("The name od the student is "+name+"\n"+"The age of the student is "+age+"\n"+"The id of the student is "+id);
	}
 
}
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
public class University {
	Person tab[];
	int size_tab;
	int n=0;
	public University(int size, int x){
		size_tab=size;
		n=x;
		tab=new Person[size_tab];
 
	}
	public int addPerson(Person p){
		System.out.println("n= "+n);
		tab[n].name=p.name;
		tab[n].age=p.age;
		System.out.println("Person "+ tab[n]);
		n++;
		return n;
 
 
	}
	public void PrintAll(){
		for (int i=0;i<n;i++){
			System.out.println(tab[i]);
		}
	}
 
}
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
package test;
 
public class Principal {
	public static void main(String []args){
		Teacher t1=new Teacher("Boutheina",32,9000);
		Teacher t2=new Teacher("Abir",29,3500);
		Teacher t3=new Teacher ("Asma",25,5000);
		Student s1=new Student("Aicha",20,12345);
		Student s2=new Student("Meriem",21,45678);
		Student s3=new Student ("Zeineb",19,78912);
		University u=new University(7,6);
		u.addPerson(s3);
		u.addPerson(s2);
		u.addPerson(s1);
		u.addPerson(t3);
		u.addPerson(t2);
		u.addPerson(t1);
		u.PrintAll();
	}
 
}
cet erreur est affiché:
Exception in thread "main" java.lang.NullPointerException
at test.University.addPerson(University.java:15)
at test.Principal.main(Principal.java:12)