Java googleMap上的addmarker和markeroptions出现空指针异常

Java googleMap上的addmarker和markeroptions出现空指针异常,java,android,google-maps,google-maps-markers,google-maps-android-api-2,Java,Android,Google Maps,Google Maps Markers,Google Maps Android Api 2,当我尝试在我们的应用程序上加载google map时,我得到了nullpointerException,如下所示。此外,java类从AppCompativty扩展到intialiseMap()内部,我们尝试getMap() 这里是我们得到异常的地方,在placeMarker方法内部 public void placeMarker(LatLongDetails user_latlongobj2, final Context contextPlace)

当我尝试在我们的应用程序上加载google map时,我得到了nullpointerException,如下所示。此外,java类从
AppCompativty
扩展到
intialiseMap()
内部,我们尝试
getMap()

这里是我们得到异常的地方,在placeMarker方法内部

public void placeMarker(LatLongDetails user_latlongobj2,
                        final Context contextPlace) {
    try {
        if (googlemap == null) {
            intialiseMap();
            animatecamera(user_latlongobj);
        }
        if (LoginDetails.Address.length() < 1) {
            LoginDetails.Address = "Getting your location .....";
        }
        //googlemap.clear();
        marker = new MarkerOptions().position(
                new LatLng(user_latlongobj2.user_latitude,
                        user_latlongobj2.user_longitude)).title(
                LoginDetails.Address);

        System.out.println("This is the Address" + LoginDetails.Address);

        marker.icon(BitmapDescriptorFactory.fromResource(R.drawable.marker));

        if (googlemap != null) {

            googlemap.addMarker(marker).showInfoWindow();
        }else {
            intialiseMap();
            googlemap.addMarker(marker).showInfoWindow();
        }
        System.out.println("PLACING MARKER" + LoginDetails.Address);
        if (marker == null || contextPlace == null) {
            Intent in =new Intent(this,ProfileActivity1.class);
            in.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            in.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(in);
        }
        else
            fixmarker(marker, contextPlace);

    } catch (Exception e) {
        fixmarker(marker, contextPlace);
        Log.d("Profileactivity", "" + e);
    }

}

根据要求,提供一份答案,总结解决方案和找到解决方案的过程:

1)
NullPointerException
和相应的消息表明问题在该块的else分支中:

    if (googlemap != null) {
        googlemap.addMarker(marker).showInfoWindow();
    }else {
        intialiseMap();
        googlemap.addMarker(marker).showInfoWindow();
    }
这里,
intialiseMap()似乎无法初始化映射

2) 在
intialiseMap()
中有一个try-catch块,其中
googlemap
如果为空,则应初始化该块。但是,
catch
-块是空的,因此尝试初始化时的任何异常都会丢失

未来读者注意:如果你发现了一个异常,你应该总是,总是,总是以某种方式处理它。至少做某事最简单的方法之一就是记录它

当然,在某些情况下,您只想忽略一个特定的异常,但在这种情况下,您应该真正了解其后果(抛出该异常时会发生什么,为什么抛出该异常等),并且您应该始终记录您是故意忽略该异常的,例如,在代码中添加一个简短的注释

3) 在记录捕获的异常之后,OP意识到
googlemap
的初始化失败,因此能够进一步跟踪问题

然后他寻找答案和解决方案,并想出以下两条线索帮助他解决问题:


我猜
初始化映射()不工作或返回新映射,因此调用应该是
googlemap=intialiseMap()。基本上,错误消息会告诉您出了什么问题:
NullPointerException
。第一个警告显示类
LatLongDetails
中的
user\u longitude
是一个类变量,您正在通过实例
user\u latlongobj2.user\u longitude
访问该类变量,但有一个
static
修饰符。您只需删除
static
@Thomas google=intialiseMap();给出不兼容的类型error
else{initialisemap();googlemap.addMarker(marker).showInfoWindow();}
@QBrute我在这里提问之前添加了这一行,以检查这是否能解决空指针异常
private void intialiseMap() {

try {
if (dialog == null)
    dialog = ProgressDialog.show(ProfileActivity1.this, "",
            getString(R.string.getting_location), true, true);
}catch(Exception e) {
Log.e("eybaba",""+e);
}
    try {
        if (googlemap == null) {
            googlemap = ((MapFragment) getFragmentManager()
                    .findFragmentById(R.id.mapfragment)).getMap();
            googlemap.setInfoWindowAdapter(new CustomInfowindow(context));
            // check if map is created successfully or not
            if (googlemap == null) {
                Toast.makeText(getApplicationContext(),
                        R.string.maps_create_error, Toast.LENGTH_SHORT)
                        .show();
            }
        }
    } catch (Exception e) {
    }
}
    if (googlemap != null) {
        googlemap.addMarker(marker).showInfoWindow();
    }else {
        intialiseMap();
        googlemap.addMarker(marker).showInfoWindow();
    }