Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8132705
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T09:24:32+00:00 2026-06-06T09:24:32+00:00

This should be pretty simple, still I can not see what/how something could be

  • 0

This should be pretty simple, still I can not see what/how something could be Null.
I have trying to have a AutoCompleteTextView with result from google places api.
I can see that my URL creation is correct, I am getting results from google but when the result is being propagated to the textView, everything crashes.

XML Layout – places_search.xml:

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#dddddd">

<AutoCompleteTextView android:id="@+id/autoCompleteTextView1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp" >
    <requestFocus></requestFocus>
    </AutoCompleteTextView>
</RelativeLayout>

XML layout for TextView:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="10dp"
    android:textSize="16sp"
    android:textColor="#000">
</TextView>

And below is the complete code which search for auto-completion results from google places api.

public class PlacesListSearchActivity extends Activity {

    private static final String TAG = "PlacesListActivity";

    private ResultReceiver mReceiver;

    private OnSharedPreferenceChangeListener sharedPreferencesListener;
    private SharedPreferences sharedPreferences;

    /** Called when the activity is first created. */
    public ArrayAdapter<String> adapter;
    public AutoCompleteTextView textView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.places_search);
        final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.item_list);
        final AutoCompleteTextView textView = (AutoCompleteTextView)findViewById(R.id.autoCompleteTextView1);
        adapter.setNotifyOnChange(true);
        textView.setHint("type store name");
        textView.setAdapter(adapter);
        textView.addTextChangedListener(new TextWatcher() {

        public void onTextChanged(CharSequence s, int start, int before, int count) {
            if (count%3 == 1) {
                adapter.clear();
                    GetPlaces task = new GetPlaces();
                    //now pass the argument in the textview to the task
                    task.execute(textView.getText().toString());
            }
        }

        public void beforeTextChanged(CharSequence s, int start, int count,
        int after) {
        // TODO Auto-generated method stub

        }

        public void afterTextChanged(Editable s) {

        }

        });
    }

    class GetPlaces extends AsyncTask<String, Void, ArrayList<String>> {
        @Override
        // three dots is java for an array of strings
        protected ArrayList<String> doInBackground(String... args)
        {
            Log.d("gottaGo", "doInBackground");
            ArrayList<String> predictionsArr = new ArrayList<String>();
            try
            {
                URL googlePlaces = new URL(
                        "https://maps.googleapis.com/maps/api/place/autocomplete/json?input=" +
                        URLEncoder.encode(args[0], "UTF-8") +
                        "&types=geocode&language=en&sensor=true&key=" +
                        getResources().getString(R.string.googleAPIKey));

                Log.d("URL", googlePlaces.toString());

                URLConnection tc = googlePlaces.openConnection();
                BufferedReader in = new BufferedReader(new InputStreamReader(
                        tc.getInputStream()));

                String line;
                StringBuffer sb = new StringBuffer();
                //take Google's legible JSON and turn it into one big string.
                while ((line = in.readLine()) != null) {
                    sb.append(line);
                }
                //turn that string into a JSON object
                JSONObject predictions = new JSONObject(sb.toString()); 
                //now get the JSON array that's inside that object            
                JSONArray ja = new JSONArray(predictions.getString("predictions"));

                for (int i = 0; i < ja.length(); i++) {
                    JSONObject jo = (JSONObject) ja.get(i);
                    //add each entry to our array
                    predictionsArr.add(jo.getString("description"));
                }
            } catch (IOException e)
            {
            Log.e("YourApp", "GetPlaces : doInBackground", e);
            } catch (JSONException e)
            {


    Log.e("YourApp", "GetPlaces : doInBackground", e);
        }

        return predictionsArr;
    }

    //then our post
    @Override
    protected void onPostExecute(ArrayList<String> result){
        Log.d("YourApp", "onPostExecute : " + result.size());
        //update the adapter
        adapter = new ArrayAdapter<String>(getBaseContext(), R.layout.item_list);
        adapter.setNotifyOnChange(true);
        //attach the adapter to textview
        textView.setAdapter(adapter);

        for (String string : result) {
            Log.d("YourApp", "onPostExecute : result = " + string);
            adapter.add(string);
            adapter.notifyDataSetChanged();
        }

        Log.d("YourApp", "onPostExecute : autoCompleteAdapter" + adapter.getCount());
    }

}

}

below is the LOGCAT:

06-24 09:34:41.225: E/AndroidRuntime(10588): FATAL EXCEPTION: main
06-24 09:34:41.225: E/AndroidRuntime(10588): java.lang.NullPointerException
06-24 09:34:41.225: E/AndroidRuntime(10588):    at com.rathinavelu.rea.PlacesListSearchActivity$GetPlaces.onPostExecute(PlacesListSearchActivity.java:156)
06-24 09:34:41.225: E/AndroidRuntime(10588):    at com.rathinavelu.rea.PlacesListSearchActivity$GetPlaces.onPostExecute(PlacesListSearchActivity.java:1)
06-24 09:34:41.225: E/AndroidRuntime(10588):    at android.os.AsyncTask.finish(AsyncTask.java:602)
06-24 09:34:41.225: E/AndroidRuntime(10588):    at android.os.AsyncTask.access$600(AsyncTask.java:156)
06-24 09:34:41.225: E/AndroidRuntime(10588):    at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:615)
06-24 09:34:41.225: E/AndroidRuntime(10588):    at android.os.Handler.dispatchMessage(Handler.java:99)
06-24 09:34:41.225: E/AndroidRuntime(10588):    at android.os.Looper.loop(Looper.java:137)
06-24 09:34:41.225: E/AndroidRuntime(10588):    at android.app.ActivityThread.main(ActivityThread.java:4507)
06-24 09:34:41.225: E/AndroidRuntime(10588):    at java.lang.reflect.Method.invokeNative(Native Method)
06-24 09:34:41.225: E/AndroidRuntime(10588):    at java.lang.reflect.Method.invoke(Method.java:511)
06-24 09:34:41.225: E/AndroidRuntime(10588):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
06-24 09:34:41.225: E/AndroidRuntime(10588):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
06-24 09:34:41.225: E/AndroidRuntime(10588):    at dalvik.system.NativeStart.main(Native Method)

Line 156 takes me to this line in the code:

textView.setAdapter(adapter);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T09:24:35+00:00Added an answer on June 6, 2026 at 9:24 am

    Its Obvious you will get NPE

    You have defined textView two times.

    1 in Oncreate Block (Which is local)

    final AutoCompleteTextView textView = (AutoCompleteTextView)findViewById(R.id.autoCompleteTextView1);
    

    2 In Class member variables.

    public AutoCompleteTextView textView;
    

    When you are trying to setAdapter its looking for member variable textView which is not Initialized.

    The solution is remove final AutoCompleteTextView.

    textView = (AutoCompleteTextView)findViewById(R.id.autoCompleteTextView1);
    

    Infact you need to modify both to following

    adapter = new ArrayAdapter<String>(this,R.layout.item_list);
    textView = (AutoCompleteTextView)findViewById(R.id.autoCompleteTextView1);
    

    If you have defined them in member variables then why you are redefining them locally in onCreate.
    If you try to access them from AsyncTask it will look for uninitialized member variables.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This should be pretty simple, but I am new at LINQ. I have a
This feels like it should be pretty simple, but not much seems to be
So this should be pretty simple, but it isn't working. I have the following
This should be pretty straightforward I would think. I have this string: [quote=Joe Johnson|1]Hi![/quote]
This should be a pretty straightforward question. I have the following code, which forms
This should be a simple one: I have an observableArray object called To in
This should be simple, but I'm getting confused. I have a parent/child tables -
I have a website and I'm trying to rewrite it's url. It's pretty simple,
I know this seems pretty basic, and it should be, but I can't find
This seems like it should be pretty straight forward, but I'm apparently confused. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.