我正在处理的某些代码出现问题。我有一个登录系统和一个带有getter和setter的类来存储用户信息。如果登录成功,我使用setter来存储信息。登录后,会出现一个新的GUI,我正在尝试使用getter将信息放在新的GUI上。我一直得到一个空指针异常错误,我不明白为什么?
继承登录GUI代码
Database_Classes.User userclass;
User_Repository user;
Main_Frame main = new Main_Frame();
Connection connection;
ResultSet resultset;
JdbcRowSetImpl RowSet;
CallableStatement callablestatement;
String URL = "jdbc:mysql://localhost:3306/gamebacklog";
String DB_Username = "root";
String DB_Password = "root";
public Login_Frame()
{
initComponents();
user = new User_Repository();
userclass = new Database_Classes.User();
}
public final void Connect()
{
RowSet = new JdbcRowSetImpl();
try
{
connection = DriverManager.getConnection(URL, DB_Username, DB_Password);
RowSet.setUrl(URL);
RowSet.setUsername(DB_Username);
RowSet.setPassword(DB_Password);
} catch (Exception ex)
{
JOptionPane.showMessageDialog(null, "There was a problem connecting to the database.");
System.out.print(ex);
System.exit(0);
}
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents()
{
UsernameField = new javax.swing.JTextField();
UsernameLabel = new javax.swing.JLabel();
PasswordLabel = new javax.swing.JLabel();
LoginButton = new javax.swing.JButton();
RegisterLabel = new javax.swing.JLabel();
WelcomeLabel = new javax.swing.JLabel();
RegisterButton = new javax.swing.JButton();
UnsuccessfulLabel = new javax.swing.JLabel();
ExitButton = new javax.swing.JButton();
PasswordField = new javax.swing.JPasswordField();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
UsernameLabel.setFont(new java.awt.Font("Dialog", 0, 14)); // NOI18N
UsernameLabel.setText("Username");
PasswordLabel.setFont(new java.awt.Font("Dialog", 0, 14)); // NOI18N
PasswordLabel.setText("Password");
LoginButton.setText("Login");
LoginButton.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(java.awt.event.ActionEvent evt)
{
LoginButtonActionPerformed(evt);
}
});
RegisterLabel.setFont(new java.awt.Font("Dialog", 0, 14)); // NOI18N
RegisterLabel.setText("Register Here");
WelcomeLabel.setFont(new java.awt.Font("Dialog", 0, 18)); // NOI18N
WelcomeLabel.setText(" Login form for the system");
WelcomeLabel.setVerticalAlignment(javax.swing.SwingConstants.TOP);
RegisterButton.setText("Register");
RegisterButton.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(java.awt.event.ActionEvent evt)
{
RegisterButtonActionPerformed(evt);
}
});
ExitButton.setFont(new java.awt.Font("Dialog", 0, 14)); // NOI18N
ExitButton.setText("Exit");
ExitButton.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(java.awt.event.ActionEvent evt)
{
ExitButtonActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(ExitButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(UnsuccessfulLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(layout.createSequentialGroup()
.addComponent(UsernameLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(UsernameField, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(RegisterLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(RegisterButton, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(PasswordLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(LoginButton, javax.swing.GroupLayout.DEFAULT_SIZE, 150, Short.MAX_VALUE)
.addComponent(PasswordField)))))
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
.addGap(22, 22, 22)
.addComponent(WelcomeLabel)
.addGap(0, 21, Short.MAX_VALUE)))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap()
.addComponent(WelcomeLabel)
.addGap(25, 25, 25)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(UsernameLabel)
.addComponent(UsernameField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(PasswordLabel)
.addComponent(PasswordField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(LoginButton)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(UnsuccessfulLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(RegisterButton)
.addComponent(RegisterLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(ExitButton)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void LoginButtonActionPerformed(java.awt.event.ActionEvent evt)
{
try
{
Connect();
String username = UsernameField.getText();
String password = PasswordField.getText();
resultset = user.Login(connection, username, password);
if (resultset.next())
{
result(resultset);
} else
{
UnsuccessfulLabel.setText("The Login Attempt was unsuccessful");
}
} catch (Exception ex)
{
JOptionPane.showMessageDialog(null, "Encountered a problem with the Server. Please try again later");
System.out.println("two \t" + ex.toString());
} finally
{
try
{
user.Close();
connection.close();
main.setVisible(true);
this.setVisible(false);
} catch (Exception ex)
{
JOptionPane.showMessageDialog(null, "Encountered a problem with the Server. Please try again later");
System.out.println("three \t" + ex.toString());
}
}
}
private void RegisterButtonActionPerformed(java.awt.event.ActionEvent evt)
{
Register_Frame Register = new Register_Frame();
Register.setVisible(true);
this.setVisible(false);
}
private void ExitButtonActionPerformed(java.awt.event.ActionEvent evt)
{
System.exit(0);
}
public static void main(String args[])
{
java.awt.EventQueue.invokeLater(new Runnable()
{
public void run()
{
new Login_Frame().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton ExitButton;
private javax.swing.JButton LoginButton;
private javax.swing.JPasswordField PasswordField;
private javax.swing.JLabel PasswordLabel;
private javax.swing.JButton RegisterButton;
private javax.swing.JLabel RegisterLabel;
private javax.swing.JLabel UnsuccessfulLabel;
private javax.swing.JTextField UsernameField;
private javax.swing.JLabel UsernameLabel;
private javax.swing.JLabel WelcomeLabel;
// End of variables declaration
public void result(ResultSet resultSet)
{
try
{
int ID = resultSet.getInt(1);
userclass.SetID(ID);
String firstname = resultSet.getString(2);
userclass.SetFirstname(firstname);
String lastname = resultSet.getString(3);
userclass.SetLastname(lastname);
String username = resultSet.getString(4);
userclass.SetUsername(username);
int accesslevel = resultSet.getInt(6);
userclass.SetAccessLevel(accesslevel);
System.out.println(userclass.GetID() + " " + userclass.GetFirstname() + " " + userclass.GetLastname() + " " + userclass.GetUsername() + " " + userclass.GetAccesslevel());
} catch (Exception ex)
{
System.out.println(ex);
}
}
这是其他GUI
Database_Classes.User userclass;
public Main_Frame()
{
initComponents();
}
public void get()
{
jLabel2.setText("Firstname = " + userclass.GetFirstname());
jLabel1.setText("ID = " + userclass.GetID());
jLabel3.setText("Lastname = " + userclass.GetLastname());
jLabel4.setText("Username = " + userclass.GetUsername());
jLabel5.setText("accesslevel = " + userclass.GetAccesslevel());
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents()
{
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
jLabel5 = new javax.swing.JLabel();
LogoutButton = new javax.swing.JButton();
SetButton = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel1.setText("jLabel1");
jLabel2.setText("jLabel2");
jLabel3.setText("jLabel3");
jLabel4.setText("jLabel4");
jLabel5.setText("jLabel5");
LogoutButton.setText("Logout");
LogoutButton.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(java.awt.event.ActionEvent evt)
{
LogoutButtonActionPerformed(evt);
}
});
SetButton.setText("Results");
SetButton.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(java.awt.event.ActionEvent evt)
{
SetButtonActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 172, Short.MAX_VALUE)
.addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jLabel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGap(0, 0, Short.MAX_VALUE))
.addGroup(layout.createSequentialGroup()
.addComponent(LogoutButton)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 37, Short.MAX_VALUE)
.addComponent(SetButton, javax.swing.GroupLayout.PREFERRED_SIZE, 127, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel1)
.addGap(18, 18, 18)
.addComponent(jLabel2)
.addGap(18, 18, 18)
.addComponent(jLabel3)
.addGap(18, 18, 18)
.addComponent(jLabel4)
.addGap(18, 18, 18)
.addComponent(jLabel5)
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(LogoutButton)
.addComponent(SetButton))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void LogoutButtonActionPerformed(java.awt.event.ActionEvent evt)
{
Login_Frame login = new Login_Frame();
login.setVisible(true);
this.setVisible(false);
}
private void SetButtonActionPerformed(java.awt.event.ActionEvent evt)
{
get();
}
// Variables declaration - do not modify
private javax.swing.JButton LogoutButton;
private javax.swing.JButton SetButton;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
// End of variables declaration
最后是用户类代码
private int ID;
private String firstname;
private String lastname;
private String username;
private String password;
private int accesslevel;
public int GetID()
{
return ID;
}
public void SetID(int ID)
{
this.ID = ID;
}
public String GetFirstname()
{
return firstname;
}
public void SetFirstname(String firstname)
{
this.firstname = firstname;
}
public String GetLastname()
{
return lastname;
}
public void SetLastname(String lastname)
{
this.lastname = lastname;
}
public String GetUsername()
{
return username;
}
public void SetUsername(String username)
{
this.username = username;
}
public String GetPassword()
{
return password;
}
public void SetPassword(String password)
{
this.password = password;
}
public int GetAccesslevel()
{
return accesslevel;
}
public void SetAccessLevel(int accesslevel)
{
this.accesslevel = accesslevel;
}
这也是Null Pointer异常错误不断出现
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at GameBacklog.Main_Frame.get(Main_Frame.java:16)
at GameBacklog.Main_Frame.SetButtonActionPerformed(Main_Frame.java:119)
at GameBacklog.Main_Frame.access$100(Main_Frame.java:3)
at GameBacklog.Main_Frame$2.actionPerformed(Main_Frame.java:62)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
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:6535)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
at java.awt.Component.processEvent(Component.java:6300)
at java.awt.Container.processEvent(Container.java:2236)
at java.awt.Component.dispatchEventImpl(Component.java:4891)
at java.awt.Container.dispatchEventImpl(Container.java:2294)
at java.awt.Component.dispatchEvent(Component.java:4713)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
at java.awt.Container.dispatchEventImpl(Container.java:2280)
at java.awt.Window.dispatchEventImpl(Window.java:2750)
at java.awt.Component.dispatchEvent(Component.java:4713)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.awt.EventQueue$4.run(EventQueue.java:731)
at java.awt.EventQueue$4.run(EventQueue.java:729)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)