2013-04-20 67 views
0

我正在使用getter,就像我總是這樣做(我認爲),它崩潰了程序,但我找不出原因!有誰知道我的錯誤在哪裏?Getter正在崩潰程序

其下方45行崩潰:


package edu.virginia.hotncolduva; 

import sofia.app.Screen; 

public class HistoricalLocation extends Screen { 
    private String name; 
    private double latitude; 
    private double longitude; 
    private double distance; 
    private String hist; 
    private boolean visited; 



    public HistoricalLocation(String name, double latitude, double longitude, double distance, 
      String hist, boolean visited) { 
     super(); 
     this.name = name; 
     this.latitude = latitude; 
     this.longitude = longitude; 
     this.distance = distance; 
     this.hist = hist; 
     this.visited = visited; 
    } 


    @Override 
    public String toString() { 
     return name; 
    } 

    public String getName() { 
     return name; 
    } 

    public double getLatitude() { 
     return latitude; 
    } 

    public double getLongitude() { 
     return longitude; 
    } 

    public double getDistance() { 
     return distance; 
    } 


    public void setDistance(double distance) { 
     this.distance = distance; 
    } 


    public String getHist() { 
     return hist; 
    } 


    public boolean getVisited() { 
     return visited; 
    } 


    public void hasVisited() { 
     visited = true; 
    } 




} 

這顯然不是整個項目,但這一切正在使用的代碼。它崩潰的

riceHall.getLatitude(); 

的logcat:

04-20 16:26:27.700: W/ActivityThread(27796): Application edu.virginia.hotncolduva can be debugged on port 8100... 
04-20 16:26:27.760: D/dalvikvm(27796): GC_FOR_ALLOC freed 131K, 4% free 7361K/7640K, paused 17ms, total 19ms 
04-20 16:26:27.760: I/dalvikvm-heap(27796): Grow heap (frag case) to 7.842MB for 539664-byte allocation 
04-20 16:26:27.790: D/dalvikvm(27796): GC_CONCURRENT freed 1K, 4% free 7886K/8168K, paused 5ms+2ms, total 29ms 
04-20 16:26:27.790: D/dalvikvm(27796): WAIT_FOR_CONCURRENT_GC blocked 24ms 
04-20 16:26:27.830: D/dalvikvm(27796): GC_FOR_ALLOC freed <1K, 4% free 7886K/8168K, paused 14ms, total 14ms 
04-20 16:26:27.830: I/dalvikvm-heap(27796): Grow heap (frag case) to 8.751MB for 955696-byte allocation 
04-20 16:26:27.850: D/dalvikvm(27796): GC_FOR_ALLOC freed 0K, 4% free 8819K/9104K, paused 17ms, total 17ms 
04-20 16:26:27.870: D/dalvikvm(27796): GC_CONCURRENT freed <1K, 4% free 8820K/9104K, paused 2ms+1ms, total 22ms 
04-20 16:26:27.930: D/AndroidRuntime(27796): Shutting down VM 
04-20 16:26:27.930: W/dalvikvm(27796): threadid=1: thread exiting with uncaught exception (group=0x40d12930) 
04-20 16:26:27.940: E/AndroidRuntime(27796): FATAL EXCEPTION: main 
04-20 16:26:27.940: E/AndroidRuntime(27796): java.lang.RuntimeException: Unable to start activity ComponentInfo{edu.virginia.hotncolduva/edu.virginia.hotncolduva.HotNColdUva}: java.lang.NullPointerException 
04-20 16:26:27.940: E/AndroidRuntime(27796): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180) 
04-20 16:26:27.940: E/AndroidRuntime(27796): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) 
04-20 16:26:27.940: E/AndroidRuntime(27796): at android.app.ActivityThread.access$600(ActivityThread.java:141) 
04-20 16:26:27.940: E/AndroidRuntime(27796): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234) 
04-20 16:26:27.940: E/AndroidRuntime(27796): at android.os.Handler.dispatchMessage(Handler.java:99) 
04-20 16:26:27.940: E/AndroidRuntime(27796): at android.os.Looper.loop(Looper.java:137) 
04-20 16:26:27.940: E/AndroidRuntime(27796): at android.app.ActivityThread.main(ActivityThread.java:5041) 
04-20 16:26:27.940: E/AndroidRuntime(27796): at java.lang.reflect.Method.invokeNative(Native Method) 
04-20 16:26:27.940: E/AndroidRuntime(27796): at java.lang.reflect.Method.invoke(Method.java:511) 
04-20 16:26:27.940: E/AndroidRuntime(27796): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 
04-20 16:26:27.940: E/AndroidRuntime(27796): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 
04-20 16:26:27.940: E/AndroidRuntime(27796): at dalvik.system.NativeStart.main(Native Method) 
04-20 16:26:27.940: E/AndroidRuntime(27796): Caused by: java.lang.NullPointerException 
04-20 16:26:27.940: E/AndroidRuntime(27796): at edu.virginia.hotncolduva.HotNColdUva.initialize(HotNColdUva.java:45) 
04-20 16:26:27.940: E/AndroidRuntime(27796): at java.lang.reflect.Method.invokeNative(Native Method) 
04-20 16:26:27.940: E/AndroidRuntime(27796): at java.lang.reflect.Method.invoke(Method.java:511) 
04-20 16:26:27.940: E/AndroidRuntime(27796): at sofia.internal.events.EventDispatcher$MethodTransformer.invoke(EventDispatcher.java:474) 
04-20 16:26:27.940: E/AndroidRuntime(27796): at sofia.internal.events.EventDispatcher.invokeTransformer(EventDispatcher.java:136) 
04-20 16:26:27.940: E/AndroidRuntime(27796): at sofia.internal.events.EventDispatcher.dispatch(EventDispatcher.java:109) 
04-20 16:26:27.940: E/AndroidRuntime(27796): at sofia.app.internal.ScreenMixin.invokeInitialize(ScreenMixin.java:561) 
04-20 16:26:27.940: E/AndroidRuntime(27796): at sofia.app.Screen.onCreate(Screen.java:186) 
04-20 16:26:27.940: E/AndroidRuntime(27796): at android.app.Activity.performCreate(Activity.java:5104) 
04-20 16:26:27.940: E/AndroidRuntime(27796): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080) 
04-20 16:26:27.940: E/AndroidRuntime(27796): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144) 
04-20 16:26:27.940: E/AndroidRuntime(27796): ... 11 more 
04-20 16:26:27.970: D/dalvikvm(27796): GC_CONCURRENT freed 797K, 11% free 8472K/9416K, paused 2ms+5ms, total 34ms 

回答

4

在方法makeHistoricalLocations中,您將新對象的引用分配給局部變量,而不是實例一。

public void makeHistoricalLocations() { 
    this.riceHall = new HistoricalLocation(
    ... 
+0

非常感謝! 9分鐘直到綠色複選標記! – Evorlor 2013-04-20 20:32:07

1

riceHallnull,因爲你從來沒有初始化它。

+0

我在makeHistoricalLocations()中調用它之前初始化它; – Evorlor 2013-04-20 20:30:13

+1

@SamuelKnox:不,你沒有。你初始化一個局部變量,也叫'riceHall'。 – CommonsWare 2013-04-20 20:30:40

1

看來你的變量riceHall爲空。我無法分辨,因爲你切斷了makeHistoricalLocations的實現,但我懷疑你是不是調用該方法,或者(更可能)創建方法本地版本riceHall,而不是將它設置在對象級別。