내 배열을 처리하는 클래스 (ListDePersonnes
)를 작성하고 싶습니다. 현재 개체를 추가하는 방법과 배열 내용을 화면에 인쇄하는 방법 두 가지만 있습니다.개체 배열에 멤버 추가
ListDePersonnes
import java.util.Scanner;
public class ListDePersonnes {
int count,t;
personne liste[];
Scanner s=new Scanner(System.in);
public void initialize() { //
System.out.println("entrer la taille"); //
t = s.nextInt(); // I'm not so sure about this part.
personne[] liste = new personne[t]; //
}
public void addpersonne(){
System.out.println("what is the name?");
String nom= s.next();
System.out.println("what is the age?");
int age= s.nextInt();
System.out.println("what is the weight?");
double poid= s.nextDouble();
liste[count] = new personne(nom,age,poid); // weight is poid in frensh
count++;
}
public void showAll(){
for (int i=0;i<t;i++){
System.out.println("name: "+ liste[i].getNom() + "/age: "+liste[i].getAge()+"/poid: "+liste[i].getPoid());
}
}
}
personne
public class personne {
private String nom;
private int age;
private double poid;
public personne(String nom, int age, double poid) {
this.nom = nom;
this.age = age;
this.poid = poid;
}
}
Main
import java.util.Scanner;
public class LaListe {
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
ListDePersonnes lper= new ListDePersonnes();
lper.initialize();
lper.addpersonne();
}
}
,174,
발생 오류 : LaListe.main (LaListe.java:16)에서 ListDePersonnes.addpersonne (ListDePersonnes.java:29) 에서 스레드에서
예외 "주"java.lang.NullPointerException이
'personne [] liste = new personne [t];'. 클래스 필드 변수를 섀도 잉하고 있습니다. 그것은'liste = new personne [t];이어야합니다. –
많은 남자, 삭제하는 방법에 대한 아이디어에 고마워요? –
나에게 쉬운 걸. 나는 이것에 처음이다 : 웃음 그래서 무서운되고 ... 그냥 코드를 포맷하고 어떤 노력을 보여, 사람들은 downvote 또는 닫지 않을 것입니다 – mprabhat