将 Hashtable 作为参数传递给方法会抛出空指针异常
我正在尝试将 Hashtable
作为参数传递到一个方法中,该方法本身将同一个表传递到另一个方法中:
public class Recruiter {
private WebDriver driver;
private DataEntry d;
private String password;
public Recruiter(WebDriver driver){
this.driver = driver;
}
public void createRecruiter(Hashtable<String, String> vars, String preferences[]){
d.enterText(vars);
driver.findElement(By.id("SAVE")).click();
}
Main:
Hashtable<String, String> vars = new Hashtable<String, String>();
vars.put("DOB","02 Mar 1983");
vars.put("SURNAME","Release");
vars.put("FIRSTNAME","David");
vars.put("COMPANYNAME","Test Inc");
Recruiter recruiter = new Recruiter(driver);
recruiter.createRecruiter(vars, preferences);
但是我一直在以下位置收到空指针异常:d.enterText(vars); 将
Hashtable
作为方法参数传递的正确方法是什么?
I am trying pass a Hashtable
as parameter into a method which itself passes that same table into another method:
public class Recruiter {
private WebDriver driver;
private DataEntry d;
private String password;
public Recruiter(WebDriver driver){
this.driver = driver;
}
public void createRecruiter(Hashtable<String, String> vars, String preferences[]){
d.enterText(vars);
driver.findElement(By.id("SAVE")).click();
}
Main:
Hashtable<String, String> vars = new Hashtable<String, String>();
vars.put("DOB","02 Mar 1983");
vars.put("SURNAME","Release");
vars.put("FIRSTNAME","David");
vars.put("COMPANYNAME","Test Inc");
Recruiter recruiter = new Recruiter(driver);
recruiter.createRecruiter(vars, preferences);
However I keep getting a null pointer exception at: d.enterText(vars);
what is the proper way to pass a Hashtable
as a method parameter?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不知道你在哪里初始化
d
。这是导致 NPE 的原因,而不是vars
。也就是说,d
应该在Recruiter
的构造函数中初始化。I don't see where you're initializing
d
. That's causing the NPE, notvars
. That being said,d
should be initialized in the constructor ofRecruiter
.只需在使用前初始化
DataEntry d
即可。类似于:在构造函数内部。
Just initialize
DataEntry d
before you use it. Something like:inside the constructor.