Java+jopendocument: 使用getCellAt(0,0)时出现空指针异常。

huangapple go评论81阅读模式
英文:

Java+jopendocument: NullPointerException when using getCellAt(0,0)

问题

在LibreOffice电子表格的工作表中,我可以访问单元格,但无法访问其值。我找不到错误所在,欢迎任何反馈意见。以下是代码:

```java
import java.io.File;
import java.io.IOException;
import org.jopendocument.dom.spreadsheet.MutableCell;
import org.jopendocument.dom.spreadsheet.Sheet;
import org.jopendocument.dom.spreadsheet.SpreadSheet;

public class MyClass {
	protected Sheet dataSheet;
	protected File dataCalcFile;
	
	public static void main(String[] args) {
		MyClass myClassInstance = new MyClass(); 
		myClassInstance.loadData();
	}

	public void loadData() {
		int numRows=0, numColumnas=0;
		MutableCell cell=null;
		
		try {
			dataCalcFile = new File("C:\\temp\\Data.ods");
			dataSheet = SpreadSheet.createFromFile(dataCalcFile).getSheet(0);
            numRows = dataSheet.getRowCount();            
			System.out.println("行数:" + numRows);
			System.out.println("位于 0,0 的单元格:" + dataSheet.getCellAt(0, 0));
			System.out.println("获取位于 0,0 处单元格值时出现空指针异常:" + dataSheet.getValueAt(0, 0));	// *** 臭名昭著的问题 ***
		} catch (Exception e) {
			System.out.println(e);
		}
	}
}

以下是控制台输出:

行数:107
位于 0,0 处的单元格:<table:table-cell xmlns:table="urn:oasis:names:tc:opendocument:xmlns:table:1.0" table:style-name="ce1" xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0" office:value-type="string" xmlns:calcext="urn:org:documentfoundation:names:experimental:calc:xmlns:calcext:1.0" calcext:value-type="string"><text:p xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0">Carpeta</text:p></table:table-cell>
java.lang.NullPointerException
英文:

In a Libreoffice Spreadsheet's sheet, I can get access to a Cell, but not to its value. I don't find my error, any feedback is welcome. Here is the code:

import java.io.File;
import java.io.IOException;
import org.jopendocument.dom.spreadsheet.MutableCell;
import org.jopendocument.dom.spreadsheet.Sheet;
import org.jopendocument.dom.spreadsheet.SpreadSheet;

public class MyClass {
	protected Sheet dataSheet;
	protected File dataCalcFile;
	
	public static void main(String[] args) {
		MyClass myClassInstance = new MyClass(); 
		myClassInstance.loadData();
	}

	public void loadData() {
		int numRows=0, numColumnas=0;
		MutableCell cell=null;
		
		try {
			dataCalcFile = new File(&quot;&quot;C:\\temp\\Data.ods&quot;&quot;);
			dataSheet = SpreadSheet.createFromFile(dataCalcFile).getSheet(0);
            numRows = dataSheet.getRowCount();            
			System.out.println(&quot;Number of rows: &quot; + numRows);
			System.out.println(&quot;Cell at 0,0: &quot; + dataSheet.getCellAt(0, 0));
			System.out.println(&quot;Nullpointer Exception when getting cell value at 0,0: &quot; + dataSheet.getValueAt(0, 0));	// *** THE INFAMOUS ONE ***		
		} catch (Exception e) {
			System.out.println(e);
		}
	}
}

And here is the output at console:

Number of rows: 107
Cell at 0,0: &lt;table:table-cell xmlns:table=&quot;urn:oasis:names:tc:opendocument:xmlns:table:1.0&quot; table:style-name=&quot;ce1&quot; xmlns:office=&quot;urn:oasis:names:tc:opendocument:xmlns:office:1.0&quot; office:value-type=&quot;string&quot; xmlns:calcext=&quot;urn:org:documentfoundation:names:experimental:calc:xmlns:calcext:1.0&quot; calcext:value-type=&quot;string&quot;&gt;&lt;text:p xmlns:text=&quot;urn:oasis:names:tc:opendocument:xmlns:text:1.0&quot;&gt;Carpeta&lt;/text:p&gt;&lt;/table:table-cell&gt;
java.lang.NullPointerException

答案1

得分: 5

刚刚发现:
"是的,LibreOffice 7 切换到了 OpenDocument 1.3。我们正在努力支持它。与此同时,您可以将格式更改为“1.2 扩展”。进入选项,然后选择加载/保存,接着选择常规,然后选择 ODF 格式版本。"

对我来说有效,但我很期待听到我的客户意见...

英文:

Just found that:
"Yes, LO 7 switched to OpenDocument 1.3. We're working on supporting it. In the mean time, you can change the format to "1.2 extended". Go to Options, then Load/Save, then General, then ODF format version."

It works for me, but am excited to hear my customers opinion...

答案2

得分: 0

我发现** jopendocument库与较新版本的Libreoffice(7.x)之间必定存在某种不兼容性**,如上面的代码所示:

  • 如果使用Libreoffice 6.4.3.2编辑电子表格,则按预期工作。
  • 如果使用Libreoffice 7.0.1.2编辑电子表格,则会抛出空指针异常。

在某些版本中,Calc生成的XML文件无法被jopendocument解析。

英文:

I found that it must be some incompatibility between the library jopendocument and the newer versions of Libreoffice (7.x), as the code above:

  • Works as expected if the spreadsheet is edited with Libreoffice 6.4.3.2
  • Spits the null pointer exception out if the spreadsheet is edited with Libreoffice 7.0.1.2

In some version in between Calc produces an XML not understood by jopendocument.

答案3

得分: 0

代替使用

getValueAt(0, 0),
你可以使用
getCellAt(0, 0).getElement().getValue()

这对我有效。

英文:

Instead of using

getValueAt(0, 0), 
you can use 
getCellAt(0, 0).getElement().getValue()

It works for me.

huangapple
  • 本文由 发表于 2020年10月19日 15:46:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/64423111.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定