为怎么3个类编译都通过了,当执行的时候出现了:Exception in thread:"java.lang.nullException" at com.hz.jimmy.BanUI.<int>(BankUI.java:32);等错误,这个是什么原因呢:
那为高手帮我一下。
-------------------------------------------------------------------
package com.hz.jimmy;
import java.awt.*;
import javax.swing.*;
public class BankUI extends JPanel{
protected final static String names[]={"Account number","First Name","Last Name","Balance","Transaction Amount"};
protected JLabel labels[];
protected JTextField fields[];
protected JButton doTask1,doTask2;
protected JPanel innerPanelCenter, innerPanelSouth;
protected int size;
public static final int ACCOUNT=0, FIRSTNAME=1, LASTNAME =2, BALANCE =3, TRANSACTOIN =4;
public BankUI(int mySize)
{
size=mySize;
labels=new JLabel[size];
fields=new JTextField[size];
for(int count=0;count<labels.length;count++)
{
labels[count]=new JLabel(names[count]);
}
for(int count=0;count<fields.length;count++)
{
fields[count]=new JTextField();
}
for(int count=0;count<size;count++)
{
innerPanelCenter.add(labels[count]);
innerPanelCenter.add(fields[count]);
}
doTask1=new JButton();
doTask2=new JButton();
innerPanelSouth=new JPanel();
innerPanelSouth.add(doTask1);
innerPanelSouth.add(doTask2);
setLayout(new BorderLayout());
add(innerPanelCenter,BorderLayout.CENTER);
add(innerPanelSouth,BorderLayout.SOUTH);
validate();
}
public JButton getDoTask1Button()
{
return doTask1;
}
public JButton getDoTask2Button()
{
return doTask2;
}
public JTextField[] getFields()
{
return fields;
}
public void clearFields()
{
for(int count=0;count<size;count++)
fields[count].setText("");
}
public void setFieldValues(String newstring[]) throws IllegalArgumentException
{
if (newstring.length!=size)
throw new IllegalArgumentException("There must be"+size+"string in the array!");
for(int count=0;count<size;count++)
fields[count].setText(newstring[count]);
}
public String[] getFieldValues()
{
String values[]=new String[size];
for(int count=0;count<size;count++)
values[count]=fields[count].getText();
return values;
}
}
-------------------------------------------------------
package com.hz.jimmy;
import java.io.Serializable;
public class AccountRecord implements Serializable{
private int account; //帐号;
private String firstname;
private String lastname;
private double balance;
public AccountRecord()
{
this(0,"","",0.0);
}
public AccountRecord(int acct,String first,String last,double bal)
{
setAccount(acct);
setFirstName(first);
setLastName(lastname);
setBalance(bal);
}
public void setAccount(int acct)
{
account=acct;
}
public void setFirstName(String first)
{
firstname=first;
}
public void setLastName(String last)
{
lastname=last;
}
public void setBalance(double bal)
{
balance=bal;
}
public int getAccount()
{
return account;
}
public String getFirstName()
{
return firstname;
}
public String getLastName()
{
return lastname;
}
public double getBalance()
{
return balance;
}
}
---------------------------------------------------
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import com.hz.jimmy.BankUI;
import com.hz.jimmy.AccountRecord;
public class CreateSequentialFile extends JFrame{
private ObjectOutputStream output;
private BankUI userInterface;
private JButton enterbutton,openbutton;
public CreateSequentialFile()
{
super("UserInterFace OF EveryBody");
userInterface=new BankUI(4);
getContentPane().add(userInterface,BorderLayout.CENTER);
openbutton=userInterface.getDoTask1Button();
openbutton.setText("Save into File......");
openbutton.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent Event)
{
openFile();
}
}
);
enterbutton=userInterface.getDoTask2Button();
enterbutton.setText("Enter");
enterbutton.setEnabled(false);
enterbutton.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent Event)
{
addRecord();
}
}
);
addWindowListener(
new WindowAdapter(){
public void windowClosing(WindowEvent Event)
{
if(output!=null)
addRecord();
closeFile();
}
}
);
setSize(600,600);
setVisible(true);
}
private void openFile()
{
JFileChooser filechooer=new JFileChooser();
filechooer.setFileSelectionMode(JFileChooser.FILES_ONLY);
int result=filechooer.showSaveDialog(this);
if (result==JFileChooser.CANCEL_OPTION)
return;
File fileName=filechooer.getSelectedFile();
if(fileName==null||fileName.getName().equals(""))
JOptionPane.showMessageDialog(this,"Invalid File Name","Invalid File Name",JOptionPane.ERROR_MESSAGE);
else
{
try{
output=new ObjectOutputStream(new FileOutputStream(fileName));
openbutton.setEnabled(false);
enterbutton.setEnabled(true);
}
catch(IOException IOException){
JOptionPane.showMessageDialog(this,"Error Opening File","Error",JOptionPane.ERROR_MESSAGE);
}
}
}
private void closeFile()
{
try{
output.close();
System.exit(0);
}
catch(IOException IOException)
{
JOptionPane.showMessageDialog(this,"Error Closeing file","Error",JOptionPane.ERROR_MESSAGE);
System.exit(1);
}
}
public void addRecord()
{
int accountNumber=0;
AccountRecord record;
String fieldValues[]=userInterface.getFieldValues();
if(!fieldValues[BankUI.ACCOUNT].equals("")){
try{
accountNumber=Integer.parseInt(fieldValues[BankUI.ACCOUNT]);
if(accountNumber>0){
record=new AccountRecord(
accountNumber,fieldValues[BankUI.FIRSTNAME],fieldValues[BankUI.LASTNAME],Double.parseDouble(fieldValues[BankUI.BALANCE]));
output.writeObject(record);
output.flush();
}
else{
JOptionPane.showMessageDialog(this,"Account number must be greater than 0","Bad Account number",JOptionPane.ERROR_MESSAGE);
}
userInterface.clearFields();
}
catch(NumberFormatException formationException){
JOptionPane.showMessageDialog(this,"Bad account number or balance","Invalid Number Format",JOptionPane.ERROR_MESSAGE);
closeFile();
}
catch(IOException IOException){
JOptionPane.showMessageDialog(this,"Error writing to file","IO Exception",JOptionPane.ERROR_MESSAGE);
}
}
}
public static void main(String args[])
{
new CreateSequentialFile();
}
}
你的innerPanelCenter根本就没new,怎么就直接用了
楼主有没有自己看出错信息啊,很明显的么