Click here to Skip to main content
15,916,432 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
TestConfused.java:
public class TestCase extends TestBase 
{
homeDetails homePage;
CarActions carsPage;
Map<Object, Object> map= new HashMap<Object, Object>();
@BeforeMethod
public void startdriver()   
     {

        startapplication();
        object= new ExcelReader();
        driver.get(url);
        driver.manage().deleteAllCookies();
        driver.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS);
        utility= new Utility();

    }
   
    }

    @Test(dataProvider= "data")
    public void CarTest(Map<Object, Object> car ) throws IOException, Exception {
         carsPage= new CarActions();
         System.out.println(car);
         carsPage.makeAndModel(car);
}
    @DataProvider(name = "data")  
    public static Object[][] CarTestData() throws IOException, Exception 
  {
        object= new ExcelReader();
        return object.getExcelData("Sheet1", fileDir);
  }
}

**CarAction.java:**

public class CarActions extends TestBase {
    WebDriver driver;

    CarPage car_pg = PageFactory.initElements(driver, CarPage.class);

public void makeAndModel(Map<Object, Object> map) throws IOException, Exception {
    
        String registerNumber= map.get("RegisterNo").toString();
        utility.elementCheck(car_pg.registerNo);
        car_pg.registerNo.click();
        car_pg.registerNo.sendKeys(registerNumber);
        car_pg.findVehicle.click();
    }
**carPage.java:**
    public class CarPage {

    @FindBy(how = How.XPATH, using = "//input[@id='RegistrationNumber']" )
    public WebElement registerNo;

ExcelReader.java: 

public class ExcelReader extends TestBase
 {
<pre>public  Object[][] getExcelData( String SheetName , String fileDir ) throws IOException {
    
       File srcFile = new File(fileDir);
       FileInputStream fis = new FileInputStream(srcFile);
       XSSFWorkbook wb = new XSSFWorkbook(fis);
       XSSFSheet sheet= wb.getSheet(SheetName);
       wb.close();
       int lastRowNum = sheet.getLastRowNum() ;
        int lastCellNum = sheet.getRow(0).getLastCellNum();
    
        Object[][] obj = new Object[lastRowNum][1];

        for (int i = 0; i < lastRowNum; i++) {
          Map<Object, Object> datamap = new HashMap<Object, Object>();
          for (int j = 0; j < lastCellNum; j++) {
        
            datamap.put(sheet.getRow(0).getCell(j).toString(), 
  sheet.getRow(i+1).getCell(j).toString());
          }
          obj[i][0] = datamap;

        }
        return  obj;
      }
}
**TestBase.java**

public class TestBase {

    public static WebDriver driver;
    public static Actions action;
    public static ExcelReader object;
    public static Utility utility;
    public String url= "https";
    protected static String fileDir= "\\InputHMap.xlsx";
    static String browserName;
    static String driverPath;
    public static Properties prop;
    public static WebDriverWait wait;

    public static void startapplication(){
        String browser="CHROME";
        if(browser.equals("CHROME"))
        {
            System.setProperty("Webdriver.chrome.driver","./chromedriver.exe");
            driver = new ChromeDriver();
        }
        else if(browser.equals("IE"))
        {
            System.setProperty("Webdriver.ie.driver","./geckodriver.exe");
            driver = new InternetExplorerDriver();
        }
         else
        {
            System.out.println("we do not support this browser");
        }
    
            driver.manage().window().maximize();
    }
}
**utility.java**
public class Utility {
    protected WebDriver driver = null;
    public void enterText(WebElement element, String value) {
        if(null != value)
        {
            //element.clear();
            element.sendKeys(value);
        }
    }



  public void elementCheck(WebElement element) throws IOException {

    WebDriverWait wait=new WebDriverWait(driver,20);
        wait.until(ExpectedConditions.visibilityOf(element));           
    }   
}
My problem is whenever the carpage.makeandmodel() is invoked in the @Test of TestConfused.java, a 
null pointer exception is encountered.



What I have tried:

I am trying my hands on Selenium test automation using TestNG as the test framework. For this, I have 
used a Page Object pattern to model each of the pages of the website that I am writing the test for.
Posted
Updated 5-Jul-20 4:58am

1 solution

This is one of the most common problems we get asked, and it's also the one we are least equipped to answer, but you are most equipped to answer yourself.

Let me just explain what the error means: You have tried to use a variable, property, or a method return value but it contains null - which means that there is no instance of a class in the variable.
It's a bit like a pocket: you have a pocket in your shirt, which you use to hold a pen. If you reach into the pocket and find there isn't a pen there, you can't sign your name on a piece of paper - and you will get very funny looks if you try! The empty pocket is giving you a null value (no pen here!) so you can't do anything that you would normally do once you retrieved your pen. Why is it empty? That's the question - it may be that you forgot to pick up your pen when you left the house this morning, or possibly you left the pen in the pocket of yesterdays shirt when you took it off last night.

We can't tell, because we weren't there, and even more importantly, we can't even see your shirt, much less what is in the pocket!

Back to computers, and you have done the same thing, somehow - and we can't see your code, much less run it and find out what contains null when it shouldn't.
But you can - and yout IDE will help you here. Run your program in the debugger and when it fails, it will show you the line it found the problem on. You can then start looking at the various parts of it to see what value is null and start looking back through your code to find out why. So put a breakpoint at the beginning of the method containing the error line, and run your program from the start again. This time, the debugger will stop before the error, and let you examine what is going on by stepping through the code looking at your values.

But we can't do that - we don't have your code, we don't know how to use it if we did have it, we don't have your data. So try it - and see how much information you can find out!
 
Share this answer
 
Comments
surya223344 6-Jul-20 0:04am    
My problem is whenever the carpage.makeandmodel() is invoked in the @Test of TestConfused.java, a null pointer exception is encountered. It successfully reads the data from excel. while debugging it shows a problem in click action in carAction.java(car_pg.registerNo.click();).please can you help me to solve this issue
OriginalGriff 6-Jul-20 2:09am    
I f I could, I would have already!
As I said, this needs your code running within your complete app, and probably also requires your data as well, and we just don;t have any access to either of those.

You need to use the debugger to find out what exactly is null, and work back from that to find out why - we cannot do that for you!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900