我正在编写一个应用程序,当用户输入文件所在的URL地址时,该应用程序从URL读取文件。它将读取数据值,然后将数据值输入到每个类对象。当我写setText命令(在while循环之外)时,当我输入e1.getName()时会出错。它要求我初始化e1。我这样做了,但是当我运行应用程序时,getName()显示为null。我想要员工的名字。所以,首先是这个类是什么样的:
public class Employee {
private String name;
private double salary;
private String id;
private String office;
private String extension;
private int yearsofservice;
//default constructor
Employee(String string, double d, String string2, String string3, String string4, int i)
{
}
//copy constructor
Employee(Employee eData)
{
name = eData.name;
salary = eData.salary;
id = eData.id;
office = eData.office;
extension = eData.extension;
yearsofservice = eData.yearsofservice;
}
//Define the methods
public String getName()
{
//Return the object's name
return name;
}
public void setName(String n)
{
//set the object's name to the given name
name = n;
}
public double getSalary()
{
return salary;
}
public void setSalary(double s)
{
salary = s;
}
public String getId()
{
return id;
}
public void setId(String ID)
{
id = ID;
}
public String getOffice()
{
return office;
}
public void setOffice(String o)
{
office = o;
}
public String getExtension()
{
return extension;
}
public void setExtension(String e)
{
extension = e;
}
public int getYearsOfServ()
{
return yearsofservice;
}
public void setYearsOfServ(int yos)
{
yearsofservice = yos;
}
}
其次,这是MainActivity中的代码或app的代码:
public void displayEmployees(View view)
{
EditText edt1;
TextView tv;
String urlfile;
//Declare three references to Employee object
Employee e1;
Employee e2;
Employee e3;
//Create reference for EditText and TextView
edt1 = (EditText) findViewById(R.id.edit_file);
tv = (TextView) findViewById(R.id.text_main);
//Store the URL address when user input it and assign the input
//to variable (reference) urlfile
urlfile = edt1.getText().toString();
//Use try/catch to check if the file opening on the web succeed
try
{
//Create an URL object that read urlfile
URL file_url = new URL(urlfile);
//try to open the file from the web
Scanner fsc = new Scanner(file_url.openStream());
//As long as there is data to read, continue
//reading the file
while(fsc.hasNext())
{
//tv.append("\n" + fsc.nextLine());//This is for testing to display data
//Create three Employee objects and input the data values
//from urlfile to each object
e1 = new Employee(fsc.nextLine(), Double.parseDouble(fsc.nextLine()),
fsc.nextLine(), fsc.nextLine(), fsc.nextLine(),
Integer.parseInt(fsc.nextLine()));
e2 = new Employee(fsc.nextLine(), Double.parseDouble(fsc.nextLine()),
fsc.nextLine(), fsc.nextLine(), fsc.nextLine(),
Integer.parseInt(fsc.nextLine()));
e3 = new Employee(fsc.nextLine(), Double.parseDouble(fsc.nextLine()),
fsc.nextLine(), fsc.nextLine(), fsc.nextLine(),
Integer.parseInt(fsc.nextLine()));
//e1.setOffice(Integer.parseInt(data));
//...Continue with other class objects with their respective set method
}
tv.setText("The name of the employee: " + e1.getName()); //The error is here!
}
catch(IOException e)
{
tv.setText("Error: Invalid URL Address or File Does Not Exist");
}
}
}
最后,文件看起来像这样(注意:每个值都在一个单独的行中): 莎莉史密斯 086244 100000.00 Ramsey Hall 3814 9976 9 露西拜耳 288567 39500.57 Cullimore Hall 100 6000 1 威廉哈里斯 732241 120000.00 GITC 165 7533 4
答案 0 :(得分:0)
fsc可能没有下一个元素(while循环不会启动e1)。写System.out.println(“text”);在while循环中并告诉它是否被调用。
答案 1 :(得分:0)
您在while循环中使用的Employee
构造函数不会对您提供的数据执行任何操作。因此,您永远不会在Employee对象上获得任何名称
public Employee(String name , double salary, String id, String office, String extension, int yearsofservice)
{
this.name = name;
this.salary = salary;
this.id = id;
this.office = office;
this.extension = extension;
this.yearsofservice = yearsofservice;
}