感谢您有时间阅读此内容。我的问题是,当我调用另一个类时,它给了我很多错误,我不知道如何解决它。我希望你能:) ty和美好的一天:)
这是我的代码
public class Xavier1 extends JFrame implements ActionListener,Runnable
{
JButton a[] = new JButton [10];
JLabel pin,acc;
JTextField acc1;
JButton del,ok,crt;
int c=0,d=0,e=0;
JPasswordField pin1;
JFrame x;
String us,pw;
JButton res,back,next;
JLabel n,accn,bal,age,sex,dep,pins;
JFrame X,r;
JTextField n1,accn1,bal1,sex1,age1,dep1;
JPasswordField pins1;
JLabel Xn,Xacc,Xbal,Xage,Xsex;
String xn,xacc,xbal,xage,xsex;
public void run()
{
}
public Xavier1()
{
x = new JFrame("ATM");
x.setLayout(null);
x.setBounds(400,100,550,500);
x.setVisible(true);
JPanel x3 = new JPanel();
x.add(x3);
x3.setVisible(true);
x3.setBounds(150,420,250,100);
crt = new JButton("Create Account");
crt.addActionListener(this);
x3.add(crt);
JPanel x2 = new JPanel();
x.add(x2);
x2.setBounds(150,10,250,100);
x2.setVisible(true);
x2.setLayout(new GridLayout(4,2));
pin = new JLabel("PIN");
acc = new JLabel("ACCOUNT NUMBER");
pin1 = new JPasswordField(20);
acc1 = new JTextField(20);
x2.add(acc);
x2.add(acc1);
x2.add(pin);
x2.add(pin1);
JPanel x1 = new JPanel();
x.add(x1);
x1.setBounds(150,200,250,200);
x1.setVisible(true);
x1.setLayout(new GridLayout(4,3));
for(int b=0;b<10;b++)
{
a[b] = new JButton();
a[b].setText(Integer.toString(b));
x1.add(a[b]);
a[b].addActionListener(this);
}
ok = new JButton("OK");
ok.addActionListener(this);
del = new JButton("DEL");
del.addActionListener(this);
x1.add(ok);
x1.add(del);
}
public static void main (String args[])
{
Xavier1 xs = new Xavier1();
Thread xd = new Thread(xs);
xd.setDaemon(true);
}
public void actionPerformed(ActionEvent z)
{
Object zz = z.getSource();
if(e == 0)
{
for(int c=0;c<a.length;c++)
{
if(zz == a[c])
{
acc1.setText(acc1.getText()+a[c].getText());
}
}
}
if(zz == ok)
{
e++;
}
if(e==2)
{
GateKeeper();
}
if(e == 1)
{
for(int d=0;d<a.length;d++)
{
if(zz == a[d])
{
pin1.setText(pin1.getText()+a[d].getText());
}
}
}
if(zz == crt)
{
Reg rs = new Reg();
Thread sr = new Thread(rs);
sr.start();
x.setVisible(false);
}
if(zz == del)
{
if(e == 0)
{
acc1.setText(null);
}
if(e == 1)
{
pin1.setText(null);
}
}
}
public void GateKeeper()
{
try
{
Scanner sc = new Scanner(new File("DATABASE.txt"));
int loop = sc.nextInt();
sc.nextLine();
for(int gk=0;gk<loop;gk++)
{
String ps[] = sc.nextLine().split(" ");
us = ps[0];
pw = ps[1];
if(acc1.getText().equals(us));
{
if(pin1.getText().equals(pw))
{
xn = ps[2];
xage = ps[3];
xsex = ps[4];
xbal = ps[5];
xacc = ps[0];
Xn = new JLabel("NAME: "+xn);
Xacc = new JLabel("ACCOUNT NUMBER: "+xacc);
Xage = new JLabel("AGE: "+xage);
Xsex = new JLabel("SEX: "+xsex);
Xbal = new JLabel("BALANCE: "+xbal);
Bank b = new Bank();
Thread xb = new Thread(b);
xb.start();
x.setVisible(false);
break;
}
}
}
}
catch(FileNotFoundException s)
{
}
}
}
这是我遇到问题的代码。
public void GateKeeper()
{
try
{
Scanner sc = new Scanner(new File("DATABASE.txt"));
int loop = sc.nextInt();
sc.nextLine();
for(int gk=0;gk<loop;gk++)
{
String ps[] = sc.nextLine().split(" ");
us = ps[0];
pw = ps[1];
if(acc1.getText().equals(us));
{
if(pin1.getText().equals(pw))
{
xn = ps[2];
xage = ps[3];
xsex = ps[4];
xbal = ps[5];
xacc = ps[0];
Xn = new JLabel("NAME: "+xn);
Xacc = new JLabel("ACCOUNT NUMBER: "+xacc);
Xage = new JLabel("AGE: "+xage);
Xsex = new JLabel("SEX: "+xsex);
Xbal = new JLabel("BALANCE: "+xbal);
Bank b = new Bank();
Thread xb = new Thread(b);
xb.start();
x.setVisible(false);
break;
}
}
}
}
catch(FileNotFoundException s)
{
}
}
这是GateKeeper正在调用的类。
public class Bank extends Xavier1
{
public void reader()
{
try
{
FileReader fr = new FileReader("DATABASE.TXT");
BufferedReader br = new BufferedReader(fr);
}
catch(FileNotFoundException s)
{
}
}
public Bank()
{
X = new JFrame("BANK");
X.setVisible(true);
X.setBounds(400,100,550,500);
X.setLayout(null);
x.setVisible(false);
JPanel info = new JPanel();
X.add(info);
info.setVisible(true);
info.setBounds(50,10,400,400);
info.setLayout(new GridLayout(7,2));
info.add(Xn);
info.add(Xacc);
info.add(Xage);
info.add(Xsex);
info.add(Xbal);
}
public static void main(String args[])
{
}
public void actionPerformed(ActionEvent z1)
{
Object z2 = z1.getSource();
}
}
这是我得到的错误:(
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at java.awt.Container.addImpl(Container.java:1086)
at java.awt.Container.add(Container.java:410)
at Bank.<init>(Bank.java:43)
at Xavier1.GateKeeper(Xavier1.java:183)
at Xavier1.actionPerformed(Xavier1.java:117)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6505)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6270)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4861)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2719)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:735)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:694)
at java.awt.EventQueue$3.run(EventQueue.java:692)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:708)
at java.awt.EventQueue$4.run(EventQueue.java:706)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:705)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
答案 0 :(得分:0)
很难说出异常发生在哪里,但你的Bank类中有两个变量“X”和“x”,其中x小写的变量未正确初始化?
X.setLayout(null);
x.setVisible(false);
答案 1 :(得分:0)
看起来你将null传递给java.awt.Container.add(组件comp)的一个调用
public Bank() {
...
X.add(info);
...
info.add(Xn);
info.add(Xacc);
info.add(Xage);
info.add(Xsex);
info.add(Xbal);
}
检查所有值(info,Xn,Xacc等)是否为空
答案 2 :(得分:0)
您的错误就在这些行中。您永远不会实现这些变量:x, Xn, Xacc, Xage, Xsex, Xbal
。
x.setVisible(false);
info.add(Xn);
info.add(Xacc);
info.add(Xage);
info.add(Xsex);
info.add(Xbal);
我还建议您对对象使用正确的命名。现在很难看出x和X之间的区别是什么。它还使您更容易避免出现奇怪的错误。
答案 3 :(得分:0)
我觉得,当调用Bank类的构造函数时,它会隐式调用超类(Xavier1),它将所有全局变量初始化为null。因此,您在Xn,Xacc,Xage,Xsex上获得此异常。我希望你能理解我。