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.


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

Results 1 to 2 of 2

Thread: If arraylist value is null

  1. #1
    Junior Member
    Join Date
    Nov 2010
    My Mood
    Thanked 0 Times in 0 Posts

    Default If arraylist value is null

    So I have written a for loop that will go through my arraylist and should check if the value is empty. When it finds an empty arraylist value it will add the new vertex. So far it is like this:

    for(int i = 0; i < Vertices.size()+1; i++){
    						if(Vertices.get(i) == null){   <------------------------------------------------------
    							Display.append(" Vertices " + Vertices.get(i+1) + " added" + newline);

    So it will check if the arraylist is empty, if it isn't it will check if the value at position 'i' in the arraylist is null. If it isn't null it will add 1 to 'i' and try again until it finds a null value. The problem is with the line that checks if the value is null but I don't know why that is wrong or how to fix it.

    The problem is that I am getting an exception when it tries to add the second vertex

  2. #2
    Junior Member JavaManNoob's Avatar
    Join Date
    Oct 2012
    Thanked 0 Times in 0 Posts

    Default Re: If arraylist value is null

    First thing looking at your code.. The for statement your going to want Vertices.size(); without the +1. The size() method takes into account that the first value will be at index 0. Throws: IndexOutOfBoundsException - if the index is out of range (index < 0 || index >= size())

Similar Threads

  1. ArrayList<Class> is returning null
    By havinFun in forum What's Wrong With My Code?
    Replies: 19
    Last Post: April 14th, 2012, 04:59 PM
  2. Array with null (same for Arraylist)
    By Alex L in forum What's Wrong With My Code?
    Replies: 5
    Last Post: December 5th, 2011, 12:51 AM
  3. string==null or string.equals(null) problem
    By csharp100 in forum What's Wrong With My Code?
    Replies: 31
    Last Post: November 4th, 2011, 08:17 AM
  4. Ordering ArrayList by 3 conditions as you add to ArrayList
    By aussiemcgr in forum Collections and Generics
    Replies: 4
    Last Post: July 13th, 2010, 02:08 PM
  5. [SOLVED] Extracting an How to ArrayList from an ArrayList and convert to int??
    By igniteflow in forum Collections and Generics
    Replies: 2
    Last Post: August 16th, 2009, 01:11 PM

Tags for this Thread