-1

Please, help to understand what is wrong in next code, since I have pretty modest knowledge in Java generics. Can't find out why I getting pageData is null in class BPage, when in APage it's fine. Thanks in advance. I've simplified the excample classes to several rows:

public abstract class PageData {
}

public abstract class Page<T extends PageData> {
    protected T pageData;
    public Page(T pageData) {
        this.pageData = pageData;
    }
}

public class APageData extends PageData {
    public final String locator = "//*[@id=\"id_1\"]";
}
public class APage<T extends APageData> extends Page<APageData> {
    public APage(T pageData) {
        super(pageData);
    }

    public void getLocator() {
        System.out.println(pageData.locator);
    }
}


public class BPageData extends APageData {
    public final String locator = "//*[@class=\"class_1\"]";
}
public class BPage extends APage<BPageData> {
    public BPage(BPageData pageData) {
        super(pageData);
    }
}


APage aPage = new APage(new APageData());
aPage.getLocator();  // locator found, OK

BPage bPage = new BPage(new BPageData());
bPage.getLocator();  //pageData  NullPointerException
  • 1
    This code doesn't compile. You'd have to invoke the constructor of `APage` from the constructor of `BPage`. Please post a [mcve]. – Andy Turner Feb 27 '18 at 19:33
  • yes, sorry for this mistake, but it's not the reason, I've mistaken when typing from the phone. I've corrected the code. Of course I call super first – Alan Menken Feb 27 '18 at 19:44

1 Answers1

0

You need to tell the super what to do

public class BPage extends APage<BPageData> {
    public BPage(BPageData pageData) {
        super(pageData);
        //this.pageData = pageData;
    }
}
bcr666
  • 2,157
  • 1
  • 12
  • 23