public class init extends JFrame implements ActionListener,ItemListener{
static ServerSocket socket;
static JLabel jl1,jl2;
static JPanel jp1,jp2,jp3,jp4;
static JTextField jt1,jt2;
static JButton jbt1,jbt2;
private PrintWriter pw;
private ServerSocket server;
public static int port;//定义端口
private static int backlog=90;
private static String ServerIp;
private ExecutorService threadpool;//定义线程池
HashMap clientlist = new HashMap();//定义用来保存所有客户端连接的端口hashMap()
Scanner scan = new Scanner(System.in);
private List<PrintWriter> allout;
public init(){
super("服务端");
try{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}catch(Exception e){
e.printStackTrace();
}
jl1 = new JLabel("IP:");
jt1 = new JTextField(15);
jt1.addKeyListener(new KeyAdapter(){
public void KeyPressed(KeyEvent e){
if(e.getKeyCode()==10&&!jt1.getText().equals("")){
ServerIp = jt1.getText();
jt2.requestFocus();
}
}
});
jp1 = new JPanel();
jp1.add(jl1);jp1.add(jl2);
jl2 = new JLabel("PORT:");
jt2 = new JTextField(15);
jt2.addKeyListener(new KeyAdapter(){
public void KeyPressed(KeyEvent e){
if(e.getKeyCode()==10&&!jt2.getText().equals("")){
port = Integer.parseInt(jt2.getText());
jbt1.requestFocus();
}
}
});
jp2 = new JPanel();
jp2.add(jl2);jp2.add(jt2);
jbt1 = new JButton("Login");
jbt1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
if(e.getSource()==jbt1&&ServerIp!=null&&!String.valueOf(port).equals("")){
}
}
});
jp3 = new JPanel();
jp3.setLayout(new BorderLayout());
jp3.add("North",jp1);jp3.add("Center",jp2);jp3.add("South",jbt1);
this.add(jp3);
this.pack();
this.setResizable(false);
this.setVisible(true);
}
public static void main(String [] args){
new init();
}
说jp1.add哪一行实例化出错
Exception in thread "main" java.lang.NullPointerException
at java.awt.Container.addImpl(Container.java:1045)
at java.awt.Container.add(Container.java:365)
at che.init.<init>(init.java:59)
at che.init.main(init.java:97)
------解决思路----------------------
jp1.add(jl1);jp1.add(jl2);
这一句jl2没有初始化