首页 > 解决方案 > java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“double android.location.Location.getLongitude()”

问题描述

我不明白为什么应用程序关闭:

java.lang.NullPointerException: Attempt to invoke virtual method 'double android.location.Location.getLongitude()' on a null object reference
  private void getWeatherInformation() {
        compositeDisposable.add(mSevice.getWeatherByLatLhg(String.valueOf(Common.current_location),
                String.valueOf(Common.current_location.getLongitude()),
                Common.APP_ID,
                "metric")
                .subscribeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<WeatherResponse>() {
                    @Override
                    public void accept(WeatherResponse weatherResponse) throws Exception {
                        Picasso.get().load(new StringBuilder("https://openweathermap.org/img/wn/")
                                .append(weatherResponse.getWeather().get(0).getIcon()).append(".png").toString()).into(imageWeather);

                        cityName.setText(weatherResponse.getName());
                        description.setText(new StringBuilder("Weather in ")
                        .append(weatherResponse.getName()).toString());
                        temperature.setText(new StringBuilder(String.valueOf(weatherResponse.getMain().getTemp()))
                        .append("°C"));
                        dateTime.setText(Common.convertUnixToDate(weatherResponse.getDt()));

                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        Toast.makeText(getActivity(), ""+throwable.getMessage(), Toast.LENGTH_SHORT).show();
                    }
                })

        );

标签: javaandroidnullpointerexceptionandroid-location

解决方案


Common.current_location.getLongitude() 是你的问题

Common 或 Common.current_location 为 null(未分配值),因此您正在调用 getLongitude() 方法对不存在的东西。


推荐阅读