-2

Hello i am trying to add my Class's objects into an ArrayList with the objects attributes but i keep getting null pointers I am reading values from a txt file assigning them to the attributes and then put the object into the array list

here is my code

public class Users {
Scanner scan;
BufferedReader reader;
MainGui gui;
Login login;
String name;
String surname;
String dob;
String address;
String town;
String number;
String position;
ArrayList<Users> allUsers = new ArrayList<Users>();

 public Users(Login login){   
  this.login = login;
  open();
}
 public void open(){
  try {
    URL path = Users.class.getResource("records.txt");
    File f = new File(path.getFile());
    reader = new BufferedReader(new FileReader(f));
    scan = new Scanner(reader);
    } catch (FileNotFoundException e) {
    System.out.println("File not found");
    }
}

public void readRecords(){
 open();
 while(scan.hasNext()){
     Users user = new Users(login);
     user.name = scan.next();
     user.surname = scan.next();
     user.dob = scan.next();
     user.address = scan.next();
     user.address = addSpace(user.address);
     user.town = scan.next();
     user.number = scan.next();
     user.position = scan.next();
     allUsers.add(user);
 }
 scan.close();
 }
  public void printUsers(){
   readRecords();
     for(Users user : allUsers){
     System.out.println("" + user );
   }
   }
 }

I have included all the methods playing part in this in order for you to help me better.

Eilleen
  • 357
  • 1
  • 16
  • What is your main method? – OneCricketeer Feb 06 '16 at 16:18
  • 1
    Sorry, but your code is a mess. What do you want it to do? In your constructor, you are initializing unused local variables with `null` -- did you want to initialize the member variables instead? Afterwards, you call `open()`. So each time you are creating an object using `new Users()`, you are opening the file? And then in `readRecords` you are opening it again?? And then you are assigning the result of `scan.next()` to `this` instead of the newly created `user` object... – Ferdinand Beyer Feb 06 '16 at 16:22
  • You need to implement a toString method for your Users class, otherwise it will print like Users@389afb685 – OneCricketeer Feb 06 '16 at 16:25

4 Answers4

1

Change this part of code, you need to set the values in the user object you are creating.

Users user = new Users(login);
user.name = scan.next();
..
allUsers.add(user);

And it is better to create getter and setters for attributes.

And you don't need to set the values to null in constructor, java does that for you.

Munawir
  • 3,346
  • 9
  • 33
  • 51
awsome
  • 2,143
  • 2
  • 23
  • 41
  • i did exactly as you said and i get this in the console yuconz.Users@28ef6eaa yuconz.Users@53a7a372 – Eilleen Feb 06 '16 at 16:23
  • 1
    It's also worth mentioning that the model object shouldn't be doing this opening of files and prompting for input. It should only exist to store the variables – OneCricketeer Feb 06 '16 at 16:24
  • the System.out.println is there only for testing purpose to see if it work...eventually these variable will be displayed in a gui if i manage to make it work – Eilleen Feb 06 '16 at 16:24
  • if you want the sysout to print something better then you need to override the toString method of Users class. http://www.javapractices.com/topic/TopicAction.do?Id=55 – awsome Feb 06 '16 at 17:54
0

You're setting attributes in the instance that's executing the method, but you're adding the new (empty) object to your array list. Thus the list is full of empty Users instances.

zerobandwidth
  • 1,213
  • 11
  • 18
  • i know what you mean i tried allUsers.add(this); also but this printed 2 Users(the txt file has 2 "Users") with the same values in their attributes – Eilleen Feb 06 '16 at 16:32
  • You have it backwards. Your `readRecords()` needs to set the fields of `user` inside the loop. – zerobandwidth Feb 06 '16 at 16:34
0

This code is a bit of a mess - it's doing too much stuff in one class. You have a class called Users, which has a list of Users and the attributes of a user. Each of the users inside the list will also have a list of users.

It seems like you should have two classes, one called Users, and one called User (or similarly named classes)

The Users class has the list of s and is responsible for populating that list (by reading in the file). Then the User class just holds onto information for a particular user, and any operation pertaining to a single user.

Then, when you read in the records, you can get the scanner (as you currently are doing), pass it to the User, then that particular user object will populate it's own fields (similar to what awesome said)

AAG
  • 235
  • 1
  • 11
0

First I suggest you to put "System.out.print" statements while creating the USERS instance in read function to check if you are reading those values properly from the file. Second, Override toString() in Users class to display the values i.e.

    public String toString() {
        System.out.println("Name: "+name+", surname: "+surname+", DOB: "+dob+", Address: "+address+", Town: "+town+", Number: "+number+", and Position: "+position);
    }

Now your System.out.println("" + user ); should display values read from the file.

Sunil Dabburi
  • 1,442
  • 12
  • 18