为什么我得到一个 NullPointerException 而没有显示我添加的组件?
Why am I getting a NullPointerException and no display the component i added?
下面是我收到的错误消息。输出只显示 JFrame
没有内容。
Exception in thread "main" java.lang.NullPointerException
at java.awt.Container.addImpl(Container.java:1043)
at java.awt.Container.add(Container.java:363)
at gui.<init>(gui.java:37)
at gui.main(gui.java:15)
Java Result: 1
代码如下:
import javax.swing.*;
import java.awt.*;
public class gui extends JFrame {
JPanel p1;
JTextField tf1,tf2,tf3,tf4;
JLabel lbl1,lbl2,lbl3,lbl5,lbl6,lbl7,lbl8,lbl9,lbl10;
JTextArea txtMessage;
JButton b1,b2,b3,b4,b5;
public static void main(String [] args) {
gui f = new gui();
f.setTitle("RSA.");
f.setVisible(true);
f.setSize(700,500);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public gui() {
lbl1 = new JLabel("Enter first prime number : ");
lbl2 = new JLabel("Enter second prime number: ");
lbl3 = new JLabel("Enter the message to be send : ");
tf1 = new JTextField(15);
tf2 = new JTextField(15);
txtMessage = new JTextArea("Please enter",5,20);
p1 = new JPanel();
p1.add(lbl1);
p1.add(tf1);
p1.add(lbl2);
p1.add(tf2);
p1.add(lbl3);
p1.add(txtMessage);
p1.setLayout(new GridLayout(3,2));
p1.add(lbl1);
p1.add(tf1);
p1.add(lbl2);
p1.add(tf2);
p1.add(lbl3);
p1.add(txtMessage);
}
}
我将感谢那些为 me.Thanks 提供解决方案的人
编辑后输出如下图所示:
lbl3
永远不会设置为 null
以外的值,而是设置 lbl2
两次:
lbl2=new JLabel("Enter second prime number: ");
lbl2=new JLabel("Enter the message to be send : ");
将其替换为
lbl2=new JLabel("Enter second prime number: ");
lbl3=new JLabel("Enter the message to be send : ");
您没有在 gui
的构造函数中初始化 lbl3
。我猜这是一个错字 - 你有两行初始化 lbl2
:
lbl2=new JLabel("Enter second prime number: ");
lbl2=new JLabel("Enter the message to be send : ");
把第二个改成lbl3
就可以了:
lbl2=new JLabel("Enter second prime number: ");
lbl3=new JLabel("Enter the message to be send : "); // here
您需要在构造函数中添加这一行:
this.add(p1);
下面是我收到的错误消息。输出只显示 JFrame
没有内容。
Exception in thread "main" java.lang.NullPointerException
at java.awt.Container.addImpl(Container.java:1043)
at java.awt.Container.add(Container.java:363)
at gui.<init>(gui.java:37)
at gui.main(gui.java:15)
Java Result: 1
代码如下:
import javax.swing.*;
import java.awt.*;
public class gui extends JFrame {
JPanel p1;
JTextField tf1,tf2,tf3,tf4;
JLabel lbl1,lbl2,lbl3,lbl5,lbl6,lbl7,lbl8,lbl9,lbl10;
JTextArea txtMessage;
JButton b1,b2,b3,b4,b5;
public static void main(String [] args) {
gui f = new gui();
f.setTitle("RSA.");
f.setVisible(true);
f.setSize(700,500);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public gui() {
lbl1 = new JLabel("Enter first prime number : ");
lbl2 = new JLabel("Enter second prime number: ");
lbl3 = new JLabel("Enter the message to be send : ");
tf1 = new JTextField(15);
tf2 = new JTextField(15);
txtMessage = new JTextArea("Please enter",5,20);
p1 = new JPanel();
p1.add(lbl1);
p1.add(tf1);
p1.add(lbl2);
p1.add(tf2);
p1.add(lbl3);
p1.add(txtMessage);
p1.setLayout(new GridLayout(3,2));
p1.add(lbl1);
p1.add(tf1);
p1.add(lbl2);
p1.add(tf2);
p1.add(lbl3);
p1.add(txtMessage);
}
}
我将感谢那些为 me.Thanks 提供解决方案的人
编辑后输出如下图所示:
lbl3
永远不会设置为 null
以外的值,而是设置 lbl2
两次:
lbl2=new JLabel("Enter second prime number: ");
lbl2=new JLabel("Enter the message to be send : ");
将其替换为
lbl2=new JLabel("Enter second prime number: ");
lbl3=new JLabel("Enter the message to be send : ");
您没有在 gui
的构造函数中初始化 lbl3
。我猜这是一个错字 - 你有两行初始化 lbl2
:
lbl2=new JLabel("Enter second prime number: ");
lbl2=new JLabel("Enter the message to be send : ");
把第二个改成lbl3
就可以了:
lbl2=new JLabel("Enter second prime number: ");
lbl3=new JLabel("Enter the message to be send : "); // here
您需要在构造函数中添加这一行:
this.add(p1);