将 Hashtable 作为参数传递给方法会抛出空指针异常

发布于 2024-12-03 16:40:48 字数 1151 浏览 0 评论 0原文

我正在尝试将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

野却迷人 2024-12-10 16:40:48

我不知道你在哪里初始化d。这是导致 NPE 的原因,而不是 vars。也就是说,d 应该在 Recruiter 的构造函数中初始化。

I don't see where you're initializing d. That's causing the NPE, not vars. That being said, d should be initialized in the constructor of Recruiter.

我最亲爱的 2024-12-10 16:40:48

只需在使用前初始化 DataEntry d 即可。类似于:

d = new DataEntry();

在构造函数内部。

Just initialize DataEntry d before you use it. Something like:

d = new DataEntry();

inside the constructor.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文