※この記事は英語版を翻訳しており、内容は後日更新される可能性があります。最新の内容は英語版を参照してください。表示言語は画面右下の言語名から切り替えられます。
この記事では、「System.ListException: List index out of bounds: 0」エラーが発生する理由と、それを解決する方法について説明します。
あるリストオブジェクトの 0 番目の要素にアクセスしようとした場合、0 番目にデータが存在しなければ、エラーが発生します。
リストの要素にアクセスする前に、そのリストにレコードが存在するかどうかを確認することをお勧めします。
リストの 0 番目のインデックスを参照する前に、そのリストが空であるかどうかのチェックを実行する必要があります。リストのレコードにアクセスしようとするたびに、まず、それが空であるかどうかをチェックします。
List<Account> lstAccount = [Select Id, Name from Account Limit 10];
// Before processing the list check if its empty or not
// It will go inside the loop only if the List is having values in it.
if(lstAccount.size() > 0) {
// Do something with lstAccount[0].Name
}
// If you try to access lstAccount[0] without empty check then it will obviously throw that error!!
クエリがの結果が 0 件であった場合などには「System.ListException: List index out of bounds: 0」を受信します。
000385743

We use three kinds of cookies on our websites: required, functional, and advertising. You can choose whether functional and advertising cookies apply. Click on the different cookie categories to find out more about each category and to change the default settings.
Privacy Statement
Required cookies are necessary for basic website functionality. Some examples include: session cookies needed to transmit the website, authentication cookies, and security cookies.
Functional cookies enhance functions, performance, and services on the website. Some examples include: cookies used to analyze site traffic, cookies used for market research, and cookies used to display advertising that is not directed to a particular individual.
Advertising cookies track activity across websites in order to understand a viewer’s interests, and direct them specific marketing. Some examples include: cookies used for remarketing, or interest-based advertising.