Java 我的代码中的错误线程中的异常 "AWT-EventQueue-0"
Java error in my code Exception in thread "AWT-EventQueue-0"
我正在打造一个学生管理器并从 YouTube 学到了一切。现在我陷入了困境。下面给出了代码,下面也给出了错误。
这是代码:
public class RegisterStart extends JFrame {
private JPanel contentPane;
private static JTextField uid, fname, lname, sa;
private static JPasswordField pass;
private static JComboBox sq;
private static Connection Conn;
private static ResultSet Rs;
static Properties prop;
static InputStream input;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try
{
RegisterStart frame = new RegisterStart();
frame.setVisible(true);
Randomize();
input = new FileInputStream("/Users/rajat/Programming/EclipseJava/Student Manager/src/config.properties");
prop.load(input);
DriverManager.registerDriver(new com.mysql.cj.jdbc.Driver());
Conn = DriverManager.getConnection(prop.getProperty("database"), prop.getProperty("dbuser"), prop.getProperty("dbpassword"));
Randomize();
}
catch (Exception e)
{
e.printStackTrace();
}
}
});
}
public RegisterStart() {
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 600, 440);
contentPane = new JPanel();
contentPane.setBackground(new Color(255, 204, 153));
contentPane.setBorder(null);
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
JPanel panel = new JPanel();
panel.setBorder(new EmptyBorder(20, 0, 0, 0));
panel.setBackground(new Color(255, 204, 153));
contentPane.add(panel, BorderLayout.NORTH);
JLabel lblAdminRegistration = new JLabel("ADMIN REGISTRATION");
lblAdminRegistration.setFont(new Font("Lucida Grande", Font.PLAIN, 20));
lblAdminRegistration.setBackground(new Color(255, 204, 153));
panel.add(lblAdminRegistration);
JPanel panel_1 = new JPanel();
panel_1.setBackground(new Color(255, 204, 153));
contentPane.add(panel_1, BorderLayout.CENTER);
panel_1.setLayout(null);
JLabel lblNewLabel = new JLabel("USER ID :");
lblNewLabel.setBounds(62, 45, 180, 16);
panel_1.add(lblNewLabel);
JLabel lblNewLabel_1 = new JLabel("FIRST NAME :");
lblNewLabel_1.setBounds(62, 83, 180, 16);
panel_1.add(lblNewLabel_1);
JLabel lblNewLabel_2 = new JLabel("LAST NAME :");
lblNewLabel_2.setBounds(62, 122, 180, 16);
panel_1.add(lblNewLabel_2);
JLabel lblPassworf = new JLabel("PASSWORD :");
lblPassworf.setBounds(62, 164, 180, 16);
panel_1.add(lblPassworf);
JLabel lblSecurityQuestions = new JLabel("SECURITY QUESTION : ");
lblSecurityQuestions.setBounds(62, 207, 180, 16);
panel_1.add(lblSecurityQuestions);
uid = new JTextField();
uid.setHorizontalAlignment(SwingConstants.CENTER);
uid.setBackground(Color.WHITE);
uid.setEditable(false);
uid.setBounds(249, 40, 278, 26);
panel_1.add(uid);
uid.setColumns(10);
fname = new JTextField();
fname.setBounds(249, 78, 278, 26);
panel_1.add(fname);
fname.setColumns(10);
lname = new JTextField();
lname.setBounds(249, 117, 278, 26);
panel_1.add(lname);
lname.setColumns(10);
sa = new JTextField();
sa.setBounds(249, 246, 278, 26);
panel_1.add(sa);
sa.setColumns(10);
JLabel lblSecurityAnswer = new JLabel("SECURITY ANSWER : ");
lblSecurityAnswer.setBounds(62, 251, 180, 16);
panel_1.add(lblSecurityAnswer);
sq = new JComboBox();
sq.setModel(new DefaultComboBoxModel(new String[] {"Which is your favorite animal?", "Who is your favorite Actor or Actress?", "Who is your first childhood friend?", "What is your hobby?", "Which is your dream Bike/Car?"}));
sq.setBounds(249, 203, 283, 27);
panel_1.add(sq);
JButton regbutt = new JButton("REGISTER");
regbutt.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Register();
}
});
regbutt.setBounds(257, 300, 117, 29);
panel_1.add(regbutt);
pass = new JPasswordField();
pass.setBounds(254, 159, 273, 26);
panel_1.add(pass);
}
private static void Randomize ()
{
Random r = new Random ();
int a = r.nextInt(1000000+1);
uid.setText(a+"");
}
private static void Register ()
{
char[] pswrd = pass.getPassword();
String pass2 = String.valueOf(pswrd);
Calendar cal1 = new GregorianCalendar();
java.util.Date CurrDate = cal1.getTime();
cal1.setTime(CurrDate);
cal1.add(Calendar.MONTH, 1);
java.sql.Date SqlDate3 = new java.sql.Date(CurrDate.getTime());
SimpleDateFormat DMY = new SimpleDateFormat("yyyy-mm-dd");
DMY.format(SqlDate3);
try
{
int x=0;
System.out.println(x++);
PreparedStatement ps = Conn.prepareStatement("insert into admin_database values (?,?,?,?,?,?,?)");
System.out.println(x++);
ps.setInt(1, Integer.parseInt(uid.getText()));
System.out.println(x++);
ps.setString(2, fname.getText());
System.out.println(x++);
ps.setString(3, lname.getText());
System.out.println(x++);
ps.setString(4, pswrd.toString());
System.out.println(x++);
ps.setString(5, sq.getSelectedItem().toString());
System.out.println(x++);
ps.setString(6, sa.getText());
System.out.println(x++);
ps.setDate(7, SqlDate3);
System.out.println(x++);
int i = ps.executeUpdate();
JOptionPane.showMessageDialog(null, "Thanks for Registering your UserID is "+uid.getText());
}
catch (SQLException e)
{
e.printStackTrace();
}
}
}
这是我遇到的错误:
java.lang.NullPointerException at
frames.RegisterStart.run(RegisterStart.java:58) at
java.desktop/java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:313)
at
java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:770)
at java.desktop/java.awt.EventQueue.access0(EventQueue.java:97)
at java.desktop/java.awt.EventQueue.run(EventQueue.java:721) at
java.desktop/java.awt.EventQueue.run(EventQueue.java:715) at
java.base/java.security.AccessController.doPrivileged(Native Method)
at
java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:87)
at
java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:740)
at
java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
at
java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
at
java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
at
java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
at
java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at
java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
0Exception in thread "AWT-EventQueue-0"
java.lang.NullPointerException at
frames.RegisterStart.Register(RegisterStart.java:190) at
frames.RegisterStart.access(RegisterStart.java:171) at
frames.RegisterStart.actionPerformed(RegisterStart.java:153) at
java.desktop/javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1967)
at
java.desktop/javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2308)
at
java.desktop/javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:405)
at
java.desktop/javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:262)
at
java.desktop/javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:270)
at
java.desktop/java.awt.Component.processMouseEvent(Component.java:6589)
at
java.desktop/javax.swing.JComponent.processMouseEvent(JComponent.java:3342)
at java.desktop/java.awt.Component.processEvent(Component.java:6354)
at java.desktop/java.awt.Container.processEvent(Container.java:2261)
at
java.desktop/java.awt.Component.dispatchEventImpl(Component.java:4966)
at
java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2319)
at java.desktop/java.awt.Component.dispatchEvent(Component.java:4798)
at
java.desktop/java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4914)
at
java.desktop/java.awt.LightweightDispatcher.processMouseEvent(Container.java:4543)
at
java.desktop/java.awt.LightweightDispatcher.dispatchEvent(Container.java:4484)
at
java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2305)
at java.desktop/java.awt.Window.dispatchEventImpl(Window.java:2772)
at java.desktop/java.awt.Component.dispatchEvent(Component.java:4798)
at
java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:772)
at java.desktop/java.awt.EventQueue.access0(EventQueue.java:97)
at java.desktop/java.awt.EventQueue.run(EventQueue.java:721) at
java.desktop/java.awt.EventQueue.run(EventQueue.java:715) at
java.base/java.security.AccessController.doPrivileged(Native Method)
at
java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:87)
at
java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:97)
at java.desktop/java.awt.EventQueue.run(EventQueue.java:745) at
java.desktop/java.awt.EventQueue.run(EventQueue.java:743) at
java.base/java.security.AccessController.doPrivileged(Native Method)
at
java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:87)
at
java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:742)
at
java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
at
java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
at
java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
at
java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
at
java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at
java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
2018-08-15 22:28:31.367 java[699:58449] Lookup: Unhandled exception
'JavaNativeException' caught in +[LULookupDefinitionModule
_focusTermUsingQueue:handler:]
我解决了我的问题。我刚刚将 Properties prop;
更改为 Properties prop = new Properties();
这是它的样子
public class RegisterStart extends JFrame {
private JPanel contentPane;
private static JTextField uid, fname, lname, sa;
private static JPasswordField pass;
private static JComboBox sq;
private static Connection Conn;
private static ResultSet Rs;
static Properties prop = new Properties();
static InputStream input = null;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try
{
RegisterStart frame = new RegisterStart();
frame.setVisible(true);
Randomize();
input = new FileInputStream("/Users/rajat/Programming/EclipseJava/Student Manager/src/config.properties");
prop.load(input);
DriverManager.registerDriver(new com.mysql.cj.jdbc.Driver());
Conn = DriverManager.getConnection(prop.getProperty("database"), prop.getProperty("dbuser"), prop.getProperty("dbpassword"));
Randomize();
}
catch (Exception e)
{
e.printStackTrace();
}
}
});
}
public RegisterStart() {
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 600, 440);
contentPane = new JPanel();
contentPane.setBackground(new Color(255, 204, 153));
contentPane.setBorder(null);
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
JPanel panel = new JPanel();
panel.setBorder(new EmptyBorder(20, 0, 0, 0));
panel.setBackground(new Color(255, 204, 153));
contentPane.add(panel, BorderLayout.NORTH);
JLabel lblAdminRegistration = new JLabel("ADMIN REGISTRATION");
lblAdminRegistration.setFont(new Font("Lucida Grande", Font.PLAIN, 20));
lblAdminRegistration.setBackground(new Color(255, 204, 153));
panel.add(lblAdminRegistration);
JPanel panel_1 = new JPanel();
panel_1.setBackground(new Color(255, 204, 153));
contentPane.add(panel_1, BorderLayout.CENTER);
panel_1.setLayout(null);
JLabel lblNewLabel = new JLabel("USER ID :");
lblNewLabel.setBounds(62, 45, 180, 16);
panel_1.add(lblNewLabel);
JLabel lblNewLabel_1 = new JLabel("FIRST NAME :");
lblNewLabel_1.setBounds(62, 83, 180, 16);
panel_1.add(lblNewLabel_1);
JLabel lblNewLabel_2 = new JLabel("LAST NAME :");
lblNewLabel_2.setBounds(62, 122, 180, 16);
panel_1.add(lblNewLabel_2);
JLabel lblPassworf = new JLabel("PASSWORD :");
lblPassworf.setBounds(62, 164, 180, 16);
panel_1.add(lblPassworf);
JLabel lblSecurityQuestions = new JLabel("SECURITY QUESTION : ");
lblSecurityQuestions.setBounds(62, 207, 180, 16);
panel_1.add(lblSecurityQuestions);
uid = new JTextField();
uid.setHorizontalAlignment(SwingConstants.CENTER);
uid.setBackground(Color.WHITE);
uid.setEditable(false);
uid.setBounds(249, 40, 278, 26);
panel_1.add(uid);
uid.setColumns(10);
fname = new JTextField();
fname.setBounds(249, 78, 278, 26);
panel_1.add(fname);
fname.setColumns(10);
lname = new JTextField();
lname.setBounds(249, 117, 278, 26);
panel_1.add(lname);
lname.setColumns(10);
sa = new JTextField();
sa.setBounds(249, 246, 278, 26);
panel_1.add(sa);
sa.setColumns(10);
JLabel lblSecurityAnswer = new JLabel("SECURITY ANSWER : ");
lblSecurityAnswer.setBounds(62, 251, 180, 16);
panel_1.add(lblSecurityAnswer);
sq = new JComboBox();
sq.setModel(new DefaultComboBoxModel(new String[] {"Which is your favorite animal?", "Who is your favorite Actor or Actress?", "Who is your first childhood friend?", "What is your hobby?", "Which is your dream Bike/Car?"}));
sq.setBounds(249, 203, 283, 27);
panel_1.add(sq);
JButton regbutt = new JButton("REGISTER");
regbutt.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Register();
}
});
regbutt.setBounds(257, 300, 117, 29);
panel_1.add(regbutt);
pass = new JPasswordField();
pass.setBounds(254, 159, 273, 26);
panel_1.add(pass);
}
private static void Randomize ()
{
Random r = new Random ();
int a = r.nextInt(1000000+1);
uid.setText(a+"");
}
private static void Register ()
{
char[] pswrd = pass.getPassword();
String pass2 = String.valueOf(pswrd);
Calendar cal1 = new GregorianCalendar();
java.util.Date CurrDate = cal1.getTime();
cal1.setTime(CurrDate);
cal1.add(Calendar.MONTH, 1);
java.sql.Date SqlDate3 = new java.sql.Date(CurrDate.getTime());
SimpleDateFormat DMY = new SimpleDateFormat("yyyy-mm-dd");
DMY.format(SqlDate3);
try
{
PreparedStatement ps = Conn.prepareStatement("insert into admin_database values (?,?,?,?,?,?,?)");
ps.setInt(1, Integer.parseInt(uid.getText()));
ps.setString(2, fname.getText());
ps.setString(3, lname.getText());
ps.setString(4, pswrd.toString());
ps.setString(5, sq.getSelectedItem().toString());
ps.setString(6, sa.getText());
ps.setDate(7, SqlDate3);
int i = ps.executeUpdate();
JOptionPane.showMessageDialog(null, "Thanks for Registering your UserID is "+uid.getText());
}
catch (SQLException e)
{
e.printStackTrace();
}
}
}
我正在打造一个学生管理器并从 YouTube 学到了一切。现在我陷入了困境。下面给出了代码,下面也给出了错误。
这是代码:
public class RegisterStart extends JFrame {
private JPanel contentPane;
private static JTextField uid, fname, lname, sa;
private static JPasswordField pass;
private static JComboBox sq;
private static Connection Conn;
private static ResultSet Rs;
static Properties prop;
static InputStream input;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try
{
RegisterStart frame = new RegisterStart();
frame.setVisible(true);
Randomize();
input = new FileInputStream("/Users/rajat/Programming/EclipseJava/Student Manager/src/config.properties");
prop.load(input);
DriverManager.registerDriver(new com.mysql.cj.jdbc.Driver());
Conn = DriverManager.getConnection(prop.getProperty("database"), prop.getProperty("dbuser"), prop.getProperty("dbpassword"));
Randomize();
}
catch (Exception e)
{
e.printStackTrace();
}
}
});
}
public RegisterStart() {
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 600, 440);
contentPane = new JPanel();
contentPane.setBackground(new Color(255, 204, 153));
contentPane.setBorder(null);
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
JPanel panel = new JPanel();
panel.setBorder(new EmptyBorder(20, 0, 0, 0));
panel.setBackground(new Color(255, 204, 153));
contentPane.add(panel, BorderLayout.NORTH);
JLabel lblAdminRegistration = new JLabel("ADMIN REGISTRATION");
lblAdminRegistration.setFont(new Font("Lucida Grande", Font.PLAIN, 20));
lblAdminRegistration.setBackground(new Color(255, 204, 153));
panel.add(lblAdminRegistration);
JPanel panel_1 = new JPanel();
panel_1.setBackground(new Color(255, 204, 153));
contentPane.add(panel_1, BorderLayout.CENTER);
panel_1.setLayout(null);
JLabel lblNewLabel = new JLabel("USER ID :");
lblNewLabel.setBounds(62, 45, 180, 16);
panel_1.add(lblNewLabel);
JLabel lblNewLabel_1 = new JLabel("FIRST NAME :");
lblNewLabel_1.setBounds(62, 83, 180, 16);
panel_1.add(lblNewLabel_1);
JLabel lblNewLabel_2 = new JLabel("LAST NAME :");
lblNewLabel_2.setBounds(62, 122, 180, 16);
panel_1.add(lblNewLabel_2);
JLabel lblPassworf = new JLabel("PASSWORD :");
lblPassworf.setBounds(62, 164, 180, 16);
panel_1.add(lblPassworf);
JLabel lblSecurityQuestions = new JLabel("SECURITY QUESTION : ");
lblSecurityQuestions.setBounds(62, 207, 180, 16);
panel_1.add(lblSecurityQuestions);
uid = new JTextField();
uid.setHorizontalAlignment(SwingConstants.CENTER);
uid.setBackground(Color.WHITE);
uid.setEditable(false);
uid.setBounds(249, 40, 278, 26);
panel_1.add(uid);
uid.setColumns(10);
fname = new JTextField();
fname.setBounds(249, 78, 278, 26);
panel_1.add(fname);
fname.setColumns(10);
lname = new JTextField();
lname.setBounds(249, 117, 278, 26);
panel_1.add(lname);
lname.setColumns(10);
sa = new JTextField();
sa.setBounds(249, 246, 278, 26);
panel_1.add(sa);
sa.setColumns(10);
JLabel lblSecurityAnswer = new JLabel("SECURITY ANSWER : ");
lblSecurityAnswer.setBounds(62, 251, 180, 16);
panel_1.add(lblSecurityAnswer);
sq = new JComboBox();
sq.setModel(new DefaultComboBoxModel(new String[] {"Which is your favorite animal?", "Who is your favorite Actor or Actress?", "Who is your first childhood friend?", "What is your hobby?", "Which is your dream Bike/Car?"}));
sq.setBounds(249, 203, 283, 27);
panel_1.add(sq);
JButton regbutt = new JButton("REGISTER");
regbutt.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Register();
}
});
regbutt.setBounds(257, 300, 117, 29);
panel_1.add(regbutt);
pass = new JPasswordField();
pass.setBounds(254, 159, 273, 26);
panel_1.add(pass);
}
private static void Randomize ()
{
Random r = new Random ();
int a = r.nextInt(1000000+1);
uid.setText(a+"");
}
private static void Register ()
{
char[] pswrd = pass.getPassword();
String pass2 = String.valueOf(pswrd);
Calendar cal1 = new GregorianCalendar();
java.util.Date CurrDate = cal1.getTime();
cal1.setTime(CurrDate);
cal1.add(Calendar.MONTH, 1);
java.sql.Date SqlDate3 = new java.sql.Date(CurrDate.getTime());
SimpleDateFormat DMY = new SimpleDateFormat("yyyy-mm-dd");
DMY.format(SqlDate3);
try
{
int x=0;
System.out.println(x++);
PreparedStatement ps = Conn.prepareStatement("insert into admin_database values (?,?,?,?,?,?,?)");
System.out.println(x++);
ps.setInt(1, Integer.parseInt(uid.getText()));
System.out.println(x++);
ps.setString(2, fname.getText());
System.out.println(x++);
ps.setString(3, lname.getText());
System.out.println(x++);
ps.setString(4, pswrd.toString());
System.out.println(x++);
ps.setString(5, sq.getSelectedItem().toString());
System.out.println(x++);
ps.setString(6, sa.getText());
System.out.println(x++);
ps.setDate(7, SqlDate3);
System.out.println(x++);
int i = ps.executeUpdate();
JOptionPane.showMessageDialog(null, "Thanks for Registering your UserID is "+uid.getText());
}
catch (SQLException e)
{
e.printStackTrace();
}
}
}
这是我遇到的错误:
java.lang.NullPointerException at frames.RegisterStart.run(RegisterStart.java:58) at java.desktop/java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:313) at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:770) at java.desktop/java.awt.EventQueue.access0(EventQueue.java:97) at java.desktop/java.awt.EventQueue.run(EventQueue.java:721) at java.desktop/java.awt.EventQueue.run(EventQueue.java:715) at java.base/java.security.AccessController.doPrivileged(Native Method) at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:87) at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:740) at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203) at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124) at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113) at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109) at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101) at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90) 0Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at frames.RegisterStart.Register(RegisterStart.java:190) at frames.RegisterStart.access(RegisterStart.java:171) at frames.RegisterStart.actionPerformed(RegisterStart.java:153) at java.desktop/javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1967) at java.desktop/javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2308) at java.desktop/javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:405) at java.desktop/javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:262) at java.desktop/javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:270) at java.desktop/java.awt.Component.processMouseEvent(Component.java:6589) at java.desktop/javax.swing.JComponent.processMouseEvent(JComponent.java:3342) at java.desktop/java.awt.Component.processEvent(Component.java:6354) at java.desktop/java.awt.Container.processEvent(Container.java:2261) at java.desktop/java.awt.Component.dispatchEventImpl(Component.java:4966) at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2319) at java.desktop/java.awt.Component.dispatchEvent(Component.java:4798) at java.desktop/java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4914) at java.desktop/java.awt.LightweightDispatcher.processMouseEvent(Container.java:4543) at java.desktop/java.awt.LightweightDispatcher.dispatchEvent(Container.java:4484) at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2305) at java.desktop/java.awt.Window.dispatchEventImpl(Window.java:2772) at java.desktop/java.awt.Component.dispatchEvent(Component.java:4798) at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:772) at java.desktop/java.awt.EventQueue.access0(EventQueue.java:97) at java.desktop/java.awt.EventQueue.run(EventQueue.java:721) at java.desktop/java.awt.EventQueue.run(EventQueue.java:715) at java.base/java.security.AccessController.doPrivileged(Native Method) at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:87) at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:97) at java.desktop/java.awt.EventQueue.run(EventQueue.java:745) at java.desktop/java.awt.EventQueue.run(EventQueue.java:743) at java.base/java.security.AccessController.doPrivileged(Native Method) at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:87) at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:742) at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203) at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124) at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113) at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109) at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101) at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90) 2018-08-15 22:28:31.367 java[699:58449] Lookup: Unhandled exception 'JavaNativeException' caught in +[LULookupDefinitionModule _focusTermUsingQueue:handler:]
我解决了我的问题。我刚刚将 Properties prop;
更改为 Properties prop = new Properties();
这是它的样子
public class RegisterStart extends JFrame {
private JPanel contentPane;
private static JTextField uid, fname, lname, sa;
private static JPasswordField pass;
private static JComboBox sq;
private static Connection Conn;
private static ResultSet Rs;
static Properties prop = new Properties();
static InputStream input = null;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try
{
RegisterStart frame = new RegisterStart();
frame.setVisible(true);
Randomize();
input = new FileInputStream("/Users/rajat/Programming/EclipseJava/Student Manager/src/config.properties");
prop.load(input);
DriverManager.registerDriver(new com.mysql.cj.jdbc.Driver());
Conn = DriverManager.getConnection(prop.getProperty("database"), prop.getProperty("dbuser"), prop.getProperty("dbpassword"));
Randomize();
}
catch (Exception e)
{
e.printStackTrace();
}
}
});
}
public RegisterStart() {
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 600, 440);
contentPane = new JPanel();
contentPane.setBackground(new Color(255, 204, 153));
contentPane.setBorder(null);
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
JPanel panel = new JPanel();
panel.setBorder(new EmptyBorder(20, 0, 0, 0));
panel.setBackground(new Color(255, 204, 153));
contentPane.add(panel, BorderLayout.NORTH);
JLabel lblAdminRegistration = new JLabel("ADMIN REGISTRATION");
lblAdminRegistration.setFont(new Font("Lucida Grande", Font.PLAIN, 20));
lblAdminRegistration.setBackground(new Color(255, 204, 153));
panel.add(lblAdminRegistration);
JPanel panel_1 = new JPanel();
panel_1.setBackground(new Color(255, 204, 153));
contentPane.add(panel_1, BorderLayout.CENTER);
panel_1.setLayout(null);
JLabel lblNewLabel = new JLabel("USER ID :");
lblNewLabel.setBounds(62, 45, 180, 16);
panel_1.add(lblNewLabel);
JLabel lblNewLabel_1 = new JLabel("FIRST NAME :");
lblNewLabel_1.setBounds(62, 83, 180, 16);
panel_1.add(lblNewLabel_1);
JLabel lblNewLabel_2 = new JLabel("LAST NAME :");
lblNewLabel_2.setBounds(62, 122, 180, 16);
panel_1.add(lblNewLabel_2);
JLabel lblPassworf = new JLabel("PASSWORD :");
lblPassworf.setBounds(62, 164, 180, 16);
panel_1.add(lblPassworf);
JLabel lblSecurityQuestions = new JLabel("SECURITY QUESTION : ");
lblSecurityQuestions.setBounds(62, 207, 180, 16);
panel_1.add(lblSecurityQuestions);
uid = new JTextField();
uid.setHorizontalAlignment(SwingConstants.CENTER);
uid.setBackground(Color.WHITE);
uid.setEditable(false);
uid.setBounds(249, 40, 278, 26);
panel_1.add(uid);
uid.setColumns(10);
fname = new JTextField();
fname.setBounds(249, 78, 278, 26);
panel_1.add(fname);
fname.setColumns(10);
lname = new JTextField();
lname.setBounds(249, 117, 278, 26);
panel_1.add(lname);
lname.setColumns(10);
sa = new JTextField();
sa.setBounds(249, 246, 278, 26);
panel_1.add(sa);
sa.setColumns(10);
JLabel lblSecurityAnswer = new JLabel("SECURITY ANSWER : ");
lblSecurityAnswer.setBounds(62, 251, 180, 16);
panel_1.add(lblSecurityAnswer);
sq = new JComboBox();
sq.setModel(new DefaultComboBoxModel(new String[] {"Which is your favorite animal?", "Who is your favorite Actor or Actress?", "Who is your first childhood friend?", "What is your hobby?", "Which is your dream Bike/Car?"}));
sq.setBounds(249, 203, 283, 27);
panel_1.add(sq);
JButton regbutt = new JButton("REGISTER");
regbutt.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Register();
}
});
regbutt.setBounds(257, 300, 117, 29);
panel_1.add(regbutt);
pass = new JPasswordField();
pass.setBounds(254, 159, 273, 26);
panel_1.add(pass);
}
private static void Randomize ()
{
Random r = new Random ();
int a = r.nextInt(1000000+1);
uid.setText(a+"");
}
private static void Register ()
{
char[] pswrd = pass.getPassword();
String pass2 = String.valueOf(pswrd);
Calendar cal1 = new GregorianCalendar();
java.util.Date CurrDate = cal1.getTime();
cal1.setTime(CurrDate);
cal1.add(Calendar.MONTH, 1);
java.sql.Date SqlDate3 = new java.sql.Date(CurrDate.getTime());
SimpleDateFormat DMY = new SimpleDateFormat("yyyy-mm-dd");
DMY.format(SqlDate3);
try
{
PreparedStatement ps = Conn.prepareStatement("insert into admin_database values (?,?,?,?,?,?,?)");
ps.setInt(1, Integer.parseInt(uid.getText()));
ps.setString(2, fname.getText());
ps.setString(3, lname.getText());
ps.setString(4, pswrd.toString());
ps.setString(5, sq.getSelectedItem().toString());
ps.setString(6, sa.getText());
ps.setDate(7, SqlDate3);
int i = ps.executeUpdate();
JOptionPane.showMessageDialog(null, "Thanks for Registering your UserID is "+uid.getText());
}
catch (SQLException e)
{
e.printStackTrace();
}
}
}