-1

I'm trying to read the value from the XML. Then, in the browser, I want to login to the Gmail application. There I'm getting NullPointerException.

The code used in Eclipse is as follows:

import java.io.File;
import java.io.IOException;
import java.util.Properties;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;


public class A {

    private static WebDriver driver;
    Properties p= new Properties();
    String url=p.getProperty("url");

    public static void main(String[] args) throws SAXException, IOException, ParserConfigurationException {

        A a = new A();
        String url = a.readXML("logindetails","url");
        String username = a.readXML("logindetails","username");
        String password = a.readXML("logindetails","password");
        System.out.println(url);
        System.out.println(username);
        System.out.println(password);
        //use username for webdriver specific actions
        driver.findElement(By.id("1001")).sendKeys(url);
    }

    public String readXML(String searchelement,String tag) throws SAXException, IOException, ParserConfigurationException{
        String ele = null;
        File fXmlFile = new File("D://NewFile.xml");
        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
        Document doc = dBuilder.parse(fXmlFile);

        doc.getDocumentElement().normalize();

        NodeList nList = doc.getElementsByTagName(searchelement);


        Node nNode = nList.item(0);

        if (nNode.getNodeType() == Node.ELEMENT_NODE) {
            Element eElement = (Element) nNode;
             ele=eElement.getElementsByTagName(tag).item(0).getTextContent();

        }
        return ele;
    }

}

The output is:

www.gmail.com
test 
test123
Exception in thread "main" java.lang.NullPointerException
at A.main(A.java:33)
blalasaadri
  • 5,990
  • 5
  • 38
  • 58
testing
  • 1,736
  • 15
  • 46
  • 75
  • possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – Dennis Meng Oct 09 '14 at 06:17

2 Answers2

2
private static WebDriver driver;
....
driver.findElement(By.id("1001")).sendKeys(url);

You access driver but never initialize it.

That's what gives you the NullPointerException.

Eran
  • 387,369
  • 54
  • 702
  • 768
1

Initialize the driver. For eg:

If you are using firefox browser try

WebDriver driver = new FirefoxDriver();
Deb
  • 2,922
  • 1
  • 16
  • 32
  • It may be a `FirefoxDriver`. Or a `ChromeDriver`. Or a `HtmlUnitDriver`. Or any implementation of `WebDriver` really. – blalasaadri Oct 08 '14 at 08:11
  • getting error as follows: JavaScript error: chrome://browser/content/urlbarBindings.xml, line 677: aUrl is undefined – testing Oct 08 '14 at 08:26
  • Maybe this is firefox and Selenium's version issue. Maybe this will help you. Go through this link https://groups.google.com/forum/#!topic/selenium-users/yX5i1A1fRd0 – Deb Oct 08 '14 at 09:04