控制台显示:
java.lang.NullPointerException: Canvas3D: null GraphicsConfiguration
at javax.media.j3d.Canvas3D.checkForValidGraphicsConfig(Canvas3D.java:982)
at javax.media.j3d.Canvas3D.<init>(Canvas3D.java:1025)
at wjg.test.MyCone.<init>(MyCone.java:49)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at java.lang.Class.newInstance0(Unknown Source)
at java.lang.Class.newInstance(Unknown Source)
at sun.applet.AppletPanel.createApplet(Unknown Source)
at sun.applet.AppletPanel.runLoader(Unknown Source)
at sun.applet.AppletPanel.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
程序源代码为:
package wjg.test;
import java.applet.Applet;
import java.awt.BorderLayout;
import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.geometry.Cone; //调用生成Cone的Utility包
import com.sun.j3d.utils.geometry.Primitive;
import com.sun.j3d.utils.universe.*;
import javax.media.j3d.*;
import javax.vecmath.*;
public class MyCone extends Applet{
public BranchGroup createSceneGraph(){
BranchGroup objRoot = new BranchGroup(); //创建一个分支节点来包含一组对象
TransformGroup objTrans = new TransformGroup(); //创建转换节点(生成坐标系)
objRoot.addChild(objTrans); //将转换节点(坐标系)添加到根节点上
BoundingSphere bounds = new BoundingSphere(new Point3d(0.0,0.0,0.0),100.0); //设置场景的有效范围
Color3f bgColor = new Color3f(0.0f,0.0f,0.0f); //定义背景颜色
Background bg = new Background(bgColor); //创建背景颜色
bg.setApplicationBounds(bounds);
objRoot.addChild(bg); //将背景添加到场景中
Color3f directionalColor= new Color3f(1.f,1.f,1.f);
Vector3f vec = new Vector3f(0.f,0.f,-1.f);
DirectionalLight directionalLight = new DirectionalLight(directionalColor,vec);
directionalLight.setInfluencingBounds(bounds);
objRoot.addChild(directionalLight);
//设置外观
Appearance app = new Appearance();
Material material =new Material();
material.setDiffuseColor(new Color3f(1.0f,1.0f,1.0f));
app.setMaterial(material);
//生成基本的圆锥体
Cone cone = new Cone(.5f,1.0f,1,app);
//将生成的圆锥添加到场景中
objRoot.addChild(cone);
return objRoot;
}
public MyCone(){
setLayout(new BorderLayout());
Canvas3D c = new Canvas3D(null);
add("Center",c);
BranchGroup scene = createSceneGraph();
scene.compile();
SimpleUniverse u = new SimpleUniverse(c);
u.getViewingPlatform().setNominalViewingTransform();
u.addBranchGraph(scene);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new MainFrame(new MyCone(),400,300);
}
}
请高手指点!!
6 个解决方案
#1
MyCone.java第49行是哪一行?那行报了个空指针引用异常
#2
第49行是:
Canvas3D c = new Canvas3D(null);
#3
Canvas3D c = new Canvas3D(null);
没有这个类吧
当然报空指针错了
没有这个类吧
当然报空指针错了
#4
有这个类的,我的问题已经解决,解决方法如下:
在import中加入:
48行后添加:
原来的49行改为:
在import中加入:
import java.awt.GraphicsConfiguration;
48行后添加:
GraphicsConfiguration config=SimpleUniverse.getPreferredConfiguration();
原来的49行改为:
Canvas3D c=new Canvas3D(config);
#5
估计是new Canvas3D()的GraphicsConfiguration参数不能为空造成的
你试下这个:
Canvas3D c = new Canvas3D(GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration());
你试下这个:
Canvas3D c = new Canvas3D(GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration());
#6
谢谢。我也解决问题了..
#1
MyCone.java第49行是哪一行?那行报了个空指针引用异常
#2
第49行是:
Canvas3D c = new Canvas3D(null);
#3
Canvas3D c = new Canvas3D(null);
没有这个类吧
当然报空指针错了
没有这个类吧
当然报空指针错了
#4
有这个类的,我的问题已经解决,解决方法如下:
在import中加入:
48行后添加:
原来的49行改为:
在import中加入:
import java.awt.GraphicsConfiguration;
48行后添加:
GraphicsConfiguration config=SimpleUniverse.getPreferredConfiguration();
原来的49行改为:
Canvas3D c=new Canvas3D(config);
#5
估计是new Canvas3D()的GraphicsConfiguration参数不能为空造成的
你试下这个:
Canvas3D c = new Canvas3D(GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration());
你试下这个:
Canvas3D c = new Canvas3D(GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration());
#6
谢谢。我也解决问题了..