Why am I getting "java.lang.NullPointerException" w/ my ArrayList?

2017-03-02T15:36:36

I am making a class to deal with a friend list scenario using an ArrayList and I'm not sure what I have done wrong. The "java.lang.NullPointerException" occurred the moment I called the addFriend method and I can't seem to troubleshoot exactly why this is happening. Please give me some hints in the right direction!

public class Person {
    private String name; 
    private ArrayList<String> friends;


    public Person(String name) {
        this.name = name;
        this.friends = friends;
    }

    public String getName() {
        return this.name;
    }

    public void addFriend(String friend) {
        friends.add(friend);     
    }

    public boolean hasFriend(String name) {
        for(String friend : this.friends) {
            if(name.equals(friend)) {
                return true;
            }
        }
    return false;   
    }

    public String getFriends() {
        String stringOfFriends=friends.toString();
        return stringOfFriends;
    }

    public String unfriend(String friend) {
        if (friends.contains(friend)) {
            friends.remove(friend);
        }
    return friends.toString();    
    }



}

Copyright License:
Author:「hattic」,Reproduced under the CC 4.0 BY-SA copyright license with link to original source & disclaimer.
Link to:https://stackoverflow.com/questions/42549371/why-am-i-getting-java-lang-nullpointerexception-w-my-arraylist

About “Why am I getting "java.lang.NullPointerException" w/ my ArrayList?” questions

Why am i getting the exception error va.lang.ArrayIndexOutOfBoundsException why am i getting thisva.lang.ArrayIndexOutOfBoundsException why am i getting thisva.lang.ArrayIndexOutOfBoundsException w...
for(int i=0;i&lt;5;i++) { char ans = s.next().charAt(i); } I am getting a StringIndexOutOfBoundsException. Why it is happening?
I am trying to get NoUniqueBeanDefinitionException but i am not getting it any clue why i am not getting package com.example.demo; public interface IDateGen { } package com.example.demo; import...
Please tell me why i am getting ClassCastException in this case I have type casted , the source of B class to A as shown below , but why i am still getting ClassCastException here . public class A
In the following program: #include &lt;iostream&gt; using namespace std; int main(){ int i = 99; for(int i = 1; i &lt;= 10; i++) { cout &lt;&lt; i &lt;&lt; endl;
I have imported mysql data from my local machine to webserver through phpmyadmin and when I run my script I am getting this error Illegal mix of collations (utf8_general_ci,IMPLICIT) and (
I am getting an error in PrintF in the following java code. The Error Is : The method printf(String, Object[ ]) in the type PrintStream is not applicable for the arguments (String, int) The ...
Why I am getting the altitude as 0 while using CoreLocation framework?
I am working on FaceRecognition project. For that i am using BRISK algorithm from openbr. For that i have to train data using many number of images. I am using training command as below, br -alg...
I'm trying to understand to why I'm not getting 123.123 in the output. Should I be using the f instead of lf? #include &lt;stdio.h&gt; void main() { double foo = 123.123; int blah = 5432...

Copyright License:Reproduced under the CC 4.0 BY-SA copyright license with link to original source & disclaimer.