简体   繁体   English

在主方法中获取NullPointerException

[英]Getting a NullPointerException in main method

I'm trying to read the value from the XML. 我正在尝试从XML读取值。 Then, in the browser, I want to login to the Gmail application. 然后,在浏览器中,我要登录到Gmail应用程序。 There I'm getting NullPointerException . 那里我得到NullPointerException

The code used in Eclipse is as follows: Eclipse中使用的代码如下:

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)
private static WebDriver driver;
....
driver.findElement(By.id("1001")).sendKeys(url);

You access driver but never initialize it. 您访问driver但从不对其进行初始化。

That's what gives you the NullPointerException . 那就是给你NullPointerException

Initialize the driver . 初始化driver For eg: 例如:

If you are using firefox browser try 如果您使用的是Firefox浏览器,请尝试

WebDriver driver = new FirefoxDriver();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 NullPointerException在actionPerformed方法中但不在main中吗? - NullPointerException in actionPerformed method but not in main? 实现的方法获取NullPointerException - Implemented method getting NullPointerException 在TestNG中为方法调用获取NullPointerException - Getting NullPointerException for method Invocation in TestNG 从主调用方法NullPointerException - Calling method from main throws NullPointerException 如果在main()方法中未处理NullPointerException,会发生什么情况? - What happens if NullPointerException is not handled in main() method? 在与main方法相同的文件中调用方法时出现NullpointerException - Nullpointerexception when calling a method in the same file as the main method 当我尝试在主方法之外调用方法时发生NullPointerException - NullPointerException when I try to call a method outside my main method 在Controller JavaFX的initialize方法中获取NullPointerException - Getting NullPointerException in initialize method of Controller JavaFX 为什么我为此btree方法获取NullPointerException? - Why i am getting NullPointerException for this btree method? NullPointerException在@Produces方法中获取bean类名称 - NullPointerException getting bean class name in @Produces method
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM