-3

NULL POINTER Error message is displayed when running my programme. code is attached but cannot seem to find the problem.

Exception in thread "main" java.lang.NullPointerException at coursework2.Assignment.main(Assignment.java:330

  lastName = last;
  firstName = first;
  email = pemail;
  dob = pdob;
  town = ptown;
  job = pjob;
  school = pschool;
  status = pstatus;
  //String[] friendArray;
  friendArray = new String[]{lastName,firstName,email,dob,town,job,school,status};



  String searchName;
  int input;
  int index;

  SimpleDateFormat dateFormat= new SimpleDateFormat ("dd/mm/yyy");
  SimpleDateFormat timeFormat= new SimpleDateFormat ("hh:mm");
  SimpleDateFormat birthdayFormat= new SimpleDateFormat ("dd/mm");

  Date today = new Date();

  dateFormat.format(today);           //get current date time with date()
  timeFormat.format(today);           //get current date time with calender ()
  birthdayFormat.format(today);       //get current
  Calendar.getInstance();

  //int length = friendList.length;
  int maxSize = 100;                                                    // array size
  User arr;                                                         // reference to array
  arr = new User(maxSize);                                         // create the array
  User [] userList = new User[500];

  friendArray = new String[]{lastName};
  userList[0].setFriendArray("Henly");
  userList[0].setFriendArray("White");

  userList[1].setFriendArray("Davidson");
  userList[1].setFriendArray("White");

  userList[2].setFriendArray("Browne");
  userList[2].setFriendArray("Gomez");

  userList[3].setFriendArray("Browne");
  userList[3].setFriendArray("Reid");

  userList[4].setFriendArray("White");
  userList[4].setFriendArray("Henly");

  userList[5].setFriendArray("Myles");
  userList[5].setFriendArray("Browne");

  userList[6].setFriendArray("Davidson");
  userList[6].setFriendArray("Browne");

  userList[7].setFriendArray("White");
  userList[7].setFriendArray("Gomez");

  userList[8].setFriendArray("Reid");
  userList[8].setFriendArray("Scott");
  • No code, please copy and past it. – Kyte Jun 19 '14 at 10:56
  • Yes, that line 330 is causing the problem. Please fix it. – Suresh Atta Jun 19 '14 at 10:58
  • It wont allow me to post all my code! However Line 30 is friendArray = new String[]{lastName}; userList[0].setFriendArray("Henly"); userList[0].setFriendArray("White"); – user3592094 Jun 19 '14 at 11:02
  • You didn't initialize userList[0], so it is null. You have to do something like: `userList[0] = new User();`. And you have to do this for each element in the `userList` array. – eternay Jun 19 '14 at 11:09
  • My userList array consists of several : arr.insert("White", "Emma", "white92@hotmail.com", "25/05/1992", "Belfast", "Student", "Victoria College", "Finally the weekend!!", friendArray); // insert items – user3592094 Jun 19 '14 at 11:18
  • Great eternay! It is not working thanks for your help! – user3592094 Jun 19 '14 at 11:21
  • possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – Josef E. Jun 19 '14 at 17:39

1 Answers1

0

This code creates an array with null in every slot:

User [] userList = new User[500];

a couple of lines down, you try to use userList[0]:

userList[0].setFriendArray("Henly");

...but userList[0] is still null. You have to create a User object and assign it to that entry, e.g.:

userList[0] = new User(/*...any necessary arguments here...*/);

Then you can do this:

userList[0].setFriendArray("Henly");

...because userList[0] is no longer null.

Obviously, this is true for indexes 1, 2, 3, and so on.


Side note: Elsewhere, you have:

User arr;

I would strongly advise that you don't call something arr that isn't an array.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875