我有一个java applet代码,当它从IE打开时工作正常,但是如果它是从Safari打开的,那么Java控制台会产生一个nullpointerexception。
生成此行为的代码如下所示:
public class MsgBoxPanel extends JDialog
{
Panel panel1 = new Panel();
JLabel label1 = new JLabel();
JLabel label2 = new JLabel();
public MsgBoxPanel()
{
try
{
jbInit();
}
catch(Exception e)
{
e.printStackTrace();
}
}
private void jbInit() throws Exception
{
this.setBackground(Color.lightGray);
this.setLayout(null);
label1.setFont(common.COMPONENTS_FONT);
label2.setFont(common.COMPONENTS_FONT);
label2.setHorizontalAlignment(JLabel.CENTER);
label2.setText("Buy");
label2.setBounds(new Rectangle(0, -5, 400, 44));
label1.setHorizontalAlignment(JLabel.CENTER);
label1.setText("Are you sure?");
label1.setBounds(new Rectangle(0, 0, 400, 44));
panel1.setLayout(null);
panel1.add(label1, null);
panel1.add(label2, null);
}
public void setBounds(int x,int y,int width,int height)
{
super.setBounds(x,y,width,height);
this.setLayout(null);
panel1.setBounds(4,6,this.getSize().width-8,this.getSize().height-12);
this.add(panel1);
}
}
当我从Safari(Mac OSX)调用applet时,java控制台显示以下异常:
network: Created version ID: 1.8.0.45
network: Created version ID: 1.8.0.45
security: continue with running version
network: Created version ID: 1.8.0.45
network: Created version ID: 1.8
network: Created version ID: 8.0.45
basic: exception: java.lang.reflect.InvocationTargetException.
java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at com.sun.deploy.uitoolkit.impl.awt.AWTAppletAdapter.runOnEDTAndWait(Unknown Source)
at com.sun.deploy.uitoolkit.impl.awt.AWTAppletAdapter.instantiateApplet(Unknown Source)
at sun.plugin2.applet.Plugin2Manager.initAppletAdapter(Unknown Source)
at sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run(Unknown Source)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.reflect.InvocationTargetException
at com.sun.deploy.uitoolkit.impl.awt.OldPluginAWTUtil.invokeAndWait(Unknown Source)
... 5 more
Caused by: java.lang.NullPointerException
at mutajara.MsgBoxPanel.setBounds(Unknown Source)
at java.awt.Component.move(Component.java:2101)
at java.awt.Component.setLocation(Component.java:2090)
at java.awt.Window.setLocation(Window.java:921)
at java.awt.Window.init(Window.java:510)
at java.awt.Window.<init>(Window.java:436)
at java.awt.Window.<init>(Window.java:591)
at java.awt.Dialog.<init>(Dialog.java:665)
at java.awt.Dialog.<init>(Dialog.java:409)
at javax.swing.JDialog.<init>(JDialog.java:272)
at javax.swing.JDialog.<init>(JDialog.java:206)
at javax.swing.JDialog.<init>(JDialog.java:154)
at mutajara.MsgBoxPanel.<init>(Unknown Source)
at mutajara.MainApplet.<init>(Unknown Source)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
at java.lang.Class.newInstance(Class.java:442)
at com.sun.deploy.uitoolkit.impl.awt.AWTAppletAdapter$1.run(Unknown Source)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:301)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:756)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.awt.EventQueue$4.run(EventQueue.java:731)
at java.awt.EventQueue$4.run(EventQueue.java:729)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
Ignored exception: java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
basic: Dialog type is not candidate for embedding
为了解决这个异常,我发现主要原因是panel1实例为null,一旦我将这段代码添加到setBounds方法,代码就可以从safari中正确运行。
这是修改后的setBounds方法:
public void setBounds(int x,int y,int width,int height)
{
super.setBounds(x,y,width,height);
this.setLayout(null);
//This if statement solved the exception
if(panel1 == null)
{
return;
}
panel1.setBounds(4,6,this.getSize().width-8,this.getSize().height-12);
this.add(panel1);
}
请您澄清为什么会出现这种情况,以及为什么panel1实例变量为空。
答案 0 :(得分:0)
当你在Java中扩展另一个类时,你的类属性(例如panel1
)在超级构造函数完成后之后才会初始化。
我想JDialog's
构建的一部分涉及调用setBounds
。您已覆盖此方法,因此调用会覆盖您被覆盖的setBounds
。现在,由于JDialog
仍处于构建阶段,因此尚未创建和初始化panel1
。所以你得到你的空指针异常。
但你有更大的问题。 setBounds
可以在运行时调用很多。您不希望每次都删除并重新添加面板。
我会将这些行移出setBounds
,然后移到jbInit
方法的顶部:
this.setLayout(null);
this.add(panel1);
<强>更新强>
您可以将panel1.setBounds保留在那里:
public void setBounds(int x,int y,int width,int height) {
super.setBounds(x,y,width,height);
if (panel1 != null) {
panel1.setBounds(4,6,this.getSize().width-8,this.getSize().height-12);
}
}
或者,不是自己定位组件,而是通过使用LayoutManager让Swing为您完成:
//this.setLayout(null);
this.setLayout(new BorderLayout());
this.add(panel1);
// could use panel1.setBorder(...) to get your spacing around the outside