-4

I am working on a simulation and I get an error every time! I tried evreything but i cant find an answer. This is the code: Person.java:

package School;

import java.util.Random;

public class Person {
    private Random rng;
    private String Name;
    private String[] Friends;
    private int Hobby;
    private boolean IsDitch;
    public Person(String Name, String[] Friends, int Hobby, boolean IsDitch){
        int aof = rng.nextInt(20);
        this.Name = Name;
        this.Friends = Friends;
        Friends = new String[aof];
        this.Hobby = Hobby;
        if(rng.nextInt(10) == 0){
            IsDitch = true;
        }else{
            IsDitch = false;
        }
        this.IsDitch = IsDitch;
    }

    public boolean getIsDitch(){
        return IsDitch;
    }

    public int getMaxFriends(){
        return Friends.length;
    }

    public String getName(){
        return Name;
    }
}

Interaction.java:

package School;

public class Interaction {
    public static void main(String[] args){
        String[] g;
        g = new String[2];
        g[0] = "Natasha";
        g[1] = "Fill";
        Person mrt = new Person("Adam",g,2,false);
        System.out.println(mrt.getName());
    }
}

The error I am getting:

Exception in thread "main" java.lang.NullPointerException
    at School.Person.<init>(Person.java:12)
    at School.Interaction.main(Interaction.java:9)
Prerak Sola
  • 9,517
  • 7
  • 36
  • 67
sagiksp
  • 143
  • 1
  • 7
  • 2
    If you have to add "*Lorem Iposum*" (sic) to the end of your message to get it to post, then you should probably have listened to the message telling you that you're not posting enough useful information in your question. – Wai Ha Lee May 19 '15 at 13:36
  • It wouldn't let me comment otherwise. – sagiksp May 19 '15 at 16:04

2 Answers2

1

The rng is null, you use it in the constructor before instantiating it.

Change that to:

rng = new Random();  
int aof = rng.nextInt(20);    

You may even want to make rng static, depending on your exact needs.

peter.petrov
  • 38,363
  • 16
  • 94
  • 159
-1

I think it's because you don't initialize rng

Edit : oops too slow, but i agree peter.petrov

HRomain
  • 36
  • 1
  • 9