java - Adding objects to array list -
this question has answer here:
- what nullpointerexception, , how fix it? 12 answers
hello trying add class's objects arraylist objects attributes keep getting null pointers reading values txt file assigning them attributes , put object array list
here 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 methods playing part in in order me better.
change part of code, need set values in user object creating.
users user = new users(login); user.name = scan.next(); .. allusers.add(user); and better create getter , setters attributes.
and don't need set values null in constructor, java you.
Comments
Post a Comment