尝试从空对象引用

时间:2018-03-11 12:30:25

标签: android nullpointerexception

我正在开发一个天气预测应用程序,当我使用日期格式从JSON解析中获取我的日期时,它给了我和这样的错误:

  java.lang.NullPointerException: Attempt to read from field 'Informations.Location Informations.Weather.location' on a null object reference
                                                 at com.lovish1pandey.android.weatherforcasting.MainActivity$WeatherTask.onPostExecute(MainActivity.java:106)
                                                 at com.lovish1pandey.android.weatherforcasting.MainActivity$WeatherTask.onPostExecute(MainActivity.java:101)

我已经被困在这里3天了,并且无法解决这个问题。 mainActivity代码如下:

public void showInputDialog(View view) {
    AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);

    builder.setTitle("Change City");
    final EditText cityInput = new EditText(MainActivity.this);
    cityInput.setInputType(InputType.TYPE_CLASS_TEXT);
    cityInput.setHint("Portland,US");
    builder.setView(cityInput);
    builder.setPositiveButton("Submit", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            CityPrefer cityPrefer = new CityPrefer(MainActivity.this);
            cityPrefer.setCity(cityInput.getText().toString());

            String newCity = cityPrefer.getCity();
            displayWeatherData(newCity);
        }
    });
    builder.show();
}

public void displayWeatherData(String city) {
    WeatherTask weatherTask = new WeatherTask();
    weatherTask.execute(new String[]{city + "&units=metric"});
}

private class WeatherTask extends AsyncTask<String, Void, Weather> {

    @Override
    protected Weather doInBackground(String... params) {
        String data = ((new WeatherHttpClient()).getWeatherData(params[0]));
        weather = WeatherJASONParser.getWeather(data);
        return null;
    }

此处出现一个错误(错误2)

    @Override
    protected void onPostExecute(Weather weather) {
        super.onPostExecute(weather);

        DateFormat df = DateFormat.getTimeInstance();

另一个人在这里(1号错误)

 String sunriseTime = df.format(new Date(weather.location.getSunrise()));
        String sunsetTime = df.format(new Date(weather.location.getSunset()));

        DecimalFormat decimalFormat = new DecimalFormat("#.#");

        String temperatureFormat = decimalFormat.format(weather.currentCondition.getTemprature());

        cityName.setText(weather.location.getCity() + "," + weather.location.getCountry());
        temp.setText("" + temperatureFormat);
        humidity.setText("Humidity: " + weather.currentCondition.getHumidity() + "%");
        pressure.setText("Pressure: " + weather.currentCondition.getPressure() + "hpa");
        wind.setText("Wind: " + weather.wind.getSpeed() + "mps");
        sunrise.setText("Sunrise: " + sunriseTime);
        sunset.setText("Sunset: " + sunsetTime);
        description.setText("Condition: " + weather.currentCondition.getCondition() + " ( " + weather.currentCondition.getDescription() + " )");

    }
}
}

和JSONParser看起来像这样:

  public static Weather getWeather(String data){
    Weather weather = new Weather();
    try{
        JSONObject jsonObject = new JSONObject();
        Location location = new Location();

        JSONObject coordObj = Util.getObject("coord",jsonObject);
        location.setLat(Util.getFloat("lat",coordObj));
        location.setLongi(Util.getFloat("lon",coordObj));

        JSONObject sysObj = Util.getObject("sys",jsonObject);
        location.setCountry(Util.getString("country",sysObj));
        location.setSunrise(Util.getInt("sunrise",sysObj));
        location.setSunset(Util.getInt("sunset",sysObj));
        location.setCity(Util.getString("name ",jsonObject));
        weather.location = location;

包含所有声明的Weather Class如下:

public class Weather {
public Location location;
public String iconData;
public CurrentCondition currentCondition = new CurrentCondition();
public Temprature temprature = new Temprature();
public Wind wind = new Wind();
public Cloud cloud = new Cloud();
public Snow snow = new Snow();
 }

1 个答案:

答案 0 :(得分:0)

private class WeatherTask extends AsyncTask<String, Void, Weather> {

    @Override
    protected Weather doInBackground(String... params) {
        String data = ((new WeatherHttpClient()).getWeatherData(params[0]));
        weather = WeatherJASONParser.getWeather(data);
        return null;
    }

它返回Null ...它应该返回Weather

private class WeatherTask extends AsyncTask<String, Void, Weather> {

    @Override
    protected Weather doInBackground(String... params) {
        String data = ((new WeatherHttpClient()).getWeatherData(params[0]));
        weather = WeatherJASONParser.getWeather(data);
        return weather;
    }

............................................... ...............................