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
AasifAasif 

SelectOption error

Hi,

I am getting an error and not able to able to figure out what the issue is. Please throw your ideas.

Below is the code:

1  public class testing
2  {
3   String strQ1Opt1='0-20 million';
4   List<SelectOption> lstSelOptQ1;
5  lstSelOptQ1 = new List<SelectOption>();
6  lstSelOptQ1.add(new SelectOption(strQ1Opt1,strQ1Opt1,false));
7  }

Error: Compile Error: unexpected token: '=' at line 5 column 16

-------------------------------------------------------------------------------------------------------------------------------------------

And when i change the code pasted as below, a different error is thrown.

Code:

1 public class testing

2 {

3  String strQ1Opt1='0-20 million';

4 List lstSelOptQ1 = new List();

5 lstSelOptQ1.add(new SelectOption(strQ1Opt1,strQ1Opt1,false));

6 }

Error:Compile Error: expecting a right parentheses, found 'new' at line 5 column 20

 

Please point out any issues in the above code.

 

Thanks.

Asif

Best Answer chosen by Admin (Salesforce Developers) 
ministe_2003ministe_2003

that would solve one issue, but unless he puts the "add" operation into a method, it'll still error out

All Answers

ministe_2003ministe_2003

you need to either instantiate the list upon declaration or do it in a method:

 

option 1:

public class tester
{
	String strQ1Opt1='0-20 million';
	List<SelectOption> lstSelOptQ1 = new List<SelectOption>();
	
	public tester(){
		lstSelOptQ1.add(new SelectOption(strQ1Opt1,strQ1Opt1,false));
	}
	
}

 

 

option 2:

public class tester
{
	String strQ1Opt1='0-20 million';
	List<SelectOption> lstSelOptQ1;
	
	public tester(){
		lstSelOptQ1 = new List<SelectOption>();
		lstSelOptQ1.add(new SelectOption(strQ1Opt1,strQ1Opt1,false));
	}
	
}

 

 

vishal@forcevishal@force

Hi Aasif, 

 

The only issue I could point out in the above code is here

 

1 public class testing

2 {

3  String strQ1Opt1='0-20 million';

4 List lstSelOptQ1 = new List();

5 lstSelOptQ1.add(new SelectOption(strQ1Opt1,strQ1Opt1,false));

6 }

Error:Compile Error: expecting a right parentheses, found 'new' at line 5 column 20

 

you are not defining the List type, can you change it to : 

List<SelectOption> lstSelOptQ1 = new List<SelectOption>();

 

ministe_2003ministe_2003

that would solve one issue, but unless he puts the "add" operation into a method, it'll still error out

This was selected as the best answer
AasifAasif

Thanks Steven. That solved the issue.:smileyhappy: