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
shaqib ahmadshaqib ahmad 

unable to run this code npe

Apex class 
public class productinfo{
    public string name;
    public double price;
    public integer quantity;

}
apex code


productinfo[] products =new productinfo[4];
productinfo p1= new productinfo();
p1.name='iphone';
p1.price=50000;
p1.quantity =2;
products[0]=p1; 
//system.debug('the name is'+ p1);
productinfo p2= new productinfo();
p2.name='sumsung';
p2.price=8888;
p2.quantity=9;
products[1]=p2;
//System.debug('the name of p2 '+p2);
productinfo p3=new productinfo();
p3.name='blackberry';
p3.price=3333;
p3.quantity=4;
products[2]=p3;
//system.debug('the details of p3 '+p3);

for(productinfo p :products)
{
    system.debug(p.name);
    system.debug(p.quantity);
    system.debug(p.price);
}
 
Best Answer chosen by shaqib ahmad
ANUTEJANUTEJ (Salesforce Developers) 
Hi Shaqib,

Tried the same with a list and I was able to iterate and print them, please check the following code:

    List<Productinfo> products=new List<Productinfo>();
productinfo p1= new productinfo();
p1.name='iphone';
p1.price=50000;
p1.quantity =2;
products.add(p1);
System.debug(products);
//system.debug('the name is'+ p1);
productinfo p2= new productinfo();
p2.name='sumsung';
p2.price=8888;
p2.quantity=9;
products.add(p2);
//System.debug('the name of p2 '+p2);
productinfo p3=new productinfo();
p3.name='blackberry';
p3.price=3333;
p3.quantity=4;
products.add(p3);
//system.debug('the details of p3 '+p3);

for(productinfo p :products)
{
    system.debug(p.name);
    system.debug(p.quantity);
    system.debug(p.price);
}

Regards,
Anutej

All Answers

ANUTEJANUTEJ (Salesforce Developers) 
Hi Shaqib,

You have not used a constructor try using the below code:

public class productinfo {
public string name;
    public integer price;
    public integer quantity;
    //
    public productinfo()
    {}
}

After this try executing the same apex code in anonymous window you should be able to do the operation you were performing.

In case if this helped can you please close the thread and mark this as the best answer so that it can be used by others in the future and also helps in keeping the community clean.
shaqib ahmadshaqib ahmad
still not working
Line: 32, Column: 1
System.NullPointerException: Attempt to de-reference a null object////

public class productinfo {
public string name;
    public integer price;
    public integer quantity;
    
    public productinfo()
    {}
}

productinfo[] products =new productinfo[4];
productinfo p1= new productinfo();
p1.name='iphone';
p1.price=50000;
p1.quantity =2;
products[0]=p1; 
//system.debug('the name is'+ p1);
productinfo p2= new productinfo();
p2.name='sumsung';
p2.price=8888;
p2.quantity=9;
products[1]=p2;
//System.debug('the name of p2 '+p2);
productinfo p3=new productinfo();
p3.name='blackberry';
p3.price=3333;
p3.quantity=4;
products[2]=p3;
//system.debug('the details of p3 '+p3);

for(productinfo p :products)
{
    system.debug(p.name);
    system.debug(p.quantity);
    system.debug(p.price);
}

 
ANUTEJANUTEJ (Salesforce Developers) 
Hi Shaqib,

Tried the same with a list and I was able to iterate and print them, please check the following code:

    List<Productinfo> products=new List<Productinfo>();
productinfo p1= new productinfo();
p1.name='iphone';
p1.price=50000;
p1.quantity =2;
products.add(p1);
System.debug(products);
//system.debug('the name is'+ p1);
productinfo p2= new productinfo();
p2.name='sumsung';
p2.price=8888;
p2.quantity=9;
products.add(p2);
//System.debug('the name of p2 '+p2);
productinfo p3=new productinfo();
p3.name='blackberry';
p3.price=3333;
p3.quantity=4;
products.add(p3);
//system.debug('the details of p3 '+p3);

for(productinfo p :products)
{
    system.debug(p.name);
    system.debug(p.quantity);
    system.debug(p.price);
}

Regards,
Anutej
This was selected as the best answer
shaqib ahmadshaqib ahmad
yes it worked
thanks Anutej