大家好,我是一个刚开始学java的学生,现在第一次写了一个聊天室的程序,可是登录的时候总是报出空指针的错误,但是我反复查了好几天也找不出问题,写望各位高手们帮忙给看看。
谢谢了~
这里是登录界面的代码,用户名和密码的文件已经创建了。
package project.chatroom;import java.io.*;
import java.awt.*;
import java.util.*;
import javax.swing.*;
import java.awt.event.*;import java.util.List;/**
* 聊天室登陆程序
*
* @author 歸來の菊
*/
public class MainFrame {
String username;
String str_username;
String str_passwd; /**
* 登陆界面程序
* 定义各种组件和功能
*
* @author 歸來の菊
*/
public void init() {
//定义主窗体
final JFrame window = new JFrame("聊天室登陆程序");
window.setBounds(400, 250, 250, 200);
window.setLayout(new GridLayout(4, 2));
window.setUndecorated(true);
window.getRootPane().setWindowDecorationStyle(JRootPane.PLAIN_DIALOG);
window.addWindowListener(new WindowListener() {
public void windowActivated(WindowEvent e) {
}
public void windowClosed(WindowEvent e) {
}
public void windowClosing(WindowEvent e) {
System.exit(0);
}
public void windowDeactivated(WindowEvent e) {
}
public void windowDeiconified(WindowEvent e) {
}
public void windowIconified(WindowEvent e) {
}
public void windowOpened(WindowEvent e) {
}
}); //创建组件并且构造组件的功能
JLabel jl_username = new JLabel("帐号:");
JLabel jl_password = new JLabel("密码:");
JLabel welcome = new JLabel("请登陆聊天室");
final JTextField jt_username = new JTextField("", 10);
final JPasswordField jp_password = new JPasswordField("", 10);
JButton jb_login = new JButton("登陆");
JButton jb_reg = new JButton("没有帐号?");
//构造登陆按钮的功能
jb_login.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
str_username = jt_username.getText();
str_passwd = new String(jp_password.getPassword());
FileInputStream fis = new FileInputStream("Users.info");
ObjectInputStream ois = new ObjectInputStream(fis);
List list = (List) ois.readObject();
Iterator it = list.iterator();
while (it.hasNext()) {
Info info = (Info) it.next();
if (info.getusername().equals(str_username)&& info.getpassword().equals(str_passwd)) {
window.setVisible(false);
Client client = new Client(jt_username.getText());
client.client();
break;
}
window.setVisible(false);
Error pe = new Error();
pe.error();
break;
}
ois.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}); //构造没有帐号?按钮的功能
jb_reg.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
window.setVisible(false);
RegFrame reg = new RegFrame();
reg.init();
}
});
//监听密码框的Enter事件
jp_password.addKeyListener(new KeyListener() {
public void keyPressed(KeyEvent e) {
String strKey = e.getKeyText(e.getKeyCode());
if (strKey != null && strKey.equals("Enter")) {
try {
str_username = jt_username.getText();
str_passwd = new String(jp_password.getPassword());
FileInputStream fis = new FileInputStream("Users.info");
ObjectInputStream ois = new ObjectInputStream(fis);
List list = (List) ois.readObject();
Iterator it = list.iterator();
while (it.hasNext()) {
Info info = (Info) it.next();
if (info.getusername().equals(str_username)&& info.getpassword().equals(str_passwd)) {
window.setVisible(false);
Client client = new Client(jt_username.getText());
client.client();
break;
}
Error pe = new Error();
pe.error();
window.setVisible(false);
break;
}
ois.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
public void keyReleased(KeyEvent e) {
}
public void keyTyped(KeyEvent e) {
}
});
//创建容器
JPanel jp1 = new JPanel();
JPanel jp2 = new JPanel();
JPanel jp3 = new JPanel();
JPanel jp4 = new JPanel();
//向容器中添加组件
jp1.add(welcome);
jp2.add(jl_username);
jp2.add(jt_username);
jp3.add(jl_password);
jp3.add(jp_password);
jp4.add(jb_login);
jp4.add(jb_reg);
//向主窗体中添加容器
window.add(jp1);
window.add(jp2);
window.add(jp3);
window.add(jp4);
//主窗体显示设置
window.setVisible(true);
} /**
* 入口方法
*
* @author 歸來の菊
*/
public static void main(String[] args) {
MainFrame mf = new MainFrame();
mf.init();
}}出现的问题是,输入好用户名密码后,点击登录,出现错误信息,如下:
java.lang.NullPointerException
at project.chatroom.MainFrame$3.keyPressed(MainFrame.java:117)
at java.awt.Component.processKeyEvent(Unknown Source)
at javax.swing.JComponent.processKeyEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.KeyboardFocusManager.redispatchEvent(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.dispatchEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
谢谢了~
这里是登录界面的代码,用户名和密码的文件已经创建了。
package project.chatroom;import java.io.*;
import java.awt.*;
import java.util.*;
import javax.swing.*;
import java.awt.event.*;import java.util.List;/**
* 聊天室登陆程序
*
* @author 歸來の菊
*/
public class MainFrame {
String username;
String str_username;
String str_passwd; /**
* 登陆界面程序
* 定义各种组件和功能
*
* @author 歸來の菊
*/
public void init() {
//定义主窗体
final JFrame window = new JFrame("聊天室登陆程序");
window.setBounds(400, 250, 250, 200);
window.setLayout(new GridLayout(4, 2));
window.setUndecorated(true);
window.getRootPane().setWindowDecorationStyle(JRootPane.PLAIN_DIALOG);
window.addWindowListener(new WindowListener() {
public void windowActivated(WindowEvent e) {
}
public void windowClosed(WindowEvent e) {
}
public void windowClosing(WindowEvent e) {
System.exit(0);
}
public void windowDeactivated(WindowEvent e) {
}
public void windowDeiconified(WindowEvent e) {
}
public void windowIconified(WindowEvent e) {
}
public void windowOpened(WindowEvent e) {
}
}); //创建组件并且构造组件的功能
JLabel jl_username = new JLabel("帐号:");
JLabel jl_password = new JLabel("密码:");
JLabel welcome = new JLabel("请登陆聊天室");
final JTextField jt_username = new JTextField("", 10);
final JPasswordField jp_password = new JPasswordField("", 10);
JButton jb_login = new JButton("登陆");
JButton jb_reg = new JButton("没有帐号?");
//构造登陆按钮的功能
jb_login.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
str_username = jt_username.getText();
str_passwd = new String(jp_password.getPassword());
FileInputStream fis = new FileInputStream("Users.info");
ObjectInputStream ois = new ObjectInputStream(fis);
List list = (List) ois.readObject();
Iterator it = list.iterator();
while (it.hasNext()) {
Info info = (Info) it.next();
if (info.getusername().equals(str_username)&& info.getpassword().equals(str_passwd)) {
window.setVisible(false);
Client client = new Client(jt_username.getText());
client.client();
break;
}
window.setVisible(false);
Error pe = new Error();
pe.error();
break;
}
ois.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}); //构造没有帐号?按钮的功能
jb_reg.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
window.setVisible(false);
RegFrame reg = new RegFrame();
reg.init();
}
});
//监听密码框的Enter事件
jp_password.addKeyListener(new KeyListener() {
public void keyPressed(KeyEvent e) {
String strKey = e.getKeyText(e.getKeyCode());
if (strKey != null && strKey.equals("Enter")) {
try {
str_username = jt_username.getText();
str_passwd = new String(jp_password.getPassword());
FileInputStream fis = new FileInputStream("Users.info");
ObjectInputStream ois = new ObjectInputStream(fis);
List list = (List) ois.readObject();
Iterator it = list.iterator();
while (it.hasNext()) {
Info info = (Info) it.next();
if (info.getusername().equals(str_username)&& info.getpassword().equals(str_passwd)) {
window.setVisible(false);
Client client = new Client(jt_username.getText());
client.client();
break;
}
Error pe = new Error();
pe.error();
window.setVisible(false);
break;
}
ois.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
public void keyReleased(KeyEvent e) {
}
public void keyTyped(KeyEvent e) {
}
});
//创建容器
JPanel jp1 = new JPanel();
JPanel jp2 = new JPanel();
JPanel jp3 = new JPanel();
JPanel jp4 = new JPanel();
//向容器中添加组件
jp1.add(welcome);
jp2.add(jl_username);
jp2.add(jt_username);
jp3.add(jl_password);
jp3.add(jp_password);
jp4.add(jb_login);
jp4.add(jb_reg);
//向主窗体中添加容器
window.add(jp1);
window.add(jp2);
window.add(jp3);
window.add(jp4);
//主窗体显示设置
window.setVisible(true);
} /**
* 入口方法
*
* @author 歸來の菊
*/
public static void main(String[] args) {
MainFrame mf = new MainFrame();
mf.init();
}}出现的问题是,输入好用户名密码后,点击登录,出现错误信息,如下:
java.lang.NullPointerException
at project.chatroom.MainFrame$3.keyPressed(MainFrame.java:117)
at java.awt.Component.processKeyEvent(Unknown Source)
at javax.swing.JComponent.processKeyEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.KeyboardFocusManager.redispatchEvent(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.dispatchEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
下次发帖子时候问清楚点吧 好不??
我刚把你的代码复制过来eclipse里..好多错误..
直接的红叉叉..不了解你想要什么样的结果..思维过程是怎么样的
这大概是没人回答的一个原因吧..
你再看看..实在搞不明白了 可以加我QQ 7003406 ..把上面说的哪些告诉我
咱一起商讨商讨...
import java.awt.*;
import java.util.*;
import javax.swing.*;
import java.awt.event.*;import java.util.List;/**
* 聊天室登陆程序
*
* @author 歸來の菊
*/
public class MainFrame {
String username;
String str_username;
String str_passwd; /**
* 登陆界面程序
* 定义各种组件和功能
*
* @author 歸來の菊
*/
public void init() {
//定义主窗体
final JFrame window = new JFrame("聊天室登陆程序");
window.setBounds(400, 250, 250, 200);
window.setLayout(new GridLayout(4, 2));
window.setUndecorated(true);
window.getRootPane().setWindowDecorationStyle(JRootPane.PLAIN_DIALOG);
window.addWindowListener(new WindowListener() {
public void windowActivated(WindowEvent e) {
}
public void windowClosed(WindowEvent e) {
}
public void windowClosing(WindowEvent e) {
System.exit(0);
}
public void windowDeactivated(WindowEvent e) {
}
public void windowDeiconified(WindowEvent e) {
}
public void windowIconified(WindowEvent e) {
}
public void windowOpened(WindowEvent e) {
}
}); //创建组件并且构造组件的功能
JLabel jl_username = new JLabel("帐号:");
JLabel jl_password = new JLabel("密码:");
JLabel welcome = new JLabel("请登陆聊天室");
final JTextField jt_username = new JTextField("", 10);
final JPasswordField jp_password = new JPasswordField("", 10);
JButton jb_login = new JButton("登陆");
JButton jb_reg = new JButton("没有帐号?");
//构造登陆按钮的功能
jb_login.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
str_username = jt_username.getText();
str_passwd = new String(jp_password.getPassword());
FileInputStream fis = new FileInputStream("Users.info");
ObjectInputStream ois = new ObjectInputStream(fis);
List list = (List) ois.readObject();
Iterator it = list.iterator();
while (it.hasNext()) {
Info info = (Info) it.next();
if (info.getusername().equals(str_username)&& info.getpassword().equals(str_passwd)) {
window.setVisible(false);
Client client = new Client(jt_username.getText());
client.client();
break;
}
window.setVisible(false);
Error pe = new Error();
pe.error();
break;
}
ois.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}); //构造没有帐号?按钮的功能
jb_reg.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
window.setVisible(false);
RegFrame reg = new RegFrame();
reg.init();
}
});
//监听密码框的Enter事件
jp_password.addKeyListener(new KeyListener() {
public void keyPressed(KeyEvent e) {
String strKey = e.getKeyText(e.getKeyCode());
if (strKey != null && strKey.equals("Enter")) {
try {
str_username = jt_username.getText();
str_passwd = new String(jp_password.getPassword());
FileInputStream fis = new FileInputStream("Users.info");
ObjectInputStream ois = new ObjectInputStream(fis);
List list = (List) ois.readObject();
Iterator it = list.iterator();
while (it.hasNext()) {
Info info = (Info) it.next();
if (info.getusername().equals(str_username)&& info.getpassword().equals(str_passwd)) {
window.setVisible(false);
Client client = new Client(jt_username.getText());
client.client();
break;
}
Error pe = new Error();
pe.error();
window.setVisible(false);
break;
}
ois.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
public void keyReleased(KeyEvent e) {
}
public void keyTyped(KeyEvent e) {
}
});
//创建容器
JPanel jp1 = new JPanel();
JPanel jp2 = new JPanel();
JPanel jp3 = new JPanel();
JPanel jp4 = new JPanel();
//向容器中添加组件
jp1.add(welcome);
jp2.add(jl_username);
jp2.add(jt_username);
jp3.add(jl_password);
jp3.add(jp_password);
jp4.add(jb_login);
jp4.add(jb_reg);
//向主窗体中添加容器
window.add(jp1);
window.add(jp2);
window.add(jp3);
window.add(jp4);
//主窗体显示设置
window.setVisible(true);
} /**
* 入口方法
*
* @author 歸來の菊
*/
public static void main(String[] args) {
MainFrame mf = new MainFrame();
mf.init();
}}
真的这么多一般的人难得看
不过自己想想应该能解决的吧我初学不久不能帮你了