0

I'm trying to run some code that imports test.x3dv in Java. The Java code that I have is:

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);
    }
}

And the code for the test.x3dv file is:

#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

When I run the code, I get the following:

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)

I believe it's to do with the Component browserComponent = (Component) x3dComponent.getImplementation(); line and trying to call a method on a null object and I don't know how to fix it. If anyone can help, it would be greatly appreciated.

EDIT I was wondering how I'd go about trying to fix that one line so it works. That other question is useful but I'm struggling to see how I can use it for this line.

smitthy
  • 307
  • 4
  • 18
  • `BrowserFactory.createX3DComponent(null);` you're passing in a null reference and getting a `NullPointerException`. Maybe pass in [an empty map](https://savage.nps.edu/Xj3D.nps/docs/javadoc/org/web3d/x3d/sai/BrowserFactory.html#createX3DComponent(java.util.Map)) instead? – Andy Turner Feb 17 '16 at 00:21
  • Possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – Andy Turner Feb 17 '16 at 00:21
  • I've just tried using the empty map on that link you've provided and it's giving me loads of errors. – smitthy Feb 17 '16 at 00:33

1 Answers1

0

Based on your codes, BrowserFactory has not been initialized, so when you call a method on this without any reference it returns a null pointer exception. Initialize this object first before calling its method

mel3kings
  • 8,857
  • 3
  • 60
  • 68
  • I know it might sound stupid (and I'm sorry!!) but how would I initialize it? I'm going to look into how I'd initialize the `BrowserFactory` and will keep the question updated :) – smitthy Feb 17 '16 at 20:25
  • @smitthyyou as this is an external jar, you might need to check online the api on how this is initialized, you need to check what are its constructors. – mel3kings Feb 18 '16 at 01:13