在jtext区域中打印unicode单词时出现ArrayIndexOutofBoundException。 (马拉雅拉姆语)

时间:2013-04-20 22:28:30

标签: java swing unicode jtextarea indexoutofboundsexception

我正在尝试将由matrubhumi“Indiavision”等各种媒体提供的rssfeeds提取的malayalam字词打印到jtext区域。

我正在做的是将所有主题从rssfeed保存到文本文件,然后逐行读取它们以将其打印到textarea中。但有些话我得到了ArrayIndexOutofBoundException

例如,打印“ബോസ്റ്റണ്സംഭവം:അമേരിക്കയിലെലെിഖ്സമൂഹംസമൂഹം്കയില്”给出了错误。 请指出我正在犯的任何错误。此外,我已将字体更改为“Meera”,所以我不认为这是问题所在。此外,“ബംഗാളില്ചിട്ടികമ്പനിപൊളിഞ്ഞു,സംഘര്ഷം”这样的字符串也不会显示任何错误并打开应用程序窗口。提前致谢。

  BufferedReader demo = new BufferedReader(new FileReader(Name));
  jScrollPane1 = new javax.swing.JScrollPane();
  jTextArea1 = new javax.swing.JTextArea();
  setBackground(java.awt.Color.white);
  setPreferredSize(new java.awt.Dimension(1366, 768));
  jTextArea1.setColumns(30);
  jTextArea1.setRows(10);
  jTextArea1.setBorder(null);
  jTextArea1.setFont(new java.awt.Font("Meera", 1, 24));
  jTextArea1.setEditable(false);
  jScrollPane1.setViewportView(jTextArea1);
  jTextArea1.setWrapStyleWord(true);
  jTextArea1.setLineWrap(true);
    while ((sCurrentLine = demo.readLine()) != null){
    jTextArea1.append(sCurrentLine);
}
  private javax.swing.JTextArea jTextArea1;
  public static final java.lang.Character.UnicodeBlock MALAYALAM = null;

其余代码是来自netbeans的自动生成代码。 这是第二堂课。主类处理获取rssfeed并将主题保存到文本文件中。并且文本文件的名称将传递给此类,其中FileReader()用于访问文件并逐行获取内容到字符串变量“sCurrentLine”。并使用jTextArea.append()将文本附加到文本区域。

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 432
at sun.font.ExtendedTextSourceLabel.createCharinfo(ExtendedTextSourceLabel.java:772)
at sun.font.ExtendedTextSourceLabel.getCharinfo(ExtendedTextSourceLabel.java:509)
at sun.font.ExtendedTextSourceLabel.getLineBreakIndex(ExtendedTextSourceLabel.java:455)
at java.awt.font.TextMeasurer.calcLineBreak(TextMeasurer.java:325)
at java.awt.font.TextMeasurer.getLineBreakIndex(TextMeasurer.java:561)
at java.awt.font.LineBreakMeasurer.nextOffset(LineBreakMeasurer.java:358)
at java.awt.font.LineBreakMeasurer.nextLayout(LineBreakMeasurer.java:440)
at javax.swing.text.TextLayoutStrategy.sync(TextLayoutStrategy.java:323)
at javax.swing.text.TextLayoutStrategy.insertUpdate(TextLayoutStrategy.java:70)
at javax.swing.text.FlowView.loadChildren(FlowView.java:143)
at javax.swing.text.CompositeView.setParent(CompositeView.java:139)
at javax.swing.text.FlowView.setParent(FlowView.java:289)
at javax.swing.plaf.basic.BasicTextAreaUI$PlainParagraph.setParent(BasicTextAreaUI.java:239)
at javax.swing.text.CompositeView.replace(CompositeView.java:217)
at javax.swing.text.BoxView.replace(BoxView.java:181)
at javax.swing.text.View.updateChildren(View.java:1116)
at javax.swing.text.View.insertUpdate(View.java:700)
at javax.swing.plaf.basic.BasicTextUI$RootView.insertUpdate(BasicTextUI.java:1602)
at javax.swing.plaf.basic.BasicTextUI$UpdateHandler.insertUpdate(BasicTextUI.java:1861)
at javax.swing.text.AbstractDocument.fireInsertUpdate(AbstractDocument.java:202)
at javax.swing.text.AbstractDocument.handleInsertString(AbstractDocument.java:749)
at javax.swing.text.AbstractDocument.insertString(AbstractDocument.java:708)
at javax.swing.text.PlainDocument.insertString(PlainDocument.java:130)
at javax.swing.JTextArea.append(JTextArea.java:477)

