-2

While trying to get matcher.group() as string it tells that it's unavailable. Why?

enter image description here

CODE:

private static ArrayList<String> validateList(List<String> listToProcess) {

        List<String> resultTemp = new ArrayList<String>();
        boolean listIsCorrectlyFormatted = true;


        String pattern = "";
        pattern = "(\\s)*(\\w\\w(\\w)*)((\\s)(\\w|-|\\.)+)?(((\\s)+|(\\s)+(,)*(\\s)*|(,)(\\s)*)+(\\+(\\d)+)+)?(((\\s)+|(\\s)+(,)*(\\s)*|(,)(\\s)*)+(\\w)+(\\w|\\s|-|,)*[^\\)|^\\(])?(((\\s)+|(\\s)+(,)*(\\s)*|(,)(\\s)*)+\\((\\w)+(\\w|\\s|-|,)*\\))?(\\s)*";
        // (\s)*(\w\w(\w)*)((\s)(\w|-|\.)+)?(((\s)+|(\s)+(,)*(\s)*|(,)(\s)*)+(\+(\d)+)+)?(((\s)+|(\s)+(,)*(\s)*|(,)(\s)*)+(\w)+(\w|\s|-|,)*[^\)|^\(])?(((\s)+|(\s)+(,)*(\s)*|(,)(\s)*)+\((\w)+(\w|\s|-|,)*\))?(\s)*


        String nameP = "(\\w\\w(\\w)*)((\\s)(\\w|-|\\.)+)?";
        String plussP = "(\\+(\\d)+)+";
        String commentP = "((\\w)+(\\w|\\s|-|,)*[^\\)|^\\(])|(\\((\\w)+(\\w|\\s|-|,)*\\))";
        String tmpStr = "";
        int counter = 1;
        Matcher matcher;

        Pattern generalPt = Pattern.compile(pattern);
        Pattern otherPattern = Pattern.compile(""); // *

        for (String str : listToProcess) {

            if (generalPt.matcher(str).find()) {
                // OK

                System.out.println("#"+counter+" :: OK ``"+str+"``");

                otherPattern = Pattern.compile(nameP); // name
                matcher = otherPattern.matcher(str);
                matcher.find();
                tmpStr +=  matcher.group(); // name
                System.out.println("@NAME@"+matcher.group()+"@");
                // -----------------------------------------------------
                matcher.reset();
                otherPattern = Pattern.compile(plussP); // plus
                matcher = otherPattern.matcher(str);
                matcher.find();

                 tmpMatcherGroup = matcher.group();
                tmpStr += tmpMatcherGroup;
                System.out.println("@PLUS@"+tmpMatcherGroup+"@");
                //--------------------------------------------------
                matcher.reset();
                otherPattern = Pattern.compile(commentP); // comment
                matcher = otherPattern.matcher(str);
                matcher.find();
                tmpStr +=  matcher.group();
                System.out.println("@COMMENT@"+matcher.group()+"@");
                //--------------------------------------------------
                resultTemp.add(tmpStr);

            } else {
                // NOK
                listIsCorrectlyFormatted = false;

                tmpStr = "ERROR at line: # " + counter;

                resultTemp.add(tmpStr);

                System.out.println("#"+counter+" :: NOT OK ``"+str+"``");
            }

            counter++;
        }

        List<String> result = new ArrayList<String>();
        result.add(Boolean.toString(listIsCorrectlyFormatted));
        result.addAll(resultTemp);

        return (ArrayList<String>) result;

    }

STACK TRACE:

Thread [main] (Suspended (exception IllegalStateException)) 
    Matcher.group(int) line: not available  
    Matcher.group() line: not available [local variables unavailable]   
    DataProcess.validateList(List<String>) line: 111    
    DataProcess.main(String[]) line: 15 

SOLUTION:

matcher.reset();
                otherPattern = Pattern.compile(plussP); // plus
                matcher = otherPattern.matcher(str);
                matcher.find();
                try {
                    if (matcher.groupCount() > 0) {
                        tmpMatcherGroup = matcher.group();
                    }
                } catch (IllegalStateException e) {
                    System.out.println(e);
                }
                tmpStr += tmpMatcherGroup;
                System.out.println("@PLUS@" + tmpMatcherGroup + "@");
J.Olufsen
  • 13,415
  • 44
  • 120
  • 185
  • "tmpMatcherGroup" is null because matcher.group() returned nothing (exception occurs). – J.Olufsen Nov 16 '12 at 11:23
  • Short: JDK classes aren't compiled with debugging information, hence you can't see local variables during debugging. [description](http://stackoverflow.com/questions/10266560/debugging-not-able-to-inspect-the-variables), [solution](http://stackoverflow.com/questions/271521/locally-declared-variables-can-not-be-inspected) – CAMOBAP Nov 16 '12 at 11:24
  • How do you know it is null? what is the exception? – Ankur Nov 16 '12 at 11:24
  • @Ankur, Debugger shows it (when hovering mouse over) – J.Olufsen Nov 16 '12 at 11:27
  • 1
    You already asked 105 questions, and you still don't know that if you ask about an exception you should paste the exception stack trace along with the code that throws it? – JB Nizet Nov 16 '12 at 11:29
  • The problem is in matcher.group rather than "tmpMatcherGroup". Right? Doesn't matter. If you mean the string which is checked USING matcher than it can be anything (in this case it's 'abc' lets assume) – J.Olufsen Nov 16 '12 at 11:30
  • `abc` will never match with `(\+(\d)+)+`, so it will obviously return null – Ankur Nov 16 '12 at 11:35

2 Answers2

1

You need to first check the groupCount() and if it is more than 0, then you need to assign your variable.

   while(m.find()) {
      for(int i = 0; i<=m.groupCount() ; i++) {
           ...Your code
      }
    }
Ankur
  • 12,676
  • 7
  • 37
  • 67
1

The explanation is in the javadoc:

Throws:

IllegalStateException - If no match has yet been attempted, or if the previous match operation failed

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255