当我尝试获取文本字段的值并在另一个类中使用它时,我得到了NullPointer异常。
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at gui.AddNewPasswordWindow.actionPerformed(AddNewPasswordWindow.java:86)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(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.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(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.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(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)
当我点击添加按钮
时,这是我收到错误的类public class AddNewPasswordWindow implements ActionListener{
static PasswordStorageWindow PasswordStorageWindow;
private JFrame frame;
private JTextField passwordField;
private JTextField usernameField;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
AddNewPasswordWindow window = new AddNewPasswordWindow(PasswordStorageWindow);
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public AddNewPasswordWindow(PasswordStorageWindow PasswordStorageWindow) {
this.PasswordStorageWindow = PasswordStorageWindow;
initialize();
}
private void initialize() {
frame = new JFrame();
frame.setAlwaysOnTop(true);
frame.setBounds(100, 100, 240, 240);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JLabel lblUsername = new JLabel("Username:");
lblUsername.setFont(new Font("Tahoma", Font.PLAIN, 14));
lblUsername.setBounds(10, 11, 204, 20);
frame.getContentPane().add(lblUsername);
JLabel lblPassword = new JLabel("Password:");
lblPassword.setFont(new Font("Tahoma", Font.PLAIN, 14));
lblPassword.setBounds(10, 81, 143, 20);
frame.getContentPane().add(lblPassword);
JButton btnAdd = new JButton("ADD");
btnAdd.setBounds(64, 160, 89, 23);
frame.getContentPane().add(btnAdd);
btnAdd.addActionListener(this);
btnAdd.setActionCommand("add");
JButton btnGenerate = new JButton("Generate");
btnGenerate.setBounds(125, 82, 89, 23);
frame.getContentPane().add(btnGenerate);
btnGenerate.addActionListener(this);
btnGenerate.setActionCommand("generate");
usernameField = new JTextField();
usernameField.setBounds(30, 42, 184, 20);
frame.getContentPane().add(usernameField);
usernameField.setColumns(10);
passwordField = new JTextField();
passwordField.setBounds(30, 116, 184, 20);
frame.getContentPane().add(passwordField);
passwordField.setColumns(10);
}
public void actionPerformed(ActionEvent e) {
if("generate".equals(e.getActionCommand())){
Random generator = new Random();
passwordField.setText(generator.toString());
}
if("add".equals(e.getActionCommand())){
String username = usernameField.getText();
String password = passwordField.getText();
PasswordStorageWindow.getUsername(username);
PasswordStorageWindow.getPassword(password);
}
}
}
这是我想要存储和显示文本框的值的类......
public class PasswordStorageWindow implements ActionListener{
private JFrame frmExistingPassword;
private JTable table;
public ArrayList<String> data1;
String username = LoginWindow.Username();
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
PasswordStorageWindow PasswordStorageWindow = new PasswordStorageWindow();
PasswordStorageWindow.frmExistingPassword.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public void getUsername(String username){
data1.add(username);
}
public void getPassword(String password){
data1.add(password);
}
public PasswordStorageWindow() throws FileNotFoundException {
initialize();
}
private void initialize() throws FileNotFoundException {
String[] columnNames = {"Site", "Password"};
File f = new File("D:\\Programming\\Projects\\Workspace\\Password Storage v1.1\\usernames\\" + username + "\\" + username + ".pass.txt");
Scanner scan = new Scanner(f);
data1 = new ArrayList<String>();
while (scan.hasNextLine())
{
data1.add(scan.nextLine());
}
Object[][] data = new Object[data1.size()/2+1][2];
data[0][0] = "Site";
data[0][1] = "Password";
int counter = 1;
for (int i = 0; i < data1.size(); i+=2){
data[counter][0] = data1.get(i);
data[counter][1] = data1.get(i+1);
counter++;
}
frmExistingPassword = new JFrame();
frmExistingPassword.setTitle("Existing Passwords");
frmExistingPassword.setBounds(100, 100, 560, 600);
frmExistingPassword.setResizable(false);
frmExistingPassword.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frmExistingPassword.getContentPane().setLayout(null);
ImageIcon add = new ImageIcon("add.png");
ImageIcon delete = new ImageIcon("delete.png");
JButton buttonAdd = new JButton("ADD", add);
buttonAdd.setBounds(162, 4, 180, 75);
frmExistingPassword.getContentPane().add(buttonAdd);
buttonAdd.setActionCommand("add");
JButton buttonDelete = new JButton("Delete password", delete);
buttonDelete.setBounds(360, 4, 180, 75);
frmExistingPassword.getContentPane().add(buttonDelete);
buttonDelete.setActionCommand("delete");
JButton buttonBack = new JButton("Go back");
buttonBack.setBounds(5, 4, 140, 75);
frmExistingPassword.getContentPane().add(buttonBack);
buttonBack.setActionCommand("back");
table = new JTable(data, columnNames)
{
public boolean isCellEditable(int row, int col)
{
return false;
}
};
table.setBounds(2, 106, 542, 455);
frmExistingPassword.getContentPane().add(table);
table.setPreferredScrollableViewportSize(new Dimension(520, 500));
table.setFillsViewportHeight(true);
buttonAdd.addActionListener(this);
buttonDelete.addActionListener(this);
buttonBack.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
if("back".equals(e.getActionCommand())){
MainMenuWindow.main(null);
frmExistingPassword.dispose();
}
if("add".equals(e.getActionCommand())){
File file = new File("D:\\Programming\\Projects\\Workspace\\Password Storage v1.1\\usernames\\" + username + "\\" + username + ".pass.txt");
if(!file.exists())
{
try {
PrintWriter out = new PrintWriter(file);
out.close();
} catch (FileNotFoundException e1) {}
}
AddNewPasswordWindow newWindow = new AddNewPasswordWindow(null);
AddNewPasswordWindow.main(null);
}
try
{
PrintWriter out = new PrintWriter("D:\\Programming\\Projects\\Workspace\\Password Storage v1.1\\usernames\\" + username + "\\" + username + ".pass.txt");
for (int i = 0; i<data1.size(); i++)
{
out.println(data1.get(i));
}
out.close();
PasswordStorageWindow frmExistingPassword1 = new PasswordStorageWindow();
frmExistingPassword1.main(null);
frmExistingPassword.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frmExistingPassword.setVisible(false);
} catch (Exception e1)
{
System.out.println("Error writing new file/saving changes...!");
}
}
}
答案 0 :(得分:1)
如果没有你告诉我们你的第86行在哪里,我觉得你永远不会初始化PasswordStorageWindow
。
你有:
static PasswordStorageWindow PasswordStorageWindow; // Not initialized anywhere
private JFrame frame;
private JTextField passwordField;
private JTextField usernameField;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
// You are passing a null value here, PasswordStorageWindow is not initialized.
AddNewPasswordWindow window = new AddNewPasswordWindow(PasswordStorageWindow);
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
检查代码中的注释。
在旁注中,您应该遵循java约定并使用camelCase,您的变量和方法名称应以小写字母开头。