+ Start a Discussion
GGill31GGill31 

Visualforce Error - Apex

Hi There,
I'm trying to input multiple Barcode_ID's for given Item object for a given employee. The visualforce page requires an employee to input their Employee_ID and enter multiple Barcode_ID's (taking in multiple keyboard keys until enter is hit for every barcode id). The Borrow Object has lookup relationship with Employee and Item objects on Employee_ID and Item_ID autonumber fields from Employee and Item object respectively. On executing the following code, its giving me the following error
'System.NullPointerException: Attempt to de-reference a null object'
Error is in expression '{!insertRecord}' in component <apex:commandButton> in page checkoutitems: Class.BorrowController.createBorrowRecord: line 10, column 1
Class.BorrowController.insertRecord: line 38, column 1

This is my code:
 Visualforce page - Checkout.vfp
<apex:page controller="BorrowController" showHeader="false" applyHtmlTag="false" applyBodyTag="false" docType="html-5.0">
    <html>
        <head>
            <body>
                <apex:pageBlock >
                    <apex:pageBlockSection >
                        <label for = "employeeIDText">Employee ID: </label>
                        <input type = "text" id = "employeeIDText" value = "{!employee_ID}"/>
                    </apex:pageBlockSection>
                    <apex:pageBlockSection >
                        <label for = "inputText">Barcode ID:</label>
                        <input type = "text" id = "inputText"/>
                    </apex:pageBlockSection>
                    <apex:pageBlockSection >
                        <span id = "outputText"/>
                    </apex:pageBlockSection>
                    <apex:pageBlockSection >
                        <span id="addedItems"/>
                        <br />
                        <input type = "hidden" id = "hiddenItems" value = "{!barcodeList}"/>
                    </apex:pageBlockSection>
                </apex:pageBlock>
                <apex:form >
                    <apex:commandButton action="{!insertRecord}" value="Checkout" id="checkoutBtn">
                    </apex:commandButton>
                        
                </apex:form>
            </body>
            <script type = "text/javascript">
                var inputText = document.getElementById("inputText");
                var completeText = "";
                var arrayItems = [];
                var items = "";
                var hiddenItems = document.getElementById("hiddenItems");
                inputText.addEventListener("keydown", function(e) {
                console.log("Inside Event");
                if(e.which!=13) {
                    if(e.which >= 48 && e.which <= 57 || e.which >= 65 && e.which <= 90) {
                        console.log("Inside If-Block");
                        completeText = completeText + e.key;
                    }
                    else {
                        console.log("Invalid Key");
                        outputText.innerHTML = "Invalid Key Pressed";
                        inputText.value = "";
                    }
                    
                }
                else {   
                    arrayItems.push(completeText);
                    items = items + completeText + ",";
                    addedItems.innerHTML = items;
                    hiddenItems.value = items;
                    console.log("Inside Else-Block");
                    outputText.innerHTML = completeText;
                    inputText.value = "";
                    completeText = "";
                }
                console.log("Item Elements:");
                for(var i = 0; i < arrayItems.length; i++) {
                    console.log(i+": "+arrayItems[i]);
                }
        });
            </script>
        </head>
    </html>
</apex:page>

Controller - BorrowController.apex
public class BorrowController {
    public String employee_ID {get; set;}
    public String borrow_ID {get; set;}
    public String barcodeList{get; set;}
    
    public BorrowController() {}
    
    public void createBorrowRecord() {
        List<String> barcodeItems = barcodeList.split(',');
        System.debug('barcodeItems:'+barcodeItems);
        List<Item__c> itemList = new List<Item__c>();
        for(Integer i = 0; i < barcodeItems.size(); i++) {
            System.debug('Inside for loop');
            itemList = [SELECT Name, Item_ID__c, Price__c, Status__c FROM Item__c WHERE Barcode_ID__c =: barcodeItems[i]];
        }
        
        for(Item__c item: itemList) {
            if(item != null) {
            Borrow__c b = new Borrow__c();
            System.debug('Borrow Item initialised');
            b.Employee_ID__c = employee_ID;
            b.Item_ID__c = item.Item_ID__c;
            b.Barcode_ID__c = item.Barcode_ID__c;
            b.Item_Price__c = item.Price__c;
            borrow_ID = b.Borrow_ID__c;
            System.debug(b.Employee_ID__c +', '+b.Item_ID__c+', '+b.Barcode_ID__c+', '+b.Item_Price__c+', '+borrow_ID);
            insert b;
        }
      }
        
        
        
    }
    
    public PageReference insertRecord() {
        System.debug('Inside Page Reference: insertRecord()');
        createBorrowRecord();
        return new PageReference('/'+ borrow_ID);  
    }

}

Its also giving me a StringException for Employee_ID and Item_ID while inserting in Borrow object. The Employee_ID and Item_ID in respective objects are autonumber field beginning with E-{0001} and I-{0001}. My requirement is to get the user entered Barcode_ID's with Item's Barcode_ID to get information about other fields of Item object, and if its a match and corresponding item record is found, it should create a borrow object record. I'm unsure if the field values from form are correctly passed to the controller or not. I don't know how to pass these field values to the controller.

Any help would be greatly appreciated.

Thanks and Regards,
Gill