This discussion is archived
7 Replies Latest reply: May 19, 2013 12:38 PM by La VloZ RSS

String problem java.lang.NullPointerException

La VloZ Newbie
Currently Being Moderated
Hii all :)
How are u?? :)

I have a little a problem with String object in this class (You can say its a homework ^__^ ).
public class Personne {
     private String nom;
     private String prenom;
     private int age;
     
     public Personne(){
          this(null, null, 0);
     }
     
     public Personne(String nom, String prenom, int age){
          setNom(nom);
          setPrenom(prenom);
          setAge(age);
     }
     
     public void setNom(String nom){
          this.nom = new String(nom);
     }
     
     public void setPrenom(String prenom){
          this.prenom = new String(prenom);
     }
     
     public void setAge(int age){
          this.age = age;
     }
     
     public String getNom(){
          return this.nom;
     }
     
     public String getPrenom(){
          return this.prenom;
     }
     
     public int getAge(){
          return this.age;
     }
     
     public String toString(){
          System.out.print("Nom : " + this.getNom() + ", Penom : " + this.getPrenom() + ", Age : " + this.getAge() + " ");
          return null;
     }
}
When i call the class personne with the Personne() i get these errors in compiling-time :

Exception in thread "main" java.lang.NullPointerException
     at java.lang.String.<init>(Unknown Source)
     at Personne.setNom(Personne.java:18)
     at Personne.<init>(Personne.java:12)
     at Personne.<init>(Personne.java:8)
     at Main.main(Main.java:4) // The line wich i inisialize my object in my main method.

can someone help me??
Thank you alll ^__^

Edited by: La VloZ on 18 mai 2013 22:50

Edited by: La VloZ on 18 mai 2013 22:51

Edited by: La VloZ on 18 mai 2013 22:51

Edited by: La VloZ on 18 mai 2013 22:53

Edited by: La VloZ on 18 mai 2013 22:55
  • 1. Re: String problem java.lang.NullPointerException
    anotherAikman Explorer
    Currently Being Moderated
    You do not initialize your variable "nom". You need to assign it a value, e. g.
    String nom = new String("LaVloZ"); // *
    Until you do that, the reference is a null. null is nothing, you cannot work with it, except checking if a reference is == null.

    *String objects can be initialized just using
    String nom = "LaVloZ"; 
    but the new keyword is relevant to your problem. Look here:
    docs.oracle.com/javase/tutorial/java/javaOO/objectcreation.html| docs.oracle.com/javase/tutorial/java/javaOO/objectcreation.html


    Am I an idiot (rethorical question) or do links still not work?

    Edited by: anotherAikman on 18.05.2013 14:11
  • 2. Re: String problem java.lang.NullPointerException
    La VloZ Newbie
    Currently Being Moderated
    anotherAikman when i use :
    public void setNom(String nom){
         this.nom = nom;
    }
    Doesn't that copy the reference of nom to this.nom??!! if it just copy the reference it wouldn't create new object !!!!
    or it a particular case for Strings???!!

    Your link it works :)

    Edited by: La VloZ on 18 mai 2013 23:51

    Edited by: La VloZ on 18 mai 2013 23:56
  • 3. Re: String problem java.lang.NullPointerException
    baftos Expert
    Currently Being Moderated
    Why the error? Because you invoke the constructor String(String s), with s null. The constructor would produce a new String with the same character sequence as s, but if s is null, it cannot do this, so it throws NullPointerException. What else could it do? Without throwing an exception, a constructor has to create a new object, but there is no raw material, so to speak, for the creation. Think what would/could you do if you had to write such a 'copy constructor' for your own class and you would be passed null.

    Now, the String copy constructor is probably useless, as the javadoc hints. When you have a String already, you don't need to create an identical one, as the original will:
    - Not be garbage collected, if you keep a reference to it
    - Never be modified, as strings are immutable

    You are right that the code you show would 'copy the reference of nom to this.nom' and this is ok. It's exactly what you need, for the reasons above.
  • 4. Re: String problem java.lang.NullPointerException
    anotherAikman Explorer
    Currently Being Moderated
    La VloZ wrote:
    anotherAikman when i use :
    public void setNom(String nom){
         this.nom = nom;
    }
    Doesn't that copy the reference of nom to this.nom??!! if it just copy the reference it wouldn't create new object !!!!
    or it a particular case for Strings???!!
    Yes it does, but not if you (as stated in the first post) call the Personne() constructor, which calls the Personne(String, String, int) constructor with null parameters. The reference still points to null.

    You could use default values here, like "Quelqu'un" (c'est corrêt, oui?) but since a person will have a name, maybe there shouldn't be a constructor without arguments at all. Also, pay more attention to baftos' post than mine, he is a professional programmer. Most regular posters here are. I am a hobbyist.
  • 5. Re: String problem java.lang.NullPointerException
    baftos Expert
    Currently Being Moderated
    (c'est corrêt, oui?)
    Correct, comme en anglais:)
  • 6. Re: String problem java.lang.NullPointerException
    anotherAikman Explorer
    Currently Being Moderated
    Zut alors. :)

    Edited by: anotherAikman on 18.05.2013 20:31
  • 7. Re: String problem java.lang.NullPointerException
    La VloZ Newbie
    Currently Being Moderated
    anotherAikman et baftos Thank you (Merci) ;)
    You gave me the answer :)

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points