请帮我整理一下这个java.awt.BoxLayout无法共享或NullPointerException [英] Please help me to sort out this java.awt.BoxLayout can't be shared or NullPointerException

查看:103
本文介绍了请帮我整理一下这个java.awt.BoxLayout无法共享或NullPointerException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的代码Boxlayout示例符合要求,但在运行时抛出以下异常:

The below code "Boxlayout example complies well but throws me the below exception at runtime:

Exception in thread "main" java.lang.NullPointerException

at java.awt.Container.addImpl(Container.java: 1027)
at java.awt.Container.add(Container.java: 935)
at javax.swing.JFrame.addImpl(JFrame.java: 545)
at java.awt.Container.add(Container.java: 352)
at BoxExample.launchFrame(BoxExample.java:26)
at BoxExample.main(BoxExample.java:40)

请帮我排序因为它浪费了我将近1天的生命..

Please help me out to sort out this as it wasted nearly 1 day of my life..

//Boxlayout example

import java.awt.*;

import javax.swing.*;

public class BoxExample
{

public JFrame f;

public JButton b1, b2,b3,b4,b5;

public BoxExample()

{

f=new JFrame("Box example");

f.setTitle("Box Layout Example");

f.setSize(150, 150);

JButton b1=new JButton("Button 1");

JButton b2=new JButton("Button 2");

JButton b3=new JButton("Button 3");

JButton b4=new JButton("Button 4");

JButton b5=new JButton("Button 5");

}


public void launchFrame()

{

System.out.println("inside lf");

f.setLayout(new BoxLayout(f,BoxLayout.Y_AXIS));

System.out.println("after layset");

f.add(b1);

f.add(b2);

f.add(b3);

f.add(b4);

f.add(b5);

f.pack();

f.setVisible(true);

}


public static void main(String args[])

{

BoxExample guiWindow=new BoxExample();

System.out.println("main");

guiWindow.launchFrame();

}

}


推荐答案

你正在通过在类中声明它们,然后重新声明并初始化它们来遮蔽一些变量,主要是JButton变量b1,b2,b3,b4和b5。在构造函数中。构造函数中新声明的变量与类中声明的变量不同,因此类变量将保持为null。

You are shadowing some variables, mainly the JButton variables b1, b2, b3, b4, and b5, by declaring them in the class, and then re-declaring and initializing them in the constructor. The newly declared variables in the constructor are not the same ones that are declared in the class, and so the class variables will remain null.

解决方案:不要重新声明构造函数中的变量。所以不是这样:

Solution: don't re-declare the variables in the constructor. So instead of this:

class Foo {
  private Bar bar;

  public Foo() {
    Bar bar = new Bar(); // bar is re-declared here!
  }
}

执行此操作:

class Foo {
  private Bar bar;

  public Foo() {
    bar = new Bar(); // notice the difference!
  }
}

此外,只要你有NullPointerException(NPE)外观小心地抛出异常的行,在BoxExample类的第26行:

Also, whenever you have a NullPointerException (NPE) look carefully at the line that throws the exception, here line 26 of the BoxExample class:

at BoxExample.launchFrame(BoxExample.java:26)

你会在该行找到其中一个变量为空。如果你找到哪个变量,你通常可以回溯你的代码,看看为什么它是空的。

You'll find on that line that one of the variables is null. If you find out which variable, you can often backtrack through your code and see why it is null.

这篇关于请帮我整理一下这个java.awt.BoxLayout无法共享或NullPointerException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