来自备用类的Java Accessing方法

时间:2017-08-10 09:58:03

标签: java class methods reference access

我有A类和B类,我想从B类的A类中访问一个方法但是它没有工作,我得到以下信息:

  

线程中的异常" main"显示java.lang.NullPointerException

导致此问题的特定部分是访问library(RSelenium) library(XML) remDr <- remoteDriver(remoteServerAddr= "192.168.99.100", port = 4445L, browserName='chrome') remDr$open() # login to access to protected data remDr$navigate("http://bit.d o") logbutton <- remDr$findElement("css selector", "#top_login_info a:nth-child(1)") logbutton$clickElement() user <- remDr$findElement('css selector', '#login_user_username') pass <- remDr$findElement('css selector', '#login_user_password') user$sendKeysToElement(list('test0001')) pass$sendKeysToElement(list('qwerty1234')) logb <- remDr$findElement('css selector', '.btn-primary') logb$clickElement() Sys.sleep(3) remDr$navigate('http://bit.d o/admin/url/http%3A%7C%7C2F%7C%7C2Fedition.cnn.com%7C%7C2F2017%7C%7C2F07%7C%7C2F21%7C%7C2Fopinions%7C%7C2Ftrump-russia-putin-lain-opinion%7C%7C2Findex.html') # getting data html <- htmlParse(remDr$getPageSource()[[1]]) clicks = xpathSApply(html,'//td//span[(((count(preceding-sibling::*) + 1) = 1) and parent::*)]') links = xpathSApply(html, '//td//a[(((count(preceding-sibling::*) + 1) = 2) and parent::*)]') text_links <- paste0(links) text_links # Opening text_links the result is a strange list of [1] "<pointer: 0x000000001bf11b30>" "<pointer: 0x000000001bf125b0>" [3] "<pointer: 0x000000001bf132b0>" "<pointer: 0x000000001bf15fb0>" [5] "<pointer: 0x000000001bf17bb0>" "<pointer: 0x000000001bf1abb0>" [7] "<pointer: 0x000000001bf1c230>"

ba

我不确定我做错了什么,因为我创建了私有字段并在main中初始化它。

B类:

ba.getBalance() >= LIM        
ba.debit(LIM);

A类:

 public class B {

        private A ba;
        private long balance;

        public B(long amount, A ba){
        }

        public boolean testCase(long amount){
           //..
        }

    public long getBalance(){
        return balance;
    }

主要

public class A {

     private long balance;

     public A(long amount){
    }

    public boolean debit(long amount){
        //.. simple arithmetic 
    }

    public long getBalance(){
        return balance;
    }

2 个答案:

答案 0 :(得分:0)

您忘记在构造函数中设置ba

    public B(long amount, A ba){
        balance = amount;
        this.ba = ba;
    }

答案 1 :(得分:0)

在B构造函数中添加此行 this.ba = ba。您没有为A分配任何值,因此它会抛出NPE