why it shows NullPointerException?
<p>I have an object which is Student student; which has some fields like name,family,... I want to make a user name for this student and by clicking on the one button ,I want to show the student's user name on the console but it will show these sentences on the console(I have copied and pasted just a part of that)-----></p>

<p><em>java.lang.NullPointerException
        at registration.Students.nextUserName(Students.java:149)
        at ui.AddStudent.submit(AddStudent.java:254)
        at ui.AddStudent.jButton1ActionPerformed(AddStudent.java:186)
        at ui.AddStudent.access$000(AddStudent.java:24)
        at ui.AddStudent$1.actionPerformed(AddStudent.java:76)
        at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
        at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
        at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
        at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
        at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
        at java.awt.Component.processMouseEvent(Component.java:6038)
        at javax.swing.JComponent.processMouseEvent(JComponent.java:3265)</em>
         .
         .
         .</p>

<p>a part of my student class which also extends Human:</p>

<pre><code>    public String nextUserName() {
    StringBuffer user = new StringBuffer();
    SystemManagement students = new SystemManagement();
    String name1 = students.getName();
    char char1 = name1.charAt(0);
    String family1 = students.getFamily();
    char char2 = family1.charAt(1);
    setUserName(name1 + "." + family1);
    if (user.length() != 0 &amp;&amp; user.toString().equals(getUserName())) {
        char[] allowedCharacters1 = {char1, char2, '1', '.'};
        SecureRandom random = new SecureRandom();

        for (int i = 0; i &lt; user.length(); i++) {
            user.append(allowedCharacters1[random.nextInt(allowedCharacters1.length)]);
        }
        setUserName(user.toString());
    }

    return getUserName();

}

public String currentUserName() {
    return getUserName();
}

public static Students getInstance() {
    if (instance == null) {
        instance = new Students();
    }
    return instance;
}
</code></pre>

<p>my Human class:</p>

<pre><code> private String userName;

 public Human(String userName) {

    this.userName = Students.getInstance().nextUserName();
}

public void setUserName(String userName) {

    this.userName = userName;
}

public String getUserName() {
    return userName;
}
</code></pre>

<p>Thanks I have changed my netUserName() method to this but it return null instead of exception.</p>

<pre><code>public String nextUserName() {
  StringBuffer user = new StringBuffer();
  SystemManagement students = new SystemManagement();
  String name1 = students.getName();
  String family1 = students.getFamily();
  if(name1 !=null &amp;&amp; family1 !=null ) {
    char char1 = name1.charAt(0);  
    char char2 = family1.charAt(1);
    setUserName(name1 + "." + family1);
    if (user.length() != 0 &amp;&amp; user.toString().equals(getUserName())) {
      char[] allowedCharacters1 = {char1, char2, '1', '.'};
      SecureRandom random = new SecureRandom();
      for (int i = 0; i &lt; user.length(); i++) {
        user.append(allowedCharacters1[random.nextInt(allowedCharacters1.length)]);
      }
      setUserName(user.toString());
    }
  }
  return getUserName();
}
</code></pre>
