java.lang.NullPointerExceptionnull 指標問題

來源:互聯網
上載者:User

標籤:android   java.lang.nullpointe   null 指標問題   xxx已停止運行   


  對於一個Android新手而言遇見null 指標問題是在所難免的。比如在調試時一開程式就出現“很抱歉!’XXX‘已停止運行!“這是null 指標的一種表現。

  為什麼會出現null 指標?

  所謂null 指標異常,是因為用空(null)去調用屬性或方法。
null表示沒有這個對象,既然沒有這個對象,那麼去調用他的屬性和方法,就會報異常。
<--主要有以下幾種原因:
1、使用了未初始化的變數(雖然已經聲明)
2、使用了未初始化的對象(雖然已經聲明)
3、使用了關鍵字或已存在的類名作變數對象方法或類名。
當應用程式試圖在需要對象的地方使用 null 時,拋出該異常。
<--這種情況包括:
調用 null對象的執行個體方法。
訪問或修改null對象的欄位。
將null作為一個數組,獲得其長度。
將null作為一個數組,訪問或修改其時間片。
將null作為Throwable值拋出。
(以上幾點看明白,然後再查看你的代碼,肯定能找出來)

 既然知道為什麼錯了那就要去看錯在哪裡。看logcat

仔細看logcat就會發現錯誤了。比如

警告: Problem parsing stream - java.lang.NullPointerExceptionjava.lang.NullPointerException<span style="color:#ff0000;"> at com.semaphore.util.plist.PElementAdapter.get(PElementAdapter.java:25) at com.semaphore.util.plist.PList.get(PList.java:25) at com.semaphore.util.plist.PElementAdapter.getPath(PElementAdapter.java:31)</span> at com.semaphore.TinyLoader.findLibs(TinyLoader.java:76) at com.semaphore.TinyLoader.main(TinyLoader.java:37) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at com.exe4j.runtime.LauncherEngine.launch(Unknown Source) at com.exe4j.runtime.WinLauncher.main(Unknown Source)
從以上的第3、4、5行中很容易看出來PElementAdapter.java中的25、31行;PList中的第25行 等存在錯誤。具體錯誤自己找。

java.lang.NullPointerExceptionnull 指標問題

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.