为什么我不能在匿名内部类中初始化变量?

时间:2017-06-11 22:22:37

标签: java firebase nullpointerexception inner-classes anonymous-class

当我尝试从内部类初始化的对象中访问变量时,我遇到了NullPointerException的问题。但是在内部类中,它设置了所有元素,一切都很好。

public class FirebaseConnectionStream extends FirebaseConnection {


   private Object DownloadResult;

   public void publish(String path,Map<String,Object> map){
        try{
       DatabaseReference ref = database.getReference(path);
       ref.updateChildren(map);
     }
    catch(Exception e){
        e.printStackTrace();
    }
 }
 public <T> PortfolioInfo download(String path,final T object){

    try{
        DatabaseReference ref = database.getReference(path);
        ref.addValueEventListener(new ValueEventListener(){

            @Override
            public void onCancelled(DatabaseError arg0) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onDataChange(DataSnapshot arg0) { 
                try{
                DownloadResult = arg0.getValue(object.getClass());
                PortfolioInfo p = (PortfolioInfo) DownloadResult;
                System.out.println(p.getId()); // This line here works?
                }
                catch(Exception e){
                    e.printStackTrace();
                }
            }   
        });
    }
    catch(Exception e){
        e.printStackTrace();
    }
    return (PortfolioInfo) DownloadResult; // But this one does not?

    }

}

1 个答案:

答案 0 :(得分:1)

$html = 'SOME TEXT HERE FOR THE CURRENT PDF'; include ("PDF/mpdf60/mpdf.php"); $mpdf = new mPDF('c', 'A4-L','','' , 0, 0, 0, 0, 0, 0); $mpdf->WriteHTML(utf8_encode($html)); $mpdf->AddPage(); $mpdf->SetImportUse(); $file = './best.pdf'; $pagecount = $mpdf->SetSourceFile($file); for ($i=1; $i<=$pagecount; $i++) { $import_page = $mpdf->ImportPage($i); $mpdf->UseTemplate($import_page); if ($i < $pagecount) $mpdf->AddPage(); } ob_end_clean(); $mpdf->Output("title" . date('d-m-Y') . ".pdf",'D'); exit; 中的方法是异步的,当您尝试返回ValueEventListener时,DownloadResult将不会被调用,因此onDataChange将为空。