求解 JTextFile 无法 取得输入的信息 已贴出代码 抛 java.lang.NullPointerException
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class test extends JFrame implements ActionListener{
static String tempname = new String("");
static String temppasswd =new String("");
JTextField jtf1 ;
JTextField jtf2 ;
JLabel jl1 ;
JLabel jl2 ;
JButton jb1 ;
JPanel panel;
test()
{
this.setVisible(true);
this.setBounds(450,200, 300, 150);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel = new JPanel();
JLabel jl1 = new JLabel("用户名");
JLabel jl2 = new JLabel("密 码");
JTextField jtf1 = new JTextField(20);
JTextField jtf2 = new JTextField(20);
JButton jb = new JButton("登录");
this.add(panel);
panel.add(jl1);
panel.add(jtf1);
panel.add(jl2);
panel.add(jtf2);
panel.add(jb);
jb.addActionListener(this);
}
public static void main(final String[] args) {
test t = new test();
}
public void actionPerformed(ActionEvent arg0) {
tempname=jtf1.getText();
temppasswd=jtf2.getText();
FileReader fi = null;
try {
fi = new FileReader("z:/test.txt");
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
final BufferedReader bufr = new BufferedReader(fi);
String str = new String();
System.out.println("测试");
try {
while((str=bufr.readLine())!=null)
{
if(tempname.equalsIgnoreCase(str))
{
str=bufr.readLine();
if(str.equalsIgnoreCase(temppasswd))
{
System.out.println("登陆成功");
break;
}
}
}
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
--------------------编程问答-------------------- 都知道抛出了NPE了,就很容易判断为什么错误了啊,打印栈信息,定位代码行,看行中那个变量为null就知道原因了。 --------------------编程问答--------------------
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class test extends JFrame implements ActionListener{
static String tempname = new String("");
static String temppasswd =new String("");
JTextField jtf1 ;
JTextField jtf2 ;
JLabel jl1 ;
JLabel jl2 ;
JButton jb1 ;
JPanel panel;
test()
{
this.setVisible(true);
this.setBounds(450,200, 300, 150);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel = new JPanel();
JLabel jl1 = new JLabel("用户名");
JLabel jl2 = new JLabel("密 码");
jtf1 = new JTextField(20);
jtf2 = new JTextField(20);
JButton jb = new JButton("登录");
this.getContentPane().add(panel);
panel.add(jl1);
panel.add(jtf1);
panel.add(jl2);
panel.add(jtf2);
panel.add(jb);
jb.addActionListener(this);
}
public static void main(final String[] args) {
test t = new test();
}
public void actionPerformed(ActionEvent arg0) {
tempname=jtf1.getText();
temppasswd=jtf2.getText();
FileReader fi = null;
try {
fi = new FileReader("z:/test.txt");
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
final BufferedReader bufr = new BufferedReader(fi);
String str = new String();
System.out.println("测试");
try {
while((str=bufr.readLine())!=null)
{
if(tempname.equalsIgnoreCase(str))
{
str=bufr.readLine();
if(str.equalsIgnoreCase(temppasswd))
{
System.out.println("登陆成功");
break;
}
}
}
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
} --------------------编程问答-------------------- jtf1和jtf2重复定义了! --------------------编程问答-------------------- 把
JTextField jtf1 = new JTextField(20);
JTextField jtf2 = new JTextField(20);
改成
jtf1 = new JTextField(20);
jtf2 = new JTextField(20);
就不会错了! --------------------编程问答-------------------- JButton jb1 是多余的! --------------------编程问答-------------------- while((str=bufr.readLine())!=null)
{
if(tempname.equalsIgnoreCase(str))
{
str=bufr.readLine();
if(str.equalsIgnoreCase(temppasswd))
{
System.out.println("登陆成功");
break;这里面也有问题不能这样实现
补充:Java , Java SE