0

I am building a Student Manager and learned everything from YouTube. Now I am stuck in this. The code is given below and the error is given below too.

This is code:

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();
    }
}
}

This is the error I am getting:

java.lang.NullPointerException at frames.RegisterStart$1.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.access$600(EventQueue.java:97) at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:721) at java.desktop/java.awt.EventQueue$4.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$2(RegisterStart.java:171) at frames.RegisterStart$2.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.access$600(EventQueue.java:97) at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:721) at java.desktop/java.awt.EventQueue$4.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$5.run(EventQueue.java:745) at java.desktop/java.awt.EventQueue$5.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:]

Uwe Allner
  • 3,399
  • 9
  • 35
  • 49
Rajat Patil
  • 1
  • 1
  • 4
  • 1
    Welcome to StackOverflow. Before posting code in your answer, make sure it's the minimum amount of code needed to reproduce your error. This is known on StackOverflow as a Minimal-Complete-Verifiable example. See here for more info- https://stackoverflow.com/help/mcve – Richie Thomas Aug 15 '18 at 19:21
  • 1
    `java.lang.NullPointerException at frames.RegisterStart$1.run(RegisterStart.java:58)` - the trace tells you line 58 is the problem. We can't help because we have no ideas what line 58 is. Find line 58 and determine with variable on the line is null and then fix the problem. The real question is why would you write so much code before doing any testing??? Write a few lines of code for your GUI and test that the components display properly. Then add extra logic for the SQL and test that. When it stops working you know what you just changed and it is easier to fix the problem. – camickr Aug 15 '18 at 19:57
  • 1
    You also have so many other basic Swing problems that you should start over from the beginning. But first of all read the [Swing Tutorial]() for Swing basics. A few pointers: 1) you should not be using setBounds() on components. You SHOULD be using layout managers. 2) You should not be using "static" variables and methods. 3) method names should NOT start with an upper case character. The tutorial contains working code you can download and test. Then change the working code to display your components. – camickr Aug 15 '18 at 20:00
  • Without the line numbers of your source code, it is difficult to follow your stack trace. Can you submit your RegisterStart.java file entirely? – Clint Munden Aug 15 '18 at 20:04

1 Answers1

0

I solved my problem. I just changed Properties prop; to Properties prop = new Properties(); Here is what it looks like

    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();
        }
    }
}
Michael Gantman
  • 7,315
  • 2
  • 19
  • 36
Rajat Patil
  • 1
  • 1
  • 4