Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4235487

Repetative Actions on a JTextArea causes a NullPointerException to be Thrown.

XMLWordPrintable

      Setting setWrapStyleWord(true) and then setting setWrapStyleWord(false) and
      then setWrapStyleWord(true) again some how unmaps the JTextArea getRowStart. It causes an Exception to be thrown.

      Steps to Reproduce bug:
      1: Compile and excute code snippet below.
      2: Check Word Wrap check box
      3: Uncheck Word Wrap check box
      4: Check Word Wrap check box
      5: Double Click in JTextArea or Click in JTextArea and then press the
      End Key on the keyboard.

      Error:
      Words should wrap and unwrap. Instead they don't wrap and unwrap and
      they cause a NullPointerException to be thrown for
        javax.swing.text.Utilities.getRowStart

      Exception:

      java SetWordWrap
      Exception occurred during event dispatching:
      java.lang.NullPointerException
      at javax.swing.text.Utilities.getRowStart(Compiled Code)
      at javax.swing.text.DefaultEditorKit$BeginLineAction.actionPerformed(DefaultEditorKit.java:1724)
      at javax.swing.text.DefaultEditorKit$SelectLineAction.actionPerformed(DefaultEditorKit.java:1961)
      at javax.swing.text.DefaultCaret.mouseClicked(DefaultCaret.java:298)
      at java.awt.AWTEventMulticaster.mouseClicked(AWTEventMulticaster.java:211)
      at java.awt.AWTEventMulticaster.mouseClicked(AWTEventMulticaster.java:210)
      at java.awt.Component.processMouseEvent(Compiled Code)
      at java.awt.Component.processEvent(Compiled Code)
      at java.awt.Container.processEvent(Compiled Code)
      at java.awt.Component.dispatchEventImpl(Compiled Code)
      at java.awt.Container.dispatchEventImpl(Compiled Code)
      at java.awt.Component.dispatchEvent(Compiled Code)
      at java.awt.LightweightDispatcher.retargetMouseEvent(Compiled Code)
      at java.awt.LightweightDispatcher.processMouseEvent(Compiled Code)
      at java.awt.LightweightDispatcher.dispatchEvent(Compiled Code)
      at java.awt.Container.dispatchEventImpl(Compiled Code)
      at java.awt.Window.dispatchEventImpl(Compiled Code)
      at java.awt.Component.dispatchEvent(Compiled Code)
      at java.awt.EventQueue.dispatchEvent(Compiled Code)
      at java.awt.EventDispatchThread.run(Compiled Code)

      Things Tried:
      1: If you set JTextArea.setWrapStyleWord(true) and then click in JTextArea and
        press End key all is well.
      2: Tried the cricket release.


      Testcase:

      import java.awt.*;
      import java.awt.event.*;
      import javax.swing.*;
      import javax.swing.event.*;

      class SetWordWrap extends JFrame implements ActionListener {

        public SetWordWrap() {
          
          JPanel mainPanel = new JPanel(new BorderLayout());
          JPanel cbPanel = new JPanel();
          JPanel buttonPanel = new JPanel();
          JPanel statusPanel = new JPanel();
          JPanel bottomPanel = new JPanel(new BorderLayout());
          

          line = addCheckBox(cbPanel, "Word Wrap");

          /** For Debugging purpose
          test = addButton(buttonPanel,"Test",Color.yellow);
          */

          pass = addButton(buttonPanel,"Pass",Color.green);
          fail = addButton(buttonPanel,"Fail",Color.red);
          
          textArea.setWrapStyleWord(true);
          textArea.setText(text1);
          scrollPane = new JScrollPane(textArea);

          mainPanel.add(cbPanel,BorderLayout.NORTH);
          mainPanel.add(scrollPane,BorderLayout.CENTER);

          //Add Status Bar
          statusPanel.setBorder(BorderFactory.createEtchedBorder());
          statusPanel.setLayout(new FlowLayout(FlowLayout.LEFT,0,0));
          statusPanel.add(status);
          status.setHorizontalAlignment(JLabel.LEFT);

          bottomPanel.add(buttonPanel,BorderLayout.NORTH);
          bottomPanel.add(statusPanel,BorderLayout.SOUTH);

          contentPane.add(mainPanel, BorderLayout.NORTH);
          contentPane.add(bottomPanel, BorderLayout.SOUTH);

          // Add Window handler
          addWindowListener(new WindowAdapter(){
            public void windowClosing(WindowEvent event){ System.exit(0);}
          });
          
        }

        public static void main(String[] args) {
          SetWordWrap f = new SetWordWrap();
          f.setTitle("Word Wrap Test for JTextArea");
          f.pack();
          f.show();
        }

        
        public JButton addButton(JPanel addComponent, String name, Color color) {
          JButton b = new JButton(name);
          b.setBackground(color);
          b.addActionListener(this);
          addComponent.add(b);
          return b;
        }


        public JCheckBox addCheckBox(JPanel addComponent, String name) {
          JCheckBox c = new JCheckBox(name);
          c.addActionListener(this);
          addComponent.add(c);
          return c;
        }

        public void actionPerformed(ActionEvent evt) {
          String cmd = evt.getActionCommand();
          
          if (line.isSelected()) {
            textArea.setWrapStyleWord(true);
            status.setText(textStatus2);
          }
          else {
            textArea.setWrapStyleWord(false);
            status.setText(textStatus1);
          }

          if (evt.getSource() == pass) {
            System.out.println("Test Passed");
            System.exit(0);
          }
          else if (evt.getSource() == fail) {
            System.out.println("Test Failed");
            System.exit(1);
          }
          /** For Debugging purpose
          else if (evt.getSource() == test) {
            System.out.println(textArea.getWrapStyleWord());
          }
          */
        }

        private JScrollPane scrollPane;
        private JTextArea textArea = new JTextArea(8,25);
        private Container contentPane = getContentPane();
        private JCheckBox insert;
        private JCheckBox line;
        private JCheckBox word;
        private JLabel status = new JLabel("Toggle Line Wrap CheckBox on/off.");
        private String textStatus1 = "Text should be scrollable.";
        private String textStatus2 = "Does the text wrap to next line?";
        private String text1 = "This is a default String that should force word wrapping.";
        private JButton pass;
        private JButton fail;
        // private JButton test; // For debugging purpose
        private boolean state = true;


      gary.collins@East 1999-05-05

            tprinzing Tim Prinzing
            collins Gary Collins (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: