您将在下面找到我收到的错误消息。输出仅显示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);
}
}
对于那些给我解决方案的人,我将不胜感激。谢谢 编辑后输出如下所示:
答案 0 :(得分:1)
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 : ");
答案 1 :(得分:0)
您未在lbl3
的构造函数中初始化gui
。我猜这是一个错字 - 你有两行初始化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
答案 2 :(得分:0)
您需要在构造函数中添加以下行:
this.add(p1);