亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

帶字典和空指針的范圍

帶字典和空指針的范圍

慕斯709654 2023-04-13 15:47:10
HashMap我在 FX 程序的啟動方法中暫時顯示信息時遇到問題。public class stack extends Application{    String mCurrentLocation; // Store current location    // Map to store name as keys and easting and northing as values    HashMap<String, List <Double>> dict = new HashMap<>();    public static void main(String[] args) {        stack mainObject = new stack();        mainObject.run();// Method where the program is built        launch(args);    }    public void start(Stage primaryStage) throws Exception{        System.out.println("In in startFX: "); // debugging only        System.out.println(dict);           // debugging only        // More fx code    }    private void run(){        System.out.println("In main");        System.out.println(dict);        //Read Northing and Easting input received from a txt file        try{            Scanner scanner = new Scanner(new             File("NorthingAndEastings.txt"));            while(scanner.hasNext()){                List<Double> coordinates = new ArrayList<>();                String name = scanner.next(); // key                Double easting = scanner.nextDouble();                coordinates.add(easting); // list(0)                Double northing = scanner.nextDouble();                coordinates.add(northing); //list(1)                this.dict.put(name, coordinates);             }        scanner.close();        } catch(FileNotFoundException e){            System.out.println("Sorry, could not open"             + "'NorthingAndEastings.txt' for reading. Stopping");            System.exit(-1);        }        // For debugging only        for (Map.Entry me : dict.entrySet()){            System.out.println("Name: "+me.getKey() + " Loc: " +                          me.getValue());        }    }}在run()首先在 main 中調用的方法中,HashMap成員變量是從文本文件中填充的。但是當代碼轉到我想顯示HashMap項目的 FX 時,字典是空的。我得到{};
查看完整描述

2 回答

?
楊__羊羊

TA貢獻1943條經驗 獲得超7個贊

launch類中的方法Application將啟動堆棧類的一個新實例,該實例將有一個空的dict. 如果您希望啟動的應用程序保留填充的值而dict不是創建dictstatic map填充您的dictin start 方法。

希望這可以幫助。


查看完整回答
反對 回復 2023-04-13
?
繁星淼淼

TA貢獻1775條經驗 獲得超11個贊

我不確定,但我認為問題可能是由stack mainObject = new stack();- 您可能正在dict為您認為在start.



查看完整回答
反對 回復 2023-04-13
  • 2 回答
  • 0 關注
  • 135 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號