Search code examples
androidandroid-serviceandroid-adapterandroid-service-binding

Android service error Attempt to invoke virtual method


I am making an app, where a server in java sends a Place Array and other stuff in other activities. so I create a service to connect to the server from different activities. I have not implement server connection yet but I am simulating the server response, which I pass as parameter to an adapter (the adapter is tested), that I create to show this places in a ListView. so I got my Place array in my activity and I set it to the return object from a method in the service, but when I run the app it crashes and I got this error message:

java.lang.NullPointerException: Attempt to invoke virtual method 'com.remedialguns.smartourist.Place[] com.remedialguns.smartourist.ConnectionService.getPlaces()' on a null object.

UPDATE now is te same error but in myAdapter class when name.setText... so this is my service.

package com.remedialguns.smartourist;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;
import java.io.IOException;
import java.io.PrintStream;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
import java.util.Locale;

public class ConnectionService extends Service {

    Socket s;
    PrintStream os;

    private static final String TAG="com.remedialguns.smartourist";
    private final IBinder myBinder = new LocalBinder();

    @Override
    public IBinder onBind(Intent intent) {
        return myBinder;
    }

    public void sendProfileData(){
        //send profile data to server
    }

    public Place[] getPlaces(){
        Place[] PlacesToShow = new Place[10];

        //F̶a̶k̶e̶ ̶d̶a̶t̶a̶ Test data
        PlacesToShow[0]= new Place("MUSEO","Museo Nacional Agropecuario", 0.15, 0.4, 0.12);
        PlacesToShow[1]= new Place("MUSEO","Museo Arqueológico Junín",0.10, 0.78, 0.44);
        PlacesToShow[2]= new Place("MUSEO","Museo Botero", 0.2, 0.8, 0.08);
        PlacesToShow[3]= new Place("MUSEO","Museo de Zea", 0.3, 0.65, 0.23);
        PlacesToShow[4]= new Place("MUSEO","MUSEO DEL ORO", 0.13, 0.56, 0.12);
        PlacesToShow[5]= new Place("MUSEO","MUSEO DE ARTE COLONIAL", 0.3, 0.67, 0.14);
        PlacesToShow[6]= new Place("MUSEO","MUSEO HISTORICO DE LA POLICIA NACIONAL", 0.34, 0.3, 0.33);
        PlacesToShow[8]= new Place("MUSEO","MUSEO DE LOS NIÑOS", 0.05, 0.65, 0.03);
        PlacesToShow[7]= new Place("MUSEO","Museo Nacional", 0.15, 0.4, 0.12);
        PlacesToShow[9]= new Place("MUSEO","MUSEO MILITAR", 0.07, 0.5, 0.6);
        return PlacesToShow;
    }



    public class LocalBinder extends Binder {
        ConnectionService getService(){
            return ConnectionService.this;
        }
    }
}

this is my activity with my ListView and adapter.(UPDATE)

package com.remedialguns.smartourist;

import android.content.ComponentName;
import android.content.Context;
import android.os.Bundle;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.os.IBinder;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import com.remedialguns.smartourist.ConnectionService.LocalBinder;

import com.google.android.gms.location.places.Places;

public class ListActivity extends AppCompatActivity {

    ConnectionService tcpService;
    boolean isBound=false;
    //Place[] PlacesToShow=tcpService.getPlaces();
    Place[] PlacesToShow=new Place[10];



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        Intent i = new Intent(this, ConnectionService.class);
        bindService(i, myConnection, Context.BIND_AUTO_CREATE);
//        PlacesToShow=tcpService.getPlaces();


        if (isBound) {
            PlacesToShow = tcpService.getPlaces();
        }

        //Place[] PlacesToShow=tcpService.getPlaces();

        ListAdapter MyAdapter = new MyAdapter(this, PlacesToShow);
        ListView ListPlaces=(ListView) findViewById(R.id.MyList);
        ListPlaces.setAdapter(MyAdapter);

        ListPlaces.setOnItemClickListener(
                new AdapterView.OnItemClickListener(){
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                        Context context = view.getContext();
                        TextView textViewItem = ((TextView)view.findViewById(R.id.name));
                        String name =textViewItem.getText().toString();
                        TextView textViewItem2 = (TextView)findViewById(R.id.description);
                        String descripcion=textViewItem2.getText().toString();
                        Toast.makeText(context,"lugar: "+name+", descripcion: "+descripcion,Toast.LENGTH_SHORT).show();
                    }
                });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    private ServiceConnection myConnection=new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            LocalBinder binder = (LocalBinder) service;
            tcpService = binder.getService();
            PlacesToShow = tcpService.getPlaces();
            isBound = true;
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            isBound=false;
        }
    };


}

this is my activity.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        android:paddingBottom="@dimen/activity_vertical_margin"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        tools:showIn="@layout/activity_list"
        tools:context="com.remedialguns.smartourist.ListActivity">

    <!--    <TextView
            android:id="@+id/answer"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"


    android:text="these are the places that our smart monkeys had find for you."/>
-->
    <ListView
        android:id="@+id/MyList"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </ListView>

</LinearLayout>

this is my manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.remedialguns.smartourist" >

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".LoginActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />

        <uses-permission android:name="android.permission.INTERNET" />
        <uses-permission android:name="android.permission.GET_ACCOUNTS" />
        <uses-permission android:name="android.permission.USE_CREDENTIALS" />

        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar" >
        </activity>
        <activity
            android:name=".RealMainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar" >
        </activity>
        <activity
            android:name=".ListActivity"
            android:label="@string/app_name"
            android:parentActivityName=".RealMainActivity"
            android:theme="@style/AppTheme.NoActionBar" >
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="com.remedialguns.smartourist.RealMainActivity" />
        </activity>

        <service
            android:name=".ConnectionService"
            android:enabled="true"
            android:exported="true" >
        </service>
    </application>

</manifest>

this is my adapter class

package com.remedialguns.smartourist;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class MyAdapter extends ArrayAdapter<Place> {

    MyAdapter(Context context, Place[] places){
        super(context, R.layout.visual_place, places);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        LayoutInflater MyInflater = LayoutInflater.from(getContext());
        View placeView = MyInflater.inflate(R.layout.visual_place, parent, false);

        Place singlePlaceItem=getItem(position);
        TextView name=(TextView) placeView.findViewById(R.id.name);
        ImageView icon =(ImageView) placeView.findViewById(R.id.icon);
        TextView description = (TextView) placeView.findViewById(R.id.description);

        name.setText(singlePlaceItem.getName().toString());
        icon.setImageResource(R.mipmap.ic_launcher);
        description.setText("Cost "+singlePlaceItem.getCost()+", distance "+singlePlaceItem.getDistance()+", rate "+singlePlaceItem.getRate()+" *");
        return placeView;

    }
}

please help me.


Solution

  • Here:

    bindService(i, myConnection, Context.BIND_AUTO_CREATE); 
    Place[] PlacesToShow=new Place[10];
    PlacesToShow=tcpService.getPlaces();
    

    tcpService is null probably using is to call getPlaces() method before onServiceConnected method is called.

    Use isBound to check onServiceConnected is called or not before using tcpService object:

    if(isBound){
      // 
      PlacesToShow=tcpService.getPlaces();
    } 
    

    and also use onServiceConnected method as:

    @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
                LocalBinder binder = (LocalBinder) service;
                tcpService = binder.getService();
                PlacesToShow=tcpService.getPlaces();
                isBound=true;
    }
    

    Also make sure added ConnectionService in AndroidManifest.xml as service.