2

I want to write a class (ListDePersonnes) that handles my array, currently I only have two methods, one to add objects and one to print the array contents to screen.

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();
    }

}

The error thrown is:

Exception in thread "main" java.lang.NullPointerException at ListDePersonnes.addpersonne(ListDePersonnes.java:29) at LaListe.main(LaListe.java:16)

12
  • 3
    personne[] liste = new personne[t];. You are shadowing your class field variable. It should be liste = new personne[t]; Commented Nov 28, 2014 at 21:31
  • 1
    take easy on me guys . i'm new to this :) lol SO becoming scary... Just format your code and show some effort, people won't downvote or close Commented Nov 28, 2014 at 21:34
  • 1
    @IslamBouderbala be happy now you have an upvote too :) Commented Nov 28, 2014 at 21:38
  • 1
    You may want to consider having a method to create a larger array to hold more Personne's if the user fills up the array. Or using a different data structure such as an ArrayList, they are very simple to use. Commented Nov 28, 2014 at 21:40
  • 1
    @Islam Bouderbala Do you mean how to delete person from array? Array have static length, so you have to create new array with length of lenght-1 and then "copy" all Objects (except the one you want to remove) to new array. I recommend you to use List. Commented Nov 28, 2014 at 21:40

2 Answers 2

4

You're shadowing the variable liste. Replace

Personne[] liste = new Personne[t]; 

with

liste = new Personne[t]; 
Sign up to request clarification or add additional context in comments.

3 Comments

thanks a lot man , any idea on how to delete a personne from the array ?
If you need to delete an Object from the collection then you can use a List, e.g. ArrayList
do i declare it the same as i did with the simple array ?
2

Java includes a class to do such operations more easily, List (docs).

Example usage (from here):

To add an element to the list:

List<Personee> listA = new ArrayList<Peronsee>(); //New ArrayList
listA.add(new Personee("Bob", 29, 165)); //Add element
listA.add(new Personee("Alice", 25, 124)); 
listA.add(0, new Personee("Eve", 34, 136)); //This adds an element to the beginning of the list, index 0.

To remove an element:

listA.remove(personee1); //Remove by object
listA.remove(0); //Remove by index

To access/iterate over the list:

//access via index
Peronsee element0 = listA.get(0);
Personee element1 = listA.get(1);
Personee element3 = listA.get(2);


//access via Iterator
Iterator iterator = listA.iterator();
while(iterator.hasNext(){
  Personee personee = (Personee) iterator.next();
}


//access via new for-loop
for(Personee personee : listA) {
    System.out.println(personee.nom + "," + personee.age + "," + personee.poid);
}

2 Comments

from what i see . that's an arraylist . what should i do to get objects "personnes" list ?
@IslamBouderbala Updated my answer. The List interface can be made generic, (the List<Personee>) which limits the types that can be added and read from the list. In this case the List object can only handle objects of the Personee type.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.