我收到一个错误,指出在成员线程“main”java.lang.NullPointerException 中出现异常。init (Members.java:23) at Main.main(Main.java:9) 而我想要做的是使用 StringTokenizer 将来自文件输入的字符串存储到对象数组中。
在main中,第9行只是初始化对象,代码是:Members members = new Members("users.txt");
第 23 行是类成员是: users[nm].setId(st.nextToken());
我无法弄清楚错误是什么。
import java.io.*;
import java.util.*;
public class Members {
int nm = 0; //Number of members
User [] users = new User[100]; //Assuming max
number of user is 100
StringTokenizer st;
Scanner s1;
File f1;
String var1; //this string determines if it a standard or admin user;
String var2;
public Members(String fn) throws FileNotFoundException {
f1 = new File(fn);
s1 = new Scanner(f1);
while(s1.hasNext()) {
//System.out.println("true");
st = new StringTokenizer(s1.nextLine(),"/");
while(st.hasMoreTokens())
{
//System.out.print(((String)st.nextToken()));
users[nm].setId(st.nextToken());
users[nm].setPw(st.nextToken());
var1 = st.nextToken();
users[nm].setFn(st.nextToken());
users[nm].setLn(st.nextToken());
users[nm].setEmail(st.nextToken());
//System.out.print(st.nextToken() + " ");
if(var1.equals("Admin")) {
users[nm].setAdmin(true);
((Admin)users[nm]).setRank(st.nextToken());
}
if(var1.equals("Standard")) {
users[nm].setStandard(true);
while(st.hasMoreTokens())
{
((Standard)users[nm]).addCar(st.nextToken());
}
}
}
nm++;
System.out.println();
}
s1.close();
System.out.println("Number of members: " + nm);
}
白衣非少年
慕的地6264312
相关分类