OutofBounds Exception : <p>I am trying here to measure how many white spaces there are in text typed in a JTextArea. I am getting an outofbounds exception. Why so? </p>

<pre><code>Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 0
        at java.lang.String.charAt(String.java:658)
        at ClassTest.main(ClassTest.java:11)





import javax.swing.*;
public class ClassTest {
    public static void main(String [] args) {
        JFrame frame = new JFrame();
        JTextArea textarea = new JTextArea();
        frame.setSize(300, 300);
        frame.add(textarea);
        frame.setVisible(true);
        String text=textarea.getText();
        int count = 0;
        for(int i=0;i&gt;=text.length();i++) {
            char spacecount = text.charAt(i);
            if(spacecount==' ') {
                System.out.print(count++);
            }

        }

    }
}
</code></pre>
