我正在建立一个学生管理器,并从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在 frame.RegisterStart $ 1.run(RegisterStart.java:58)位于 java.desktop / java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:313) 在 java.desktop / java.awt.EventQueue.dispatchEventImpl(EventQueue.java:770) 在java.desktop / java.awt.EventQueue.access $ 600(EventQueue.java:97) 在java.desktop / java.awt.EventQueue $ 4.run(EventQueue.java:721)在 java.desktop / java.awt.EventQueue $ 4.run(EventQueue.java:715)在 java.base / java.security.AccessController.doPrivileged(本机方法) 在 java.base / java.security.ProtectionDomain $ JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:87) 在 java.desktop / java.awt.EventQueue.dispatchEvent(EventQueue.java:740) 在 java.desktop / java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203) 在 java.desktop / java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124) 在 java.desktop / java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113) 在 java.desktop / java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109) 在 java.desktop / java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101) 在 java.desktop / java.awt.EventDispatchThread.run(EventDispatchThread.java:90) 0线程“ AWT-EventQueue-0”中的异常 java.lang.NullPointerException在 frame.RegisterStart.Register(RegisterStart.java:190)位于 frame.RegisterStart.access $ 2(RegisterStart.java:171)位于 frame.RegisterStart $ 2.actionPerformed(RegisterStart.java:153)位于 java.desktop / javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1967) 在 java.desktop / javax.swing.AbstractButton $ Handler.actionPerformed(AbstractButton.java:2308) 在 java.desktop / javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:405) 在 java.desktop / javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:262) 在 java.desktop / javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:270) 在 java.desktop / java.awt.Component.processMouseEvent(Component.java:6589) 在 java.desktop / javax.swing.JComponent.processMouseEvent(JComponent.java:3342) 在java.desktop / java.awt.Component.processEvent(Component.java:6354) 在java.desktop / java.awt.Container.processEvent(Container.java:2261) 在 java.desktop / java.awt.Component.dispatchEventImpl(Component.java:4966) 在 java.desktop / java.awt.Container.dispatchEventImpl(Container.java:2319) 在java.desktop / java.awt.Component.dispatchEvent(Component.java:4798) 在 java.desktop / java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4914) 在 java.desktop / java.awt.LightweightDispatcher.processMouseEvent(Container.java:4543) 在 java.desktop / java.awt.LightweightDispatcher.dispatchEvent(Container.java:4484) 在 java.desktop / java.awt.Container.dispatchEventImpl(Container.java:2305) 在java.desktop / java.awt.Window.dispatchEventImpl(Window.java:2772) 在java.desktop / java.awt.Component.dispatchEvent(Component.java:4798) 在 java.desktop / java.awt.EventQueue.dispatchEventImpl(EventQueue.java:772) 在java.desktop / java.awt.EventQueue.access $ 600(EventQueue.java:97) 在java.desktop / java.awt.EventQueue $ 4.run(EventQueue.java:721)在 java.desktop / java.awt.EventQueue $ 4.run(EventQueue.java:715)在 java.base / java.security.AccessController.doPrivileged(本机方法) 在 java.base / java.security.ProtectionDomain $ JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:87) 在 java.base / java.security.ProtectionDomain $ JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:97) 在java.desktop / java.awt.EventQueue $ 5.run(EventQueue.java:745)在 java.desktop / java.awt.EventQueue $ 5.run(EventQueue.java:743)在 java.base / java.security.AccessController.doPrivileged(本机方法) 在 java.base / java.security.ProtectionDomain $ JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:87) 在 java.desktop / java.awt.EventQueue.dispatchEvent(EventQueue.java:742) 在 java.desktop / java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203) 在 java.desktop / java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124) 在 java.desktop / java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113) 在 java.desktop / java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109) 在 java.desktop / java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101) 在 java.desktop / java.awt.EventDispatchThread.run(EventDispatchThread.java:90) 2018-08-15 22:28:31.367 java [699:58449]查找:未处理的异常 + [LULookupDefinitionModule]中捕获了“ JavaNativeException” _focusTermUsingQueue:handler:]
答案 0 :(得分:0)
我解决了我的问题,我刚刚更改了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();
}
}
}