1

I have a class called ReqOrderImpCode with the following code inside it:

`public class ReqOrderImpCode {
    WebDriver driver;
    WebDriverWait wait;
    PrintStream out;
    int maxim = 0;
    boolean bcase = true;
//Constructor
    public ReqOrderImpCode(WebDriver driver, WebDriverWait wait, PrintStream out) throws FileNotFoundException {
        this.driver = driver;
        this.wait = wait;
        this.out = out;
    }
// Start testing, initialize the page
    public void page() {
        driver.get("http://google.com");
        driver.manage().window().maximize();
    }
//Access to methods
    Methods m = new Methods(driver, wait, out);

public void checkLoginAndProceed() throws FileNotFoundException {
        String pageTitle = driver.getTitle();
        if (pageTitle.equals("GAS")) {
            m.sendKeys(xUsername, "ftalexiuc");
            m.sendKeys(xPassword, "1freedom1");
            m.click(xLogin);
        } 
}`

The methods class which i am using to call the methods above looks like this:`

public class Methods {

    //Initialization
    WebDriver driver;
    WebDriverWait wait;
    PrintStream out;

    //Constructor
    public Methods (WebDriver driver, WebDriverWait wait, PrintStream out) throws FileNotFoundException {
        this.driver = driver;
        this.wait = wait;
        this.out = out;
        }
    //Method for clicking
    public void click(String s){
        WebElement element = wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath(s)));
        element.click();
        }

    //Method for SendKeys
    public void sendKeys(String s, String t){
        WebElement element = wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath(s)));
        element.sendKeys(t);
        }
}`

The problem is that the m.sendKeys() method from the first class is returning NullPointerException error. How should i write this for this error to be avoided and my code to run as intended?

Tudor
  • 199
  • 3
  • 14
  • 1
    `m` is initialized before you assign the values to the fields `driver`, `wait` and `out`. Initialize `m` in the constructor of `ReqOrderImpCode`. – Andy Turner Sep 13 '16 at 09:41
  • How exactly is the answer to that question helping me here? ... i don't understand how my code should be changed from that post, that is why i asked the question in the first place... – Tudor Sep 13 '16 at 09:41
  • Tried that, now everywhere i use "m" i get :"m cannot be resolved". i have written `//Constructor public ReqOrderImpCode(WebDriver driver, WebDriverWait wait, PrintStream out) throws FileNotFoundException { this.driver = driver; this.wait = wait; this.out = out; Methods m = new Methods(this.driver, this.wait, this.out); }` – Tudor Sep 13 '16 at 09:43
  • I said *initialize `m` in the constructor*, not declare it in the constructor. – Andy Turner Sep 13 '16 at 09:43
  • Thank you dude, it worked! Can you please explain (at least briefly) what is the idea behind your answer? Like, why is it working now, what is happening in the code? – Tudor Sep 13 '16 at 09:46
  • When you declare and initialize a field, the initialization is basically prepended to the constructor, so you read the `driver` field before you've set its value; as such, you get the default value. – Andy Turner Sep 13 '16 at 09:47
  • But why was m initialized before i was assigning the values? I thought the constructor is the first thing to run when you create an object of that respective class. As such, i was thinking that the initialization of m comes after the constructor is called. Why is the program running the m initialization first? – Tudor Sep 13 '16 at 09:52
  • "Why is the program running the m initialization first?" because that's how Java always does it. Instance initializers are run before constructors. Or, more precisely, the bytecode for instance initializers is prepended to all constructors. – Andy Turner Sep 13 '16 at 09:53
  • Thanks man! I know i am asking what seems to be "dumb" questions, but this is how i am learning as i am new to Java. – Tudor Sep 13 '16 at 09:56
  • "Or, more precisely, the bytecode for instance initializers is prepended to all constructors" Actually, it's just all constructors which don't explicitly invoke another constructor in the same class, using `this(...);`. And the code is inserted after the call to a superconstructor. – Andy Turner Sep 13 '16 at 10:01
  • Of course, the superconstructor has to be the first thing called inside a constructor right? Otherwise you have complilation error:D – Tudor Sep 13 '16 at 11:06

0 Answers0