Panagiotis
Panagiotis

Reputation: 511

Android Getting data from rest web service

I want the Main Activity gets some data from one rest web service. For this job I use AsyncTask for the connection with the web service. When in the onCreate method displayed the XML Layout file, that will display the data from the web service, the application works successfully. However when in the onCreate method displayed a different XML Layout file, from the Layout file that will display the data from the web service, the application doesn't work and logcats displays NullPointerException in the data that were taken from web service.

public class MainActivity extends Activity  
{       
    @Override
    public void onCreate( Bundle savedInstanceState ) 
    { 
        super.onCreate( savedInstanceState );
        setContentView( R.layout.splash_screen );     
        new DownloadSights().execute();
    }

    class DownloadSights extends AsyncTask< Void, Void, Places > 
    {
        @Override
        protected void onPreExecute()
        {
            super.onPreExecute();
        }

        @Override
        protected ListKozanisPlaces doInBackground( Void... params )
        {
            try 
            {
                final String url = "http://192.168.1.3:8080/.../getSights";
                InputStreamReader reader = new InputStreamReader( new URL( url ).openStream() );
                Places sightsList = new Gson().fromJson( reader, Places.class );
                return sightsList;
            } 
            catch ( Exception e )
            {
                Log.e( "CityCuide", e.getMessage() );
            }
            return null;
        }


        @Override 
        protected void onPostExecute( Places sightsList )
        {
            TextView testTextView = (TextView) findViewById(R.id.testTextView);
            testTextView.setText( sightsList.getList().get( 1 ).toString() );
        }
    }
}

The error located in the row:

testTextView.setText( sightsList.getList().get( 1 ).toString() );

Logcat displays:

01-22 01:31:12.211: E/AndroidRuntime(5455): FATAL EXCEPTION: main
01-22 01:31:12.211: E/AndroidRuntime(5455): java.lang.NullPointerException
01-22 01:31:12.211: E/AndroidRuntime(5455):     at com.example.androidkozaniwebserviceclient.SplashScreenActivity$DownloadSights.onPostExecute(SplashScreenActivity.java:66)
01-22 01:31:12.211: E/AndroidRuntime(5455):     at com.example.androidkozaniwebserviceclient.SplashScreenActivity$DownloadSights.onPostExecute(SplashScreenActivity.java:1)
01-22 01:31:12.211: E/AndroidRuntime(5455):     at android.os.AsyncTask.finish(AsyncTask.java:602)
01-22 01:31:12.211: E/AndroidRuntime(5455):     at android.os.AsyncTask.access$600(AsyncTask.java:156)
01-22 01:31:12.211: E/AndroidRuntime(5455):     at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:615)
01-22 01:31:12.211: E/AndroidRuntime(5455):     at android.os.Handler.dispatchMessage(Handler.java:99)
01-22 01:31:12.211: E/AndroidRuntime(5455):     at android.os.Looper.loop(Looper.java:137)
01-22 01:31:12.211: E/AndroidRuntime(5455):     at android.app.ActivityThread.main(ActivityThread.java:4424)
01-22 01:31:12.211: E/AndroidRuntime(5455):     at java.lang.reflect.Method.invokeNative(Native Method)
01-22 01:31:12.211: E/AndroidRuntime(5455):     at java.lang.reflect.Method.invoke(Method.java:511)
01-22 01:31:12.211: E/AndroidRuntime(5455):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
01-22 01:31:12.211: E/AndroidRuntime(5455):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
01-22 01:31:12.211: E/AndroidRuntime(5455):     at dalvik.system.NativeStart.main(Native Method)

Upvotes: 0

Views: 172

Answers (2)

Amit K. Saha
Amit K. Saha

Reputation: 5951

Make sure sightsList is not null if you are sure about Gabe's answer. I would suggest you to break the testTextView.setText( sightsList.getList().get( 1 ).toString() ); to several pieces to easily find out what is happening there.

List targetList = sightsList.getList();
String data = targetList.get(1).toString(); // this index 1 might return null also
textTextView.setText(data);

Upvotes: 0

Gabe Sechan
Gabe Sechan

Reputation: 93708

If you load a different xml layout, it won't have a textview named testTextView. That will make findViewById return null. That will cause a NullPointerException in onPostExecute when you try to call setText on it.

This problem was kind of trivial, but in the future please attach a stack trace with any crashing bugs.

Upvotes: 1

Related Questions