我正在尝试运行一些在Java中导入test.x3dv
的代码。我拥有的Java代码是:
package xj3dtest;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Container;
import javax.swing.JFrame;
import org.web3d.x3d.sai.Browser;
import org.web3d.x3d.sai.BrowserFactory;
import org.web3d.x3d.sai.X3DComponent;
import org.web3d.x3d.sai.X3DScene;
public class Xj3DTest extends JFrame {
public Xj3DTest(String title) {
super(title);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
X3DComponent x3dComponent = BrowserFactory.createX3DComponent(null);
Browser browser = x3dComponent.getBrowser();
Component browserComponent = (Component) x3dComponent.getImplementation();
Container cp = getContentPane();
cp.setLayout(new BorderLayout());
cp.add(browserComponent, BorderLayout.CENTER);
X3DScene scene = browser.createX3DFromURL(new String[] {"test.x3dv"});
browser.replaceWorld(scene);
}
public static void main(String[] args) {
Xj3DTest frame = new Xj3DTest("Xj3D test");
frame.setSize(640, 480);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
test.x3dv文件的代码是:
#X3D V3.0 utf8
PROFILE Interactive
DEF TS TimeSensor {
cycleInterval 3
loop TRUE
}
DEF TG Transform {
rotation 0 1 0 0
children Shape {
geometry Box {}
appearance Appearance {
material DEF MAT Material {
diffuseColor 1 0 0
}
}
}
}
DEF OI OrientationInterpolator {
key [ 0 0.5 1 ]
keyValue [
0 1 0 0
0 1 0 3.14
0 1 0 6.28
]
}
ROUTE TS.fraction_changed TO OI.set_fraction
ROUTE OI.value_changed TO TG.rotation
当我运行代码时,我得到以下内容:
Exception in thread "main" java.lang.NullPointerException
Unable to find X3D browser factory implementation
at org.web3d.x3d.sai.BrowserFactory.createX3DComponent(BrowserFactory.java:252)
at xj3dtest.Xj3DTest.<init>(Xj3DTest.java:20)
at xj3dtest.Xj3DTest.main(Xj3DTest.java:36)
我认为这与Component browserComponent = (Component) x3dComponent.getImplementation();
行有关并尝试在null对象上调用方法而我不知道如何修复它。如果有人可以提供帮助,我们将不胜感激。
修改 我想知道如何尝试修复这一行,以便它可以工作。那个问题很有用,但我很难看到如何将它用于这一行。
答案 0 :(得分:0)
根据您的代码,BrowserFactory
尚未初始化,因此当您在没有任何引用的情况下调用此方法时,它会返回空指针异常。在调用其方法