0

I receive the following error message:

Static method cannot be referenced from a non static context: System.Pattern System.Pattern.compile(String)

Here is my code:

    private static Pattern pattern = Pattern.compile(String str);

public static List<String> getRecord(String record) {
        List<String> patternRecord = new List<String>();
        if(record == null || record.equals('')) {
            return patternRecord;
        }

        Matcher recordMatcher = pattern.matcher(record);
        while(recordMatcher.find()) {
            String entireRecord = recordMatcher.group();
            if(entireRecord != null && !entireRecord.equals('')) {
                List<String> splitRecord = entireRecord.split('[,]');
                for(String rec :splitRecord){
                    patternRecord.add(rec);
                }
            }     
        }
        return patternRecord;
    }

Please advise how to avoid such error?

2
  • 2
    Please edit your question you include more surrounding context.
    – Adrian Larson
    Mar 7, 2018 at 15:27
  • 2
    Pattern.compile() takes an argument that you omit here. That should produce a different error; getting the "wrong" error sometimes indicates there are other syntax issues.
    – David Reed
    Mar 7, 2018 at 15:29

1 Answer 1

2

You need to actually pass in a string ('...'). Also I advise against introducing a naming conflict by using a variable name of pattern.

Instead of:

static Pattern pattern = Pattern.compile(String str);

Use:

static Pattern myPattern = Pattern.compile('<some expression here>')
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .