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

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

變量 c 可能尚未初始化

變量 c 可能尚未初始化

弒天下 2022-08-17 17:03:16
我有這段代碼:public static void main (String[] args) {    Config c;// = null;    try {      c = new Config();    } catch (Exception e) {        System.err.println("Error while parsing/reading file: " + e.getMessage());        System.exit(-1);    }       final NetworkReporter np = new NetworkReporter(c.getValues().serverIP, c.getValues().serverPort, (short)(c.getValues().checkInterval * c.getValues().checksPerReport));    IdleChecker idleChecker = new IdleChecker(c.getValues().checkInterval, c.getValues().checksPerReport, c.getValues().idleSensitivity, new IdleChecker.reportFunction() {        public void report() {            np.report();        }       });     idleChecker.start();}當我編譯這段代碼時,我得到:[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.0:compile (default-compile) on project FanstisTime: Compilation failure[ERROR] /home/amitg/Programming/Projects/FantisTime/FantisTime/src/main/java/com/amitg/fantistimeclient/Client.java:[13,54] variable c might not have been initialized我確實理解這意味著什么,事實上 - 它將始終被初始化(因為如果無法初始化,程序將退出)。我必須在那里嘗試捕獲,因為會引發一些異常。我嘗試在那里使用,它給了我這個:variable c might not have been initializenew Config()Config c = null;[ERROR] Failed to execute goal org.codehaus.mojo:exec-maven-plugin:1.6.0:java (default-cli) on project FanstisTime: An exception occured while executing the Java class. null: NullPointerException -> [Help 1]你知道我能做些什么來解決這個問題嗎?謝謝!
查看完整描述

3 回答

?
慕俠2389804

TA貢獻1719條經驗 獲得超6個贊

System.exit(-1)

不保證您的程序會停止。如果您有某種關機掛鉤,或者您正在進行流操作,則可以阻止它。因此,編譯器會引發錯誤。


您可能希望讓 逃逸當前圖層。Exception


Config c;


try {

    c = new Config();

} catch (final Exception e) {

    System.err.println("Error while parsing/reading file: " + e.getMessage());

    throw new YourCustomRuntimeException(e);

}


c.whatever();


查看完整回答
反對 回復 2022-08-17
?
海綿寶寶撒

TA貢獻1809條經驗 獲得超8個贊

編譯器不知道如果程序無法初始化,它將退出。只需將其余代碼移動到 try 中即可。


public static void main (String[] args) {

    try {

      Config c = new Config();

      final NetworkReporter np = new NetworkReporter(c.getValues().serverIP, c.getValues().serverPort, (short)(c.getValues().checkInterval * c.getValues().checksPerReport));

      IdleChecker idleChecker = new IdleChecker(c.getValues().checkInterval, c.getValues().checksPerReport, c.getValues().idleSensitivity, new IdleChecker.reportFunction() {

        public void report() {

            np.report();

        }   

      }); 

      idleChecker.start();

    } catch (Exception e) {

        System.err.println("Error while parsing/reading file: " + e.getMessage());

        System.exit(-1);

    }

}


查看完整回答
反對 回復 2022-08-17
?
揚帆大魚

TA貢獻1799條經驗 獲得超9個贊

“事實上 - 它將始終被初始化(因為如果無法初始化,程序將退出)。

編譯器如何知道這一點?此外,如果它總是被初始化,那么為什么要費心使用a呢?你的邏輯有一個缺陷。try-catch

您可以將其余代碼移動到塊內。這樣就行了。try


查看完整回答
反對 回復 2022-08-17
  • 3 回答
  • 0 關注
  • 142 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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