使用getter时返回null的值

kaw*_*awa 1 java null latitude-longitude getter-setter

我试图理解我在这里做错了什么,但它对我来说真的没有意义.我有一个类calleld LatLongBean在这里我试图解析XML提要.

我有一个方法,我做所有的逻辑.我有一些吸气剂和二传手.

看起来安装员工作正常,但吸气剂不起作用.

这是LatLongBean:

public class LatLongBean {

    private String lat;
    private String lng;
    private String address;
    private String url = "http://maps.googleapis.com/maps/api/geocode/xml?address=";

    public void LatLongBean(String address, String lat, String lng) throws ParserConfigurationException, SAXException, IOException {
        this.address = address;
        this.lat = lat;
        this.lng = lng;

        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
        Document doc = dBuilder.parse(new URL(url + address + "&sensor=false").openStream());
        doc.getDocumentElement().normalize();

        NodeList nodes = doc.getElementsByTagName("location");

        for (int i = 0; i < nodes.getLength(); i++) {
            Node node = nodes.item(i);

            if (node.getNodeType() == Node.ELEMENT_NODE) {
                Element element = (Element) node;
                lat = getValue("lat", element);
                lng = getValue("lng", element);
            }
        }
    }

    private static String getValue(String tag, Element element) {
        NodeList nodes = element.getElementsByTagName(tag).item(0).getChildNodes();
        Node node = (Node) nodes.item(0);
        return node.getNodeValue();
    }

    public String getLat() {
        System.out.println(lat);
        return lat;
    }

    public String getLng() {
        System.out.println(lng);
        return lng;
    }

    public void setAddress(String address) {
        this.address = address;
    }
}
Run Code Online (Sandbox Code Playgroud)

这就是我使用这个类的方法:

LatLongBean latLong1 = new LatLongBean();
latLong1.setAddress("Amsterdam");
latLong1.getLat();
latLong1.getLng();
Run Code Online (Sandbox Code Playgroud)

getter返回null!

当我使LatLongBean方法不为void并且使用它作为构造函数时,它就像一个魅力:

LatLongBean latLong1 = new LatLongBean("Amsterdam");
latLong1.getLat();
latLong1.getLng();
Run Code Online (Sandbox Code Playgroud)

有人可以帮帮我吗?

Thanx提前!

san*_*hat 5

您没有调用public void LatLongBean(String address, String lat, String lng) 方法,该方法执行解析和设置值作业.

是的,这是一种方法.如果您希望上面是构造函数,则应该从中删除返回类型 void.如下

public LatLongBean(String address, String lat, String lng) {
     // your constructor logic
} 
Run Code Online (Sandbox Code Playgroud)

并调用它

LatLongBean latLong1 = new LatLongBean("http://youraddress.com/xml","a","b");
latLong1.setAddress("Amsterdam");
latLong1.getLat();
latLong1.getLng();
Run Code Online (Sandbox Code Playgroud)

编辑:也在你应该是构造函数代码更改下面这样的行

       if (node.getNodeType() == Node.ELEMENT_NODE) {
            Element element = (Element) node;
            this.lat = getValue("lat", element); // not using this will not write the Document read value to the class variable lat and lng, instead it writes to the parameter itself
            this.lng = getValue("lng", element);
        }
Run Code Online (Sandbox Code Playgroud)

  • @kawa请看我的新编辑,你应该在阅读文件的价值时使用`this.lat` (2认同)