此外,我尝试通过将错误生成部分包含在try catch中来运行程序,但后来我得到了另一个ArrayIndexOutofBoundException,但这次是在不同的行.Code Segment

 String RSSName;
 RSSName = "Matrubhumi";
 ReadTest demo = new ReadTest();
 demo.NewsFeed(RSSName); //Creates text file and fetch data from rssfeed
 JFrame frame = new JFrame();
 frame.setLayout(new BorderLayout());
 frame.add(new ImportantNews(RSSName),BorderLayout.CENTER);
 frame.setSize(1366, 768);
 frame.setVisible(true); // new exception is thrown in this line
 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 



 Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 496
at sun.font.ExtendedTextSourceLabel.createCharinfo(ExtendedTextSourceLabel.java:772)
at sun.font.ExtendedTextSourceLabel.getCharinfo(ExtendedTextSourceLabel.java:509)
at sun.font.ExtendedTextSourceLabel.getCharX(ExtendedTextSourceLabel.java:353)
at java.awt.font.TextLine$3.computeFunction(TextLine.java:515)
at java.awt.font.TextLine.applyFunctionAtIndex(TextLine.java:651)
at java.awt.font.TextLine.getCharXPosition(TextLine.java:668)
at java.awt.font.TextLine.getCharLinePosition(TextLine.java:678)
at java.awt.font.TextLayout.buildCache(TextLayout.java:683)
at java.awt.font.TextLayout.ensureCache(TextLayout.java:655)
at java.awt.font.TextLayout.getAdvance(TextLayout.java:885)
at sun.font.FontDesignMetrics.charsWidth(FontDesignMetrics.java:510)
at javax.swing.text.Utilities.getTabbedTextWidth(Utilities.java:279)
at javax.swing.text.GlyphPainter1.getSpan(GlyphPainter1.java:62)
at javax.swing.text.GlyphView.getPreferredSpan(GlyphView.java:592)
at javax.swing.text.View.getMaximumSpan(View.java:269)
at javax.swing.text.ParagraphView.findEdgeSpan(ParagraphView.java:768)
at javax.swing.text.ParagraphView.calculateMinorAxisRequirements(ParagraphView.java:736)
at javax.swing.plaf.basic.BasicTextAreaUI$PlainParagraph.calculateMinorAxisRequirements(BasicTextAreaUI.java:272)
at javax.swing.text.BoxView.checkRequests(BoxView.java:935)
at javax.swing.text.BoxView.getMinimumSpan(BoxView.java:568)
at javax.swing.text.BoxView.calculateMinorAxisRequirements(BoxView.java:903)
at javax.swing.text.BoxView.checkRequests(BoxView.java:935)
at javax.swing.text.BoxView.setSpanOnAxis(BoxView.java:343)
at javax.swing.text.BoxView.layout(BoxView.java:708)
at javax.swing.text.BoxView.setSize(BoxView.java:397)
at javax.swing.plaf.basic.BasicTextUI$RootView.setSize(BasicTextUI.java:1714)
at javax.swing.plaf.basic.BasicTextUI.getPreferredSize(BasicTextUI.java:917)
at javax.swing.plaf.basic.BasicTextAreaUI.getPreferredSize(BasicTextAreaUI.java:120)
at javax.swing.JComponent.getPreferredSize(JComponent.java:1651)
at javax.swing.JTextArea.getPreferredSize(JTextArea.java:619)
at javax.swing.ScrollPaneLayout.layoutContainer(ScrollPaneLayout.java:790)
at java.awt.Container.layout(Container.java:1503)
at java.awt.Container.doLayout(Container.java:1492)
at java.awt.Container.validateTree(Container.java:1688)
at java.awt.Container.validateTree(Container.java:1697)
at java.awt.Container.validateTree(Container.java:1697)
at java.awt.Container.validateTree(Container.java:1697)
at java.awt.Container.validateTree(Container.java:1697)
at java.awt.Container.validateTree(Container.java:1697)
at java.awt.Container.validate(Container.java:1623)
at java.awt.Container.validateUnconditionally(Container.java:1660)
at java.awt.Window.show(Window.java:1033)
at java.awt.Component.show(Component.java:1651)
at java.awt.Component.setVisible(Component.java:1603)
at java.awt.Window.setVisible(Window.java:1014)

1 个答案:

答案 0 :(得分:0)

这可能是this thread

中描述的编码问题

尝试替换此行

BufferedReader demo = new BufferedReader(new FileReader(Name));

new BufferedReader(new InputStreamReader(new FileInputStream(filePath), encoding));

其中encoding应该是您存储主题的文件中使用的编码。