Hey guys I'm trying to get back into java. Took a class in high school a couple years ago. Right now I'm working on a small project where I want to use an array of objects but I'm having a problem with initializing the objects. Here's kinda what I have right now.
import java.util.Scanner;
public class Test{
public static void main(String args[]){
Scanner s=new Scanner(System.in);
System.out.println("Welcome to Test. What is your name");
String ans=s.nextLine();
User[] people=new User[10];
people[1].initializeUser(ans);
people[1].printName();
}
}
I'm having a run time error where I try to initializeUser. "Exception in thread "main" java.lang.NullPointerException" Here's the code for class User if it's helpful.
public class User{
public String name;
public User(String x){
name=x;
}
public User intitializeUser(String x){
User y=new User(x);
return y;
}
}
and just a quick question relating to classes.. When should I use private instead of public? Also when am I supposed to use static for methods and variables? Thanks, guys