Reputation: 11
This is a Scala module:
package xpf
import java.io.File
import org.jdom.Element
import org.jdom.input.SAXBuilder
object xmlpf {
def load_file(filename: String): Element = {
val builder = new SAXBuilder
builder.build(new File(filename)).getRootElement
}
}
And here is Java code, calling the method from Scala above:
package textxpf;
import org.jdom.Element;
public class Main {
public static void main(String[] args) {
Element root = xpf.xmlpf.load_file("/home/capkidd/proj/XmlPathFinder/Staff.xml");
System.out.println(root.getName());
}
}
Running java main procedure I see
run:
Exception in thread "main" java.lang.NullPointerException
at textxpf.Main.main(Main.java:8)
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)
Exploring the problem I found that I cannot return any value of any type from any Scala method to the Java code that called it.
I use NetBeans 6.9.1 with Scala 2.8.1 plugin. scala-library.jar and jdom.jar are properly plugged to the project.
What am I doing wrong? Has anybody any idea?
Upvotes: 0
Views: 699
Reputation: 2151
I tried a similar program with no problems:
// ms/MyObject.scala
package ms
object myObject {
def foo(s: String) = s
}
// mj/MyObject2.java
package mj;
public class MyObject2 {
public static void main(String[] args) {
System.out.println(ms.myObject.foo("hello"));
}
}
I compiled both files, then "scala -cp . mj.MyObject2". Works fine with scala 2.8.1.final. Does this example work in your setup?
So, I wonder if it's some sort of environment issue, such as picking up a stale build of the Scala class? Have you tried a clean build from scratch? Is your runtime class path correct?
Upvotes: 1
Reputation: 3164
Try this and then debug accordingly:
package xpf
import java.io.File
import org.jdom.Element
import org.jdom.input.SAXBuilder
object xmlpf {
def load_file(filename: String): Element = {
val builder = new SAXBuilder
val re = builder.build(new File(filename)).getRootElement
if (re == null) throw new NullPointerException("the root element is null!")
re
}
}
Upvotes: 2