You need to sign in to do that
Don't have an account?
shaqib 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);
}
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);
}
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
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.
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);
}
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
thanks Anutej