-2

I write the program that shows a balloonView on the map and when I run the program, the program has stopped. I read the LogCat and got a NullPointerException, but I don't know where the problem is and on which line.

location.java

 public class Location extends MapActivity {
private FrameLayout linear;
private MapView map;
private MapController controller;
private EditText text_location;
GeoPoint searchLocation;
GeoPoint mylocation;
double myLatitude=0;
double myLongitude=0;
List<Overlay> mapOverlays;
Drawable drawable;
SimpleItemizedOverlay itemizedOverlay;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.location);
    initMapView();
    //initZoomControls();
    initMyLocation();
    //Not complete this method
    text_location.setOnKeyListener(new OnKeyListener(){
        public boolean onKey(View view, int keyCode,KeyEvent event){
            if(keyCode==KeyEvent.KEYCODE_ENTER){
                // The map should shows a location that user writes in edittex.
                Geocoder geo=new Geocoder(getApplicationContext(),Locale.getDefault());

                List<Address> address;
                try {
                    address = geo.getFromLocationName(text_location.getText().toString(),3);
                    if(address.size()>0){
                        searchLocation=new GeoPoint((int)(address.get(0).getLatitude()*1e6),(int)(address.get(0).getLongitude()*1e6));
                        controller.animateTo(searchLocation);
                        controller.setZoom(12);
                        //

                    }
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                return true;
            }
            return false;
        }
    });
    mapOverlays=map.getOverlays();
    drawable=getResources().getDrawable(R.drawable.marker);
    itemizedOverlay=new SimpleItemizedOverlay(drawable,map);
    OverlayItem overlayItem=new OverlayItem(searchLocation,text_location.getText().toString(),"for test");
    itemizedOverlay.addOverlay(overlayItem);
    mapOverlays.add(itemizedOverlay);

}
@Override
protected boolean isRouteDisplayed() {
    return false;
}
private void initMapView() {
    linear = (FrameLayout) findViewById(R.id.frame);
    text_location=(EditText)findViewById(R.id.edit_text);
    map = (MapView) findViewById(R.id.map);
    controller = map.getController();
    map.setSatellite(true);
}
/*private void initZoomControls() {
    //WHY???
    View zoomControls = map.getZoomControls();
    FrameLayout.LayoutParams p = new FrameLayout.LayoutParams(
       LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT,Gravity.CENTER_VERTICAL + Gravity.CENTER_HORIZONTAL);
    linear.addView(zoomControls, p);
}*/
private void initMyLocation() {
    final MyLocationOverlay overlay = new MyLocationOverlay(this, map);
    overlay.enableMyLocation();
    overlay.enableCompass(); 
    //
    mylocation=overlay.getMyLocation();
    overlay.runOnFirstFix(new Runnable() {
        public void run() {
            controller.setZoom(13);
            controller.animateTo(overlay.getMyLocation());
        }
    });
    map.getOverlays().add(overlay);

    //myLatitude=(overlay.getMyLocation().getLatitudeE6())/1e6;
    //myLongitude=(overlay.getMyLocation().getLongitudeE6())/1e6;

}

}   

LOgCat:

11-09 13:02:37.480: D/libEGL(26632): loaded /system/lib/egl/libGLES_android.so
11-09 13:02:37.488: D/libEGL(26632): loaded /vendor/lib/egl/libEGL_POWERVR_SGX540_120.so
11-09 13:02:37.488: D/libEGL(26632): loaded /vendor/lib/egl/libGLESv1_CM_POWERVR_SGX540_120.so
11-09 13:02:37.496: D/libEGL(26632): loaded /vendor/lib/egl/libGLESv2_POWERVR_SGX540_120.so
11-09 13:02:37.637: D/OpenGLRenderer(26632): Enabling debug mode 0
11-09 13:02:40.582: D/dalvikvm(26632): GC_CONCURRENT freed 189K, 2% free 14334K/14599K, paused 3ms+2ms
11-09 13:02:40.582: W/CursorWrapperInner(26632): Cursor finalized without prior close()
11-09 13:02:40.691: I/Maps.MyLocationOverlay(26632): Request updates from network
11-09 13:02:40.707: D/dalvikvm(26632): GC_CONCURRENT freed 121K, 2% free 14613K/14855K, paused 2ms+2ms
11-09 13:02:40.855: D/AndroidRuntime(26632): Shutting down VM
11-09 13:02:40.855: W/dalvikvm(26632): threadid=1: thread exiting with uncaught exception (group=0x40a3f1f8)
11-09 13:02:40.855: E/AndroidRuntime(26632): FATAL EXCEPTION: main
11-09 13:02:40.855: E/AndroidRuntime(26632): java.lang.RuntimeException: Unable to start activity ComponentInfo{org.example.loyaltier/org.example.loyaltier.Location}: java.lang.NullPointerException
11-09 13:02:40.855: E/AndroidRuntime(26632):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955)
11-09 13:02:40.855: E/AndroidRuntime(26632):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
11-09 13:02:40.855: E/AndroidRuntime(26632):    at android.app.ActivityThread.access$600(ActivityThread.java:122)
11-09 13:02:40.855: E/AndroidRuntime(26632):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
11-09 13:02:40.855: E/AndroidRuntime(26632):    at android.os.Handler.dispatchMessage(Handler.java:99)
11-09 13:02:40.855: E/AndroidRuntime(26632):    at android.os.Looper.loop(Looper.java:137)
11-09 13:02:40.855: E/AndroidRuntime(26632):    at android.app.ActivityThread.main(ActivityThread.java:4340)
11-09 13:02:40.855: E/AndroidRuntime(26632):    at java.lang.reflect.Method.invokeNative(Native Method)
11-09 13:02:40.855: E/AndroidRuntime(26632):    at java.lang.reflect.Method.invoke(Method.java:511)
11-09 13:02:40.855: E/AndroidRuntime(26632):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
11-09 13:02:40.855: E/AndroidRuntime(26632):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
11-09 13:02:40.855: E/AndroidRuntime(26632):    at dalvik.system.NativeStart.main(Native Method)
11-09 13:02:40.855: E/AndroidRuntime(26632): Caused by: java.lang.NullPointerException
11-09 13:02:40.855: E/AndroidRuntime(26632):    at com.google.android.maps.ItemizedOverlay.populate(ItemizedOverlay.java:312)
11-09 13:02:40.855: E/AndroidRuntime(26632):    at org.example.loyaltier.SimpleItemizedOverlay.addOverlay(SimpleItemizedOverlay.java:27)
11-09 13:02:40.855: E/AndroidRuntime(26632):    at org.example.loyaltier.Location.onCreate(Location.java:85)
11-09 13:02:40.855: E/AndroidRuntime(26632):    at android.app.Activity.performCreate (Activity.java:4465)
11-09 13:02:40.855: E/AndroidRuntime(26632):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
11-09 13:02:40.855: E/AndroidRuntime(26632):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
11-09 13:02:40.855: E/AndroidRuntime(26632):    ... 11 more
11-09 13:02:42.887: I/Process(26632): Sending signal. PID: 26632 SIG: 9
11-09 13:02:37.480: D/libEGL(26632): loaded /system/lib/egl/libGLES_android.so
11-09 13:02:37.488: D/libEGL(26632): loaded /vendor/lib/egl/libEGL_POWERVR_SGX540_120.so
11-09 13:02:37.488: D/libEGL(26632): loaded /vendor/lib/egl/libGLESv1_CM_POWERVR_SGX540_120.so
11-09 13:02:37.496: D/libEGL(26632): loaded /vendor/lib/egl/libGLESv2_POWERVR_SGX540_120.so
11-09 13:02:37.637: D/OpenGLRenderer(26632): Enabling debug mode 0
11-09 13:02:40.582: D/dalvikvm(26632): GC_CONCURRENT freed 189K, 2% free 14334K/14599K, paused 3ms+2ms
11-09 13:02:40.582: W/CursorWrapperInner(26632): Cursor finalized without prior close()
11-09 13:02:40.691: I/Maps.MyLocationOverlay(26632): Request updates from network
11-09 13:02:40.707: D/dalvikvm(26632): GC_CONCURRENT freed 121K, 2% free 14613K/14855K, paused  2ms+2ms
11-09 13:02:40.855: D/AndroidRuntime(26632): Shutting down VM
11-09 13:02:40.855: W/dalvikvm(26632): threadid=1: thread exiting with uncaught exception (group=0x40a3f1f8)
11-09 13:02:40.855: E/AndroidRuntime(26632): FATAL EXCEPTION: main
11-09 13:02:40.855: E/AndroidRuntime(26632): java.lang.RuntimeException: Unable to start activity ComponentInfo{org.example.loyaltier/org.example.loyaltier.Location}: java.lang.NullPointerException
11-09 13:02:40.855: E/AndroidRuntime(26632):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955)
11-09 13:02:40.855: E/AndroidRuntime(26632):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
11-09 13:02:40.855: E/AndroidRuntime(26632):    at android.app.ActivityThread.access$600(ActivityThread.java:122)
11-09 13:02:40.855: E/AndroidRuntime(26632):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
11-09 13:02:40.855: E/AndroidRuntime(26632):    at android.os.Handler.dispatchMessage(Handler.java:99)
11-09 13:02:40.855: E/AndroidRuntime(26632):    at android.os.Looper.loop(Looper.java:137)
11-09 13:02:40.855: E/AndroidRuntime(26632):    at android.app.ActivityThread.main(ActivityThread.java:4340)
11-09 13:02:40.855: E/AndroidRuntime(26632):    at java.lang.reflect.Method.invokeNative(Native Method)
11-09 13:02:40.855: E/AndroidRuntime(26632):    at java.lang.reflect.Method.invoke(Method.java:511)
11-09 13:02:40.855: E/AndroidRuntime(26632):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
11-09 13:02:40.855: E/AndroidRuntime(26632):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
11-09 13:02:40.855: E/AndroidRuntime(26632):    at dalvik.system.NativeStart.main(Native Method)
11-09 13:02:40.855: E/AndroidRuntime(26632): Caused by: java.lang.NullPointerException
11-09 13:02:40.855: E/AndroidRuntime(26632):    at com.google.android.maps.ItemizedOverlay.populate(ItemizedOverlay.java:312)
11-09 13:02:40.855: E/AndroidRuntime(26632):    at org.example.loyaltier.SimpleItemizedOverlay.addOverlay(SimpleItemizedOverlay.java:27)
11-09 13:02:40.855: E/AndroidRuntime(26632):    at org.example.loyaltier.Location.onCreate(Location.java:85)
11-09 13:02:40.855: E/AndroidRuntime(26632):    at android.app.Activity.performCreate(Activity.java:4465)
11-09 13:02:40.855: E/AndroidRuntime(26632):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
11-09 13:02:40.855: E/AndroidRuntime(26632):    at  android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
11-09 13:02:40.855: E/AndroidRuntime(26632):    ... 11 more
11-09 13:02:42.887: I/Process(26632): Sending signal. PID: 26632 SIG: 9
  • Could you post some related code? – dumbfingers Nov 09 '12 at 09:49
  • 2
    How can a mechanic repair your car with out even seeing it? show us your code. – Seshu Vinay Nov 09 '12 at 09:50
  • what is there in line no 85 of `Location.java`? – Shashank Kadne Nov 09 '12 at 09:51
  • `at com.google.android.maps.ItemizedOverlay.populate(ItemizedOverlay.java:312) 11-09 13:02:40.855: E/AndroidRuntime(26632): at org.example.loyaltier.SimpleItemizedOverlay.addOverlay(SimpleItemizedOverlay.java:27) 11-09 13:02:40.855: E/AndroidRuntime(26632): at org.example.loyaltier.Location.onCreate(Location.java:85)` There's your error. If you'd post code, we could debug it too. – varevarao Nov 09 '12 at 09:52
  • 1
    some show code and no LogCat, some show LogCat without code. They wan't help but why they make it so difficult? – Budius Nov 09 '12 at 09:53
  • From the logcat output it looks like the OverlayItem you're trying to add into the MapOverlay is NULL. So post the code in Location.java that uses the overlays. – varevarao Nov 09 '12 at 09:57
  • I edit fist post and add Location class to first post – simon Quicke Nov 09 '12 at 09:58

2 Answers2

0

Firstly we are not going to be able to help you if you just show us a error report, you need to show the actual code.

Secondly there are plenty of resources to teach you what a null pointer exception is so you can debug your program, here's one: What is a NullPointerException, and how do I fix it?

Community
  • 1
  • 1
8BitSensei
  • 301
  • 3
  • 15
0
OverlayItem overlayItem=new OverlayItem(searchLocation,text_location.getText().toString(),"for test");

text_location.getText() would be null at first go, because you've put it outside the keyListener.

Try putting this code

mapOverlays=map.getOverlays();
    drawable=getResources().getDrawable(R.drawable.marker);
    itemizedOverlay=new SimpleItemizedOverlay(drawable,map);
    OverlayItem overlayItem=new OverlayItem(searchLocation,text_location.getText().toString(),"for test");
    itemizedOverlay.addOverlay(overlayItem);
    mapOverlays.add(itemizedOverlay);

into your if(keyCode==KeyEvent.KEYCODE_ENTER){ area.

varevarao
  • 2,186
  • 13
  • 26
  • Thanks. Why text_location.getText() is null outside try part? – simon Quicke Nov 09 '12 at 10:13
  • @simonQuicke, the .getText() method in your code would be called as soon as the activity is created because it's outside the onKeyListener that you added. Which means, even before the user enters anything you're trying to retrieve text. – varevarao Nov 09 '12 at 12:56