0

When trying out a proposed solution to my previous question, Displaying multiple names from different objects from a query in VF, while there is no error in the developer console, I am receiving an error when I preview the page. The query without the where portion gets me the data I need (I tested it without the where portion in the query editor), but I believe there is an issue stemming from the where portion of the query. The controller is:

public with sharing class MyExtension
{
    public String ProductName {get;set;}
    public List<Part__c> parts { get; private set; }
    public PartsInProduct__c[] partsInProducts {get; private set;}


    public MyExtension(ApexPages.StandardController controller)
    {
        ProductName  = ApexPages.CurrentPage().getparameters().get('LGFProduct__c.name');
        //parts = [SELECT name,id, (SELECT LGFProduct__r.name, LGFProduct__r.id FROM PartsinProducts__r WHERE LGFProduct__r.Name  =: ProductName) FROM Part__c ];
        partsInProducts = [SELECT name, id, 
               (SELECT Part__r.name, Part__r.id 
                 FROM PartsinProducts__r 
                 )
            FROM LGFProduct__c where name = :ProductName ][0].PartsinProducts__r;//Part where I believe error occurs
    }
}

How do I make the array index in bounds?

I understand what an array out of bounds error is but what I don't get is the line:

FROM LGFProduct__c where name = :ProductName ][0].PartsinProducts__r;

Doesn't the query end after the ] bracket? What is the purpose and meaning of the [0].PartsinProducts__r? I think my issue stems from my lack of understanding of this line of code. Is there nothing in the array? I'm quite sure 0 index of an array indicates the first element in that array.

The controller can be seen in the link but I will put it here anyways:

<apex:page standardController="Service_Case__c" extensions="MyExtension">
    <apex:pageBlock >
        <apex:pageBlockTable value="{!partsInProducts}" var="pip">
            <apex:column value="{!pip.Part__r.Name}"/> 
            <apex:column value="{!pip.Part__r.id}"/> 
        </apex:pageBlockTable>
    </apex:pageBlock>
</apex:page>

This is the error message I get from the two huge code blocks above: enter image description here

The query that I run in the query editor and its results:enter image description here

2
public MyExtension(ApexPages.StandardController controller)
    {
        ProductName  = ApexPages.CurrentPage().getparameters().get('LGFProduct__c.name');
        //parts = [SELECT name,id, (SELECT LGFProduct__r.name, LGFProduct__r.id FROM PartsinProducts__r WHERE LGFProduct__r.Name  =: ProductName) FROM Part__c ];
        partsInProducts = [SELECT name, id, 
               (SELECT Part__r.name, Part__r.id 
                 FROM PartsinProducts__r 
                 )
            FROM LGFProduct__c where name = :ProductName ][0].PartsinProducts__r;//Part where I believe error occurs
    }

Will throw an List Index Out of bounds error if no records are returned.

You example of FROM LGFProduct__c where name = :ProductName ]; will not result in a "List has not rows for assignment" error if your code in as you have it in your example:

public PartsInProduct__c[] partsInProducts {get; private set;}

So I am a bit confused on what your code looks like when you get an which error.

As for the

Doesn't the query end after the ] bracket? What is the purpose and meaning of the [0].PartsinProducts__r?

The [0] is the index of the records returned. If no records are returned then the list is empty. Empty lists have NO indexes at all and [0] will result in a list index out of bounds error.

So you need to ensure that when you query you assign the results to a list type and check for it being empty before working with it.

Liberal use of debugs will help you find your issue. Likely the get param is returning null

  • If I change PartsinProduct__c to a list, I get an illegal list to list assignment. That was what I was trying to state. But if the get parameter is returning null, is there some syntax error I am not aware of? I'm quite sure there is a Product on the page, so I'm not sure why it's returning null. – user1470901 Aug 30 '16 at 23:43
  • I did update the question so that the code that I was using is more clear. – user1470901 Aug 30 '16 at 23:50
  • And the issue is still the same, no records are returned from your query and you are blindly using [0].xxxxxx – Eric Aug 30 '16 at 23:52
  • Well that's was kind of what I was trying to say, it was suggested to me by an answer and I have no idea what it meant. The query works fine though without the where and I placed an example above, so I am confused as to whether the get parameter is returning null or my query not working because of the where? – user1470901 Aug 30 '16 at 23:57
  • read the last line of my answer :) – Eric Aug 30 '16 at 23:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.