0

I am doing an Android weather app using the yahoo API and getting errors. Does anyone know what this error means?

07-28 20:04:23.958: W/dalvikvm(1303): threadid=1: thread exiting with uncaught exception (group=0x2bd39930)
07-28 20:04:23.958: E/AndroidRuntime(1303): FATAL EXCEPTION: main
07-28 20:04:23.958: E/AndroidRuntime(1303): java.lang.NullPointerException
07-28 20:04:23.958: E/AndroidRuntime(1303):     at com.sagar.finalweather.MainActivity.serviceSuccess(MainActivity.java:56)
07-28 20:04:23.958: E/AndroidRuntime(1303):     at com.sagar.finalweather.service.YahooWeatherService$1.onPostExecute(YahooWeatherService.java:84)
07-28 20:04:23.958: E/AndroidRuntime(1303):     at com.sagar.finalweather.service.YahooWeatherService$1.onPostExecute(YahooWeatherService.java:1)
07-28 20:04:23.958: E/AndroidRuntime(1303):     at android.os.AsyncTask.finish(AsyncTask.java:631)
07-28 20:04:23.958: E/AndroidRuntime(1303):     at android.os.AsyncTask.access$600(AsyncTask.java:177)
07-28 20:04:23.958: E/AndroidRuntime(1303):     at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
07-28 20:04:23.958: E/AndroidRuntime(1303):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-28 20:04:23.958: E/AndroidRuntime(1303):     at android.os.Looper.loop(Looper.java:137)
07-28 20:04:23.958: E/AndroidRuntime(1303):     at android.app.ActivityThread.main(ActivityThread.java:5039)
07-28 20:04:23.958: E/AndroidRuntime(1303):     at java.lang.reflect.Method.invokeNative(Native Method)
07-28 20:04:23.958: E/AndroidRuntime(1303):     at java.lang.reflect.Method.invoke(Method.java:511)
07-28 20:04:23.958: E/AndroidRuntime(1303):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
07-28 20:04:23.958: E/AndroidRuntime(1303):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
07-28 20:04:23.958: E/AndroidRuntime(1303):     at dalvik.system.NativeStart.main(Native Method)
07-28 20:04:27.148: I/Process(1303): Sending signal. PID: 1303 SIG: 9

Its is the "mainActivity.java" file please help me to find the exact error. I am new programmer at this.

package com.sagar.finalweather;

import android.app.Activity;
import android.app.ProgressDialog;
import com.sagar.finalweather.data.Item;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.sagar.finalweather.data.Channel;
import com.sagar.finalweather.service.WeatherServiceCallback;
import com.sagar.finalweather.service.YahooWeatherService;





public class MainActivity extends Activity implements WeatherServiceCallback 
{
private ImageView weatherIconImageView;
private TextView temperatureTextView;
private TextView conditionTextView;
private TextView locationTextView;

private ProgressDialog dialog;
private YahooWeatherService service;
@Override
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.layout_main);

    weatherIconImageView = (ImageView)findViewById(R.id.weatherIconImageView);
    temperatureTextView = (TextView)findViewById(R.id.LocationTextView);
    temperatureTextView = (TextView)findViewById(R.id.TemperatureTextView);
    temperatureTextView = (TextView)findViewById(R.id.conditionTextView);

    try{
    service = new YahooWeatherService(this);
    dialog=new ProgressDialog(this);
    dialog.setMessage("Loading.......");
    dialog.show();
    service.refreshWeather("Austin , TX");
    }catch(Exception e)
    {
        e.getMessage();
    }
}
@Override
public void serviceSuccess(Channel channel) {
    // TODO Auto-generated method stub
    dialog.hide();

    Item item = channel.getItem();
    int resourceId = getResources().getIdentifier("drawable/value_"+item.getCondition().getCode(), null, getPackageName());

    Drawable weatherIconDrawable = getResources().getDrawable(resourceId);

    weatherIconImageView.setImageDrawable(weatherIconDrawable);
    temperatureTextView.setText(item.getCondition().getTemperature()+ "\u00B0" + channel.getUnits().getTemperature());
    conditionTextView.setText(item.getCondition().getDescription());
    locationTextView.setText(service.getLocation());
}
@Override
public void serviceFailure(Exception exception) {
    // TODO Auto-generated method stub
    dialog.hide();
    Toast.makeText(this, exception.getMessage(), Toast.LENGTH_LONG).show();
}

}

  • You are getting a NullPointerException on line 56. Please post lines 1-56 of your `MainActivity` class. – Barett Jul 28 '15 at 20:25
  • Hi @sagar. Welcome to StackOverflow. The crash log you posted is a `NullPointerException`. If you do not know a `NPE` is, you can go through this link http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it and also search google for NPE.. – Varun Jul 28 '15 at 22:07
  • Thanx Barett . i have uploaded MainActivity.java file can you tell me where is that error. – sagar chaudhari Jul 29 '15 at 04:12

1 Answers1

0

What I suppose is that dialog you're trying to close:

dialog.hide();

Was not yet created in onCreate() method change this

   try{
    service = new YahooWeatherService(this);
    dialog=new ProgressDialog(this);
    dialog.setMessage("Loading.......");
    dialog.show();
    service.refreshWeather("Austin , TX");
    }

to this:

    dialog=new ProgressDialog(this);
    dialog.setMessage("Loading.......");
    dialog.show();
   try{
    service = new YahooWeatherService(this);
    service.refreshWeather("Austin , TX");
    }
piotrpo
  • 12,398
  • 7
  • 42
  • 58