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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T20:52:08+00:00 2026-06-17T20:52:08+00:00

Hello I am having a problem with my listivew. Please check my code below:

  • 0

Hello I am having a problem with my listivew. Please check my code below:

public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
    // TODO Auto-generated method stub
    String selected;

    //selected = parent.getItemAtPosition(position).toString();

    selected = parent.getItemAtPosition(lv.getFirstVisiblePosition() + position).toString();

    if( selected.equals("Apple") ){
        Intent apple = new Intent(Fruits.this, Apples.class);
        startActivity(apple);
    }
    else if( selected.equals("Apricot") ){
        Intent apricot = new Intent(Fruits.this, Apricots.class);
        startActivity(apricot);
    }
    else if( selected.equals("Avocado") ){
        Intent avocado = new Intent(Fruits.this, Avocado.class);
        startActivity(avocado);
    }
    else if( selected.equals("Banana") ){
        Intent banana = new Intent(Fruits.this, Banana.class);
        startActivity(banana);
    }
    else if( selected.equals("Blackberry") ){
        Intent blackberry = new Intent(Fruits.this, Blackberries.class);
        startActivity( blackberry );
    }
    else if( selected.equals("Blueberry") ){
        Intent blueberry = new Intent(Fruits.this, Blueberry.class);
        startActivity(blueberry);
    }
    else if( selected.equals("Cranberry") ){
        Intent cranberry = new Intent(Fruits.this, Cranberry.class);
        startActivity(cranberry);
    }
    else if( selected.equals("Figs") ){
        Intent fig = new Intent(Fruits.this, Figs.class);
        startActivity(fig);
    }
    else if( selected.equals("Grapefruit") ){
        Intent grapefruit = new Intent(Fruits.this, Grapefruit.class);
        startActivity(grapefruit);
    }
    else if( selected.equals("Kiwi") ){
        Intent kiwi = new Intent(Fruits.this, Kiwi.class);
        startActivity(kiwi);
    }
    else if( selected.equals("Lemon") ){
        Intent lemon = new Intent(Fruits.this, Lemons.class);
        startActivity(lemon);
    }
    else if( selected.equals("Melon") ){
        Intent melon = new Intent(Fruits.this, Melon.class);
        startActivity(melon);
    }
    else if( selected.equals("Orange") ){
        Intent orange = new Intent(Fruits.this, Orange.class);
        startActivity(orange);
    }
    else if( selected.equals("Papaya") ){
        Intent papaya = new Intent(Fruits.this, Papaya.class);
        startActivity(papaya);
    }
    else if( selected.equals("Peach") ){
        Intent peach = new Intent(Fruits.this, Peaches.class);
        startActivity(peach);
    }
    else if( selected.equals("Pear") ){
        Intent pear = new Intent(Fruits.this, Pears.class);
        startActivity(pear);
    }
    else if( selected.equals("Pineapple") ){
        Intent pineapple = new Intent(Fruits.this, Pineapple.class);
        startActivity(pineapple);
    }
    else if( selected.equals("Plum") ){
        Intent plum = new Intent(Fruits.this, Plum.class);
        startActivity(plum);
    }
    else if( selected.equals("Prune") ){
        Intent prune = new Intent(Fruits.this, Prunes.class);
        startActivity(prune);
    }
    else if( selected.equals("Raspberry") ){
        Intent raspberry = new Intent(Fruits.this, Raspberry.class);
        startActivity(raspberry);
    }   
    else if( selected.equals("Rhubarb") ){
        Intent rhubard = new Intent(Fruits.this, Rhubard.class);
        startActivity(rhubard);
    }
    else if( selected.equals("Strawberry") ){
        Intent strawberry = new Intent(Fruits.this, Strawberries.class);
        startActivity(strawberry);
    }

} // end of OnItemClick method


final String[] classes = new String[] { 
        "Apple", // #0
        "Apricot", // #1
        "Avocado", // #2
        "Banana", // #3
        "Blackberry", // #4
        "Blueberry", // #5
        "Cranberry", // #6
        "Figs", // #7
        "Grapefruit", // #8
        "Kiwi", // #9
        "Lemon", // #10
        "Melon", // #11
        "Orange", // #12
        "Papaya", // #13
        "Peach", // #14
        "Pear", // #15
        "Pineapple", // #16
        "Plum", // #17
        "Prune", // #18
        "Raspberry", // #19
        "Rhubarb", // #20
        "Strawberry"  // #21
        };

Whenever I ran the application, it throws nullpointerexception in this line:

selected = parent.getItemAtPosition(lv.getFirstVisiblePosition() + position).toString();

What’s wrong with my code? Any help is truly appreciated. thanks.

  • 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-17T20:52:09+00:00Added an answer on June 17, 2026 at 8:52 pm

    You’re right, the error code is in the line:

    selected = parent.getItemAtPosition(lv.getFirstVisiblePosition()+position).toString();
    

    Because of the function: getFirstVisiblePosition() + position, sometimes will bigger than size of parent, your code will become: null.toString(), and of course, NullPointerException. You can check the document about the function :getFirstVisiblePosition in the Link to AdapterView here

    Edited: I think you should not use this line anymore, just use: selected = classes[position]. That will solve your problem!

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

Sidebar

Related Questions

Hello i am having a problem with resuming a thread my code is public
Hello I'm having a problem here. The code looks like this. private void Form3_Load(object
Hello, I have been having a problem with this code for the last 3
Hello I just started CodeIgniter . I am having problem in loading view. My
hello I am having problem related to https:// . I have used FB.getLoginStatus(function(response) function
Hello I am having a problem with the numbering of figures using Latex, I
Hello I am a new user to Python and I am having problem doing
Hello I'm having an issue with this check box : <input type=checkbox id=hideTempSeries checked=checked
Hello I'm having trouble with the following piece of code: <h:selectOneMenu id=selectTipoAutorizacion value=#{autorizacion.codigoTipoAutorizacion} required=true>
Hello i am just Studying 3-tier Application. I am now having a problem with

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.