Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: ArrayIndexOutOfBounds in Android - help with debuuging

  1. #1
    Junior Member
    Join Date
    Dec 2013
    Posts
    22
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default ArrayIndexOutOfBounds in Android - help with debuuging

     
    public class MainActivity extends ListActivity {
    	private ArrayList<Friend> friendList;
    	private EditText et;
    	private int[] imageBlock;
    	private String[] nameBlock;
    	private String[] descBlock;
     
    	int textlength=0;
    	private Resources res;
    	private ListView lv;
     
    	public void onCreate(Bundle savedInstanceState)
    	{
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.activity_main);
    		res = getResources();
    		et 	= 	(EditText) findViewById(R.id.EditText01);
    		lv	=	(ListView) findViewById(android.R.id.list);
     
    		friendList = new ArrayList<Friend>();
     
    		nameBlock = res.getStringArray(R.array.names);
    		descBlock = res.getStringArray(R.array.descriptions);
    		imageBlock = res.getIntArray(R.array.images);
     
    		if(friendList == null) {
    			Log.i("NullPointer", "Null Pointer in Friend List");
    		}
     
    		int size = nameBlock.length;
    		for(int i = 0 ; i < size; i++) {
    			friendList.add(new Friend(nameBlock[i], descBlock[i], imageBlock[i]));
    		}
     
    		setListAdapter(new bsAdapter(this));
     
     
     
    		et.addTextChangedListener(new TextWatcher()
    		{
    			public void afterTextChanged(Editable s) { }
    			public void beforeTextChanged(CharSequence s, int start, int count, int after) { }
    			public void onTextChanged(CharSequence s, int start, int before, int count)
    			{
    				friendList.clear();
    				textlength = et.getText().length();
     
    				for (int i = 0; i < nameBlock.length; i++)
    				{
    					if (textlength <= nameBlock[i].length())
    					{
    						if(nameBlock[i].toLowerCase().contains(et.getText().toString().toLowerCase().trim())) {
    							Log.i("NullPointer", "Null Pointer in Friend List");
    							friendList.add(new Friend(nameBlock[i], descBlock[i], imageBlock[i]));
    						}
                          }
    				}
    				AppendList(friendList);
    				}
    				});
     
    	}
    	public void AppendList(ArrayList<Friend> freind) {
    		setListAdapter(new bsAdapter(this));
    	}	
    	public class bsAdapter extends BaseAdapter
        {
            Activity cntx;
            public bsAdapter(Activity context)
            {
                this.cntx=context;
     
            }
            public int getCount()
            {
                return friendList.size();
            }
            public Object getItem(int position)
            {
                return friendList.get(position);
            }
            public long getItemId(int position)
            {
                return friendList.size();
            }
     
            public View getView(final int position, View convertView, ViewGroup parent)
            {
                View row=null;
     
                LayoutInflater inflater=cntx.getLayoutInflater();
                row=inflater.inflate(R.layout.search_list_item, null);
     
                TextView   tv	=	(TextView)	row.findViewById(R.id.title);
                ImageView im	=	(ImageView)	row.findViewById(R.id.imageview);
     
                tv.setText(friendList.get(position).getName());
                im.setImageDrawable(getResources().getDrawable(friendList.get(position).getImage()));       
            return row;
            }
        }
    }


    I keep getting an ArrayIndexOutOfBounds, but my lack of experience in LogCat kind of doesn't help... Could somebody just have a look at my code. And maybe assist me in someway? I'm building a FriendsList using a ListView and intergrating a search filter feature.
    Thanks, Peter.


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: ArrayIndexOutOfBounds in Android - help with debuuging

    Any idea where the exception is being thrown?
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: ArrayIndexOutOfBounds in Android - help with debuuging

    Can you copy the contents of the Logcat that shows the error and paste it here? The stack trace in the logcat will point to where the error happened.
    If you don't understand my answer, don't ignore it, ask a question.

  4. #4
    Junior Member
    Join Date
    Dec 2013
    Posts
    22
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: ArrayIndexOutOfBounds in Android - help with debuuging

    Quote Originally Posted by aussiemcgr View Post
    Any idea where the exception is being thrown?
    I cracked the problem. It was occurring at line 62:
     friendList.add(new Friend(nameBlock[i], descBlock[i], imageBlock[i]));

    The reason: I have an xml. string-array that had 1 value. And I was looping 10 times. Causing an array index out of bounds because I set the String[] array to be of size 1.

    --- Update ---

    Quote Originally Posted by Norm View Post
    Can you copy the contents of the Logcat that shows the error and paste it here? The stack trace in the logcat will point to where the error happened.
    Hi Norm, hope your having a great day. I have fixed this error now. The problem was that I was trying to insert 10 values into an array ready to be pushed into an ArrayList. The problem was, that I had set the array to be of size 1. But pushing 10 elements into it caused the Exception.

Similar Threads

  1. ArrayIndexOutOfBounds
    By 3sbwya in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 10th, 2013, 06:39 PM
  2. android
    By game06 in forum Android Development
    Replies: 0
    Last Post: August 8th, 2013, 11:33 PM
  3. ArrayIndexOutOfBounds Help
    By connorlm3 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 8th, 2013, 12:36 AM
  4. I need help with ArrayIndexOutOfBounds exception!
    By jameschristian in forum Exceptions
    Replies: 6
    Last Post: November 3rd, 2012, 12:32 AM