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 8820389
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T05:35:46+00:00 2026-06-14T05:35:46+00:00

I have written a piece of code, to get some information through the website

  • 0

I have written a piece of code, to get some information through the website and display it in a ListView. I am not able to configure why i am getting the force close error. I am pasting the log cat along with the code. Please help me find and solve the error

11-07 21:43:14.729: E/AndroidRuntime(2799): FATAL EXCEPTION: main
11-07 21:43:14.729: E/AndroidRuntime(2799): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.air.test/com.air.test.MainActivity}: java.lang.NullPointerException
11-07 21:43:14.729: E/AndroidRuntime(2799):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
11-07 21:43:14.729: E/AndroidRuntime(2799):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
11-07 21:43:14.729: E/AndroidRuntime(2799):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
11-07 21:43:14.729: E/AndroidRuntime(2799):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
11-07 21:43:14.729: E/AndroidRuntime(2799):     at android.os.Handler.dispatchMessage(Handler.java:99)
11-07 21:43:14.729: E/AndroidRuntime(2799):     at android.os.Looper.loop(Looper.java:130)
11-07 21:43:14.729: E/AndroidRuntime(2799):     at android.app.ActivityThread.main(ActivityThread.java:3683)
11-07 21:43:14.729: E/AndroidRuntime(2799):     at java.lang.reflect.Method.invokeNative(Native Method)
11-07 21:43:14.729: E/AndroidRuntime(2799):     at java.lang.reflect.Method.invoke(Method.java:507)
11-07 21:43:14.729: E/AndroidRuntime(2799):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
11-07 21:43:14.729: E/AndroidRuntime(2799):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
11-07 21:43:14.729: E/AndroidRuntime(2799):     at dalvik.system.NativeStart.main(Native Method)
11-07 21:43:14.729: E/AndroidRuntime(2799): Caused by: java.lang.NullPointerException
11-07 21:43:14.729: E/AndroidRuntime(2799):     at java.util.Arrays$ArrayList.<init>(Arrays.java:47)
11-07 21:43:14.729: E/AndroidRuntime(2799):     at java.util.Arrays.asList(Arrays.java:169)
11-07 21:43:14.729: E/AndroidRuntime(2799):     at android.widget.ArrayAdapter.<init>(ArrayAdapter.java:138)
11-07 21:43:14.729: E/AndroidRuntime(2799):     at com.air.test.MainActivity.onCreate(MainActivity.java:59)
11-07 21:43:14.729: E/AndroidRuntime(2799):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
11-07 21:43:14.729: E/AndroidRuntime(2799):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
11-07 21:43:14.729: E/AndroidRuntime(2799):     ... 11 more

Code

private Spinner spinner1;
private ProgressDialog pd;
private StringBuilder response;
private ListView listView;
private String[] values;
private ArrayAdapter<String> adapter;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    listView = (ListView) findViewById(R.id.mylist);
    spinner1 = (Spinner) findViewById(R.id.spinnerCategory);
    spinner1.setOnItemSelectedListener(new OnItemSelectedListener() {

        public void onItemSelected(AdapterView<?> av, View view, int id,
                long ids) {
            new sendMessageAsync().execute();
        }

        public void onNothingSelected(AdapterView<?> av) {
        }
    });

    adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, android.R.id.text1, values);

    listView.setAdapter(adapter);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

private class sendMessageAsync extends AsyncTask<Void, Void, String> {

    @Override
    protected void onPreExecute() {
        pd = ProgressDialog.show(MainActivity.this, null, "Loading...",
                true, true);
    }

    @Override
    protected void onCancelled() {
        Toast.makeText(getApplicationContext(),
                "Message Sending Cancelled", Toast.LENGTH_LONG).show();
    }

    @Override
    protected String doInBackground(Void... arg0) {
        try {
            doInBg();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(String result) {
        pd.dismiss();
    }
}

public void doInBg() {
    try {
        final String msgURL = "http://example.com/message?category="
                + String.valueOf(spinner1.getSelectedItem().toString()
                        .replace(" ", "%20"));
        URLConnection connection = new URL(msgURL).openConnection();
        connection.setRequestProperty("Accept-Charset", "UTF-8");
        InputStream responseStream = connection.getInputStream();
        BufferedReader br = new BufferedReader(new InputStreamReader(
                responseStream));
        response = new StringBuilder();
        String line;
        while ((line = br.readLine()) != null) {
            response.append(line);
        }
        values = String.valueOf(response).split("<br/>");
        adapter.notifyDataSetChanged();
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}
  • 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-14T05:35:47+00:00Added an answer on June 14, 2026 at 5:35 am

    You need to set your String array values before attempting to create the adapter.

    This line will fail because values is null:

    adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, android.R.id.text1, values);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written this piece of CF code to get and display the data
What I have written a small piece of code to find and remove if
I have just written a small piece of code and it struck me that
I have a piece of code that was written by someone else before the
I have a piece of performance critical code written with pointers and dynamic memory.
I have written a piece of code that detects installed application in android and
You have some code you want to remove associated with an obsolete piece of
I have written a piece of code : public class Child{ int y ;
I have written a piece of code for queue. #include <iostream> using namespace std;
I have written this piece of code here and I have linked it alright

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.