function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
EIE50EIE50 

Basic List Understanding

Hello,

 

I have a very basic question on Lists.

 

If any one can give me an explanation in very simple terms it would be really great.

 

List<Integer> myList = new List<Integer>();
	myList.add(47);
	myList.add(20);
	myList.add(43);
	System.assertEquals(myList.get(1));

 

Compile error at line 5 column 2
Method does not exist or incorrect signature: System.assertEquals(Integer)

 I thought, when i added three elements to a list which is of type integer (would basically look like this in an array myList ={47, 20, 43} ) and just expected the system assert to return me 20 as it is in the [1] index of the list. Why am i getting this incorrect signature error?

 

Also, i would like to know how to go about Null pointer exception errors and List index out of bound errors while dealing with the list.

 

Thanks.

LakshmanLakshman

Hi

 

System.assertEquals() is used to validate two values are equal. Basically it is used in test method. This method asserts that the first two arguments, x and y are the same, if they are not a runtime exception is thrown.

 

For Example:

 

Account ac=new Account(name=’testName’);
Insert ac;
Account acc=[select id,name from account where id=:ac.id];
System.assertEquals(acc.name, ‘testName’);

 

If you want to get value 20 in from your list debug log you will have to do Sytem.debug(myList.get(1));

System.debug(); helps us to debug our code just as System.out.println() of Java. The debug information can be found in System log page.

Null pointer exception will come when your list is empty. List index out of bound will come when you try access index which does not exist.

You can bypass these exception as follows:

 

Null pointer exception

Check size of list before accessing it like:

if(myList.size() > 0)

{

//do your operations

}



List index out of bound

Again check the size of list and verify whether it is greater than the index which want to access.

 

Let me know if you have any questions.

 

Regards,

Lakshman

BritishBoyinDCBritishBoyinDC

And just to be clear, this would work if you needed to do an assert:

 

List<Integer> myList = new List<Integer>();
	myList.add(47);
	myList.add(20);
	myList.add(43);

	System.assertEquals(20, myList.get(1));

 

EIE50EIE50

Hi,

 

Thank you for the replies.

 

Can you throw an example on list index out of bounds please if you dont mind.

 

For example, if i have a list which has 6 integers, but then i try to get(7), i understand i will get list index out of bounds error, how can i prevent this using size() ? should i say Size() not greater than 6 ??

 

An example would help.

 

Once agian thank you for your time and explanation.

BritishBoyinDCBritishBoyinDC

Use the Size to ensure you don't reference an invalid index e.g.

 

List<Integer> myList = new List<Integer>();
	myList.add(47);
	myList.add(20);
	myList.add(43);

for (Integer i = 0; i < myList.size();i++ ) {
system.debug('THIS VAL IS: ' + myList.get(i));
}

 

 

EIE50EIE50

Awesome, thank you!!!!!!!!!!