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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T14:54:31+00:00 2026-06-15T14:54:31+00:00

public void directionSpinner (String directions []) { //Does some stuff to assign namedDirections [0]

  • 0
public void directionSpinner (String directions []) {
        //Does some stuff to assign namedDirections [0] and namedDirections[1] a value.

        dirSpinner = (Spinner) findViewById(R.id.route_direction_spinner); //THIS IS LINE 87
        dirSpinner.setOnItemSelectedListener(this);
        ArrayAdapter<CharSequence> adapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item);

        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        adapter.add(namedDirections[0]);
        adapter.add(namedDirections[1]);
        dirSpinner.setAdapter(adapter);

        Log.i(TAG, "spinner populated");
}

LogCat:

12-09 20:50:13.497: E/AndroidRuntime(857): FATAL EXCEPTION: main
12-09 20:50:13.497: E/AndroidRuntime(857): java.lang.NullPointerException
12-09 20:50:13.497: E/AndroidRuntime(857):  at android.app.Activity.findViewById(Activity.java:1839)
12-09 20:50:13.497: E/AndroidRuntime(857):  at com.nicotera.colton.londontransitguide.RoutesActivity.directionSpinner(RoutesActivity.java:87)
12-09 20:50:13.497: E/AndroidRuntime(857):  at com.nicotera.colton.londontransitguide.MyInnerClass.onPostExecute(RoutesActivity.java:142)
12-09 20:50:13.497: E/AndroidRuntime(857):  at com.nicotera.colton.londontransitguide.MyInnerClass.onPostExecute(RoutesActivity.java:1)

Anyways, route_direction_spinner is defined correctly. directionSpinner() runs after an AsyncTask is completed.

Entire Code:

package com.nicotera.colton.londontransitguide;

import java.io.IOException;
import java.text.DecimalFormat;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.jsoup.*;
import org.jsoup.nodes.Attributes;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;

public class RoutesActivity extends Activity implements OnItemSelectedListener {
    Spinner dirSpinner;
    Spinner routeSpinner;


    static String [] namedDirections = new String [2];
    private static final String TAG = "RoutesActivity";

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_routes);
        dirSpinner = (Spinner) findViewById(R.id.route_direction_spinner); // Create an ArrayAdapter using the string array and a default spinner layout
        routeSpinner = (Spinner) findViewById(R.id.route_name_spinner); // Create an ArrayAdapter using the string array and a default spinner layout
        routeSpinner.setOnItemSelectedListener(this);
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.routes_array, android.R.layout.simple_spinner_item);
        // Specify the layout to use when the list of choices appears
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); // Apply the adapter to the spinner
        routeSpinner.setAdapter(adapter);
        Log.i(TAG, "second spinner populated");
    }

    public void onItemSelected(AdapterView<?> parent, View view, 
            int pos, long id) {
        Log.i(TAG, "Item selected");
        //DecimalFormat df = new DecimalFormat("00##");
        int tempPos = pos;
        Log.i(TAG, ("Position of selected item: " + tempPos));
        int routeSelected;
        if (tempPos < 17)
            routeSelected = (tempPos + 1);
        else if (tempPos >= 17 && tempPos < 29)
            routeSelected = (tempPos + 2);
        else
            routeSelected = (tempPos + 3);
        String temp;
        if (routeSelected < 10)
            temp = ("0") + routeSelected;
        else
            temp = ("") + routeSelected;
        String url = "http://www.ltconline.ca/WebWatch/MobileAda.aspx?r=" + temp;
        new MyInnerClass().execute(url);

    }

    public void directionSpinner (String directions []) {
        int temp;
        for (int i = 1; i <=2; i++)
        {
            temp = Integer.parseInt(directions[i]);
            if (temp == 1)
                namedDirections[(i-1)] = "Eastbound";
            else if (temp == 2)
                namedDirections[(i-1)] = "Northbound";
            else if (temp == 3)
                namedDirections[(i-1)] = "Southbound";
            else if (temp == 4)
                namedDirections[(i-1)] = "Westbound";               
        }
        //setContentView(R.layout.activity_routes);

        dirSpinner.setOnItemSelectedListener(this);
        ArrayAdapter<CharSequence> adapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        adapter.add(namedDirections[0]);
        adapter.add(namedDirections[1]);
        dirSpinner.setAdapter(adapter);
        Log.i(TAG, "spinner populated");
    }



    public void onNothingSelected(AdapterView<?> parent) {
        // Another interface callback
    }
}

class MyInnerClass extends AsyncTask<String, Void, String> {
    String [] directions = new String [3];
    String [] directionNames = new String [3];
    private static final String TAG = "RoutesActivity";
       @Override
       protected void onPreExecute() {
       super.onPreExecute();

       }

       @Override
       protected void onPostExecute(String result) {
       super.onPostExecute(result);
       RoutesActivity tc = new RoutesActivity();
       tc.directionSpinner(directions);
       }

    @Override
    protected String doInBackground(String... params) {
        try{
            Pattern routeDirPattern = Pattern.compile("\\&d=(\\d{1,2})");
            Connection conn = Jsoup.connect(params[0]);
            Document doc = conn.get();
            int i = 0;
            Elements routeLinks = doc.select("a[href]");
            for (Element routeLink : routeLinks) {
                i = (i + 1);
                String name = routeLink.text();
                Attributes attrs = routeLink.attributes();
                String href = attrs.get("href");
                Matcher m = routeDirPattern.matcher(href);
                if (m.find()) {
                        String number = m.group(1);
                        directions [i] = number;
                        directionNames [i] = name;
                        Log.i(TAG, directionNames [i]);
                }
            }
            }catch(Exception e){Log.d("doinbackground exception", e.toString());}
           return ("Done");
    }
}

Basically, this grabs some values from a transit website. This section of code so far just gets the direction of the route based off the route selected.

  • 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-15T14:54:32+00:00Added an answer on June 15, 2026 at 2:54 pm

    (Your directionSpinner() method is different in the two snippets you’ve posted, and I’m working using the one added later, but this solution should work for both snippets, assuming the problem is the same)

    Your problem is that you’re not following the Activity lifecycle properly.

    When you call:

    RoutesActivity tc = new RoutesActivity();
    tc.directionSpinner(directions);
    

    You are actually creating a brand new instance of RoutesActivity. And since you’re calling it as a standard Java class, it never follows the Activity lifecycle, which means that onCreate() is never called, which in turn means that your two spinner objects remain null (as you assign values to them in onCreate()).

    Due to this, when you use:

    dirSpinner.setOnItemSelectedListener(this);
    

    dirSpinner is actually null resulting in a NullPointerException.

    Instead, try using:

    RoutesActivity.this.directionSpinner(directions);
    

    Instead of:

    RoutesActivity tc = new RoutesActivity();
    tc.directionSpinner(directions);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

public void Button1_Click(object sender, EventArgs e) { String a = DropDownList1.SelectedItem.Value; String b =
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.map); TextView lattv=(TextView)findViewById(R.id.lat); TextView lngtv=(TextView)findViewById(R.id.lng); JSONParstring jParser =
public void EditAndSave(String fileName) { Bitmap b = new Bitmap(fileName); /** * Edit bitmap
public void setRange(int which) { if (datelimitsset == 1) { if (startPicker.Value >= endPicker.Value
public void responsePost(Editable editable) { TableLayout chatbox = (TableLayout)findViewById(R.id.chatbox); TableRow tr1 = new TableRow(this);
public void RestoreDatabase(String databaseName, String filePath, String serverName, String userName, String password, String dataFilePath,
public void Stream(String FOLDER_PATH, int port){ File myDir = new File(FOLDER_PATH); File[] files =
public void ExecStoredProc(string strprocName, SqlConnection sqlConnect, List<string> Paramvalues) { if (ConnectToDB() == true) {
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.uniqueid); simID = (TextView) findViewById(R.id.text2); simIMSI = getSubscriberId().toString();
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); lView = (ListView) findViewById(R.id.list1); PackageManager pm =

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.