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

  • Home
  • SEARCH
  • 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 6838203
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T23:34:07+00:00 2026-05-26T23:34:07+00:00

I am working on a Android code that returns me String values that I

  • 0

I am working on a Android code that returns me String values that I store in a String array.I want to use this String array to populate one of my Spinners.

My main intention is that depending on what the user selects in 1st spinner , I want to populate my 2nd spinner using my String array.

However, when I try I am getting a NullPointerException even when I am populating the adapter

My Android code:

public class Mark2 extends Activity 
{
  private static final String SOAP_ACTION = "http://tempuri.org/getData";
  private static final String METHOD_NAME = "getData";
  private static final String NAMESPACE = "http://tempuri.org/";
  private static final String URL = "http://10.0.2.2/getsubject/Service1.asmx"; 


  private Spinner s;
  private Spinner s2;
  private Spinner s3;

@Override
    protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);

    setContentView(R.layout.mainspinner);

    s = (Spinner) findViewById(R.id.spinner1);
    s2 = (Spinner) findViewById(R.id.spinner2);


    String[] myList4=call();
    for(int i=0;i<myList4.length;i++)
    System.out.println(myList4[i]);

    String[] myList = new String[] {"First half","Second Half"};
    String[] myList2=new  String[] {
                                           "abc",
                                   "ghf", 
                                   "ijk",                             
                                   };

    String[] myList3=new String[]  {
                            "john",
                            "ann", 
                            "joe",

                                   };

   s.setAdapter(new ArrayAdapter<String> (this,android.R.layout.simple_spinner_item, myList));
    s2.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, myList2));


    final ArrayAdapter<String> adapter3 = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, myList4);
    final ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, myList3);


    s.setOnItemSelectedListener(new OnItemSelectedListener()
    {

        @Override
        public void onItemSelected(AdapterView<?> parent, View v,int pos, long id) 
        {

        if(parent.getItemAtPosition(pos).toString().equalsIgnoreCase("First half"))
        {
           s2.setSelection(pos);
           s3.setAdapter(adapter3);
        }
        else
        {
            s2.setAdapter(adapter2);
            s2.setSelection(pos);


        }

        }



        @Override
        public void onNothingSelected(AdapterView<?> arg0) {


        }});


}

public String[] call()
{
    SoapPrimitive responsesData = null; 

    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); 

    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope( 
    SoapEnvelope.VER11); 
    envelope.dotNet = true; 
    envelope.setOutputSoapObject(request);

    HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

    androidHttpTransport.debug = true; 

    try {

    androidHttpTransport.call(SOAP_ACTION, envelope);

    responsesData = (SoapPrimitive) envelope.getResponse(); 
    System.out.println(" --- response ---- " + responsesData); 

    } catch (SocketException ex) { 
    ex.printStackTrace(); 
    } catch (Exception e) { 
    e.printStackTrace(); 
    } 

    System.out.println( " ----" + responsesData );

    String serviceResponse= responsesData .toString(); 


    String[] temp; 
    String delimiter = "#"; 
    temp= serviceResponse.split(delimiter);
    System.out.println( " ---- length ---- " + temp.length); 

    return temp; 


   }      


  }

My logcat:

 11-17 15:51:09.668: DEBUG/dalvikvm(125): GC_EXPLICIT freed 1220 objects / 68272 bytes in 164ms
 11-17 16:20:29.649: ERROR/AndroidRuntime(620): java.lang.NullPointerException
 11-17 16:20:29.649: ERROR/AndroidRuntime(620):     at com.example.Mark2$1.onItemSelected(Mark2.java:88)
 11-17 16:20:29.649: ERROR/AndroidRuntime(620):     at android.widget.AdapterView.fireOnSelected(AdapterView.java:864)
 11-17 16:20:29.649: ERROR/AndroidRuntime(620):     at android.widget.AdapterView.access$200(AdapterView.java:42)
 11-17 16:20:29.649: ERROR/AndroidRuntime(620):     at  android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:830)
 11-17 16:20:29.649: ERROR/AndroidRuntime(620):     at android.os.Handler.handleCallback(Handler.java:587)  
 11-17 16:20:29.649: ERROR/AndroidRuntime(620):     at android.os.Handler.dispatchMessage(Handler.java:92)

Edit: Line no: 88 is s3.setAdapter(adapter3);

  • 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-05-26T23:34:08+00:00Added an answer on May 26, 2026 at 11:34 pm

    NullPointerException occurs when one of the object is null and you are trying to use it.

    From your code i found that you have not initialized s3 of Spinner.

    You just forgot to add :

    s3 = (Spinner) findViewById(R.id.spinner3);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got this code that i'm working on : <?xml version=1.0 encoding=utf-8?> <RelativeLayout xmlns:android=http://schemas.android.com/apk/res/android
I am working on an Android code that on click of a button inserts
In my Android program, I have some code that downloads a file. This works
I am working on an Android app that utilizes the Google Maps API MapView,
I'm working on an Android app in which I would like to use multi-touch.
I'm currently working on an Android app. I would like to use the app
I have a piece of code that aims to launch the android camera, let
I'm working on an android app that has to send and receive information from
I've been working on a Android app that posts workouts to Dailymile.com. To authorise
I'm writing an Android app that is basically a ListView populated with an array

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.