468,512 Members | 1,449 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,512 developers. It's quick & easy.

getting err java.lang.NullPointerException

i dont know y im getting this err java.lang.NullPointerException

if u find out something here is my mail id <deleted>

Expand|Select|Wrap|Line Numbers
  1. package login;
  2.  
  3. import java.awt.EventQueue;
  4. import java.swing.*;
  5. import javax.swing.*;
  6. import java.awt.BorderLayout;
  7. import java.awt.GridBagLayout;
  8. import java.awt.event.ActionListener;
  9. import java.awt.event.ActionEvent;
  10. import java.sql.*;
  11.  
  12. public class Remake extends JFrame {
  13.  
  14.  
  15.  
  16.  
  17.     private JFrame frame;
  18.     private JTextField txtfname;
  19.     private JTextField txtadd;
  20.     private JTextField txtemail;
  21.     private JTextField txtuser;
  22.     private JTextField txtlname;
  23.     private JPasswordField txtpass;
  24.     private JTextField txtphone;
  25.     private JComboBox cmbgender;
  26.  
  27.     /**
  28.      * Launch the application.
  29.      */
  30.     public static void main(String[] args) {
  31.         EventQueue.invokeLater(new Runnable() {
  32.             public void run() {
  33.                 try {
  34.                     Remake window = new Remake();
  35.                     window.frame.setVisible(true);
  36.                 } catch (Exception e) {
  37.                     e.printStackTrace();
  38.                 }
  39.             }
  40.         });
  41.     }
  42.  
  43.     /**
  44.      * Create the application.
  45.      */
  46.     public Remake() {
  47.         initialize();
  48.     }
  49.  
  50.     /**
  51.      * Initialize the contents of the frame.
  52.      */
  53.     private void initialize() {
  54.         frame = new JFrame();
  55.         frame.setBounds(100, 100, 743, 460);
  56.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  57.         frame.getContentPane().setLayout(null);
  58.  
  59.         JLabel lblNewLabel = new JLabel("pd");
  60.         lblNewLabel.setBounds(226, 34, 258, 54);
  61.         frame.getContentPane().add(lblNewLabel);
  62.  
  63.         JLabel lblNewLabel_1 = new JLabel("First name");
  64.         lblNewLabel_1.setBounds(76, 112, 46, 14);
  65.         frame.getContentPane().add(lblNewLabel_1);
  66.  
  67.         JLabel lblNewLabel_2 = new JLabel("addr");
  68.         lblNewLabel_2.setBounds(76, 162, 46, 14);
  69.         frame.getContentPane().add(lblNewLabel_2);
  70.  
  71.         JLabel lblNewLabel_3 = new JLabel("email");
  72.         lblNewLabel_3.setBounds(76, 222, 46, 14);
  73.         frame.getContentPane().add(lblNewLabel_3);
  74.  
  75.         JLabel lblNewLabel_4 = new JLabel("username");
  76.         lblNewLabel_4.setBounds(76, 278, 46, 14);
  77.         frame.getContentPane().add(lblNewLabel_4);
  78.  
  79.         JLabel lblNewLabel_5 = new JLabel("last name");
  80.         lblNewLabel_5.setBounds(448, 112, 46, 14);
  81.         frame.getContentPane().add(lblNewLabel_5);
  82.  
  83.         JLabel lblNewLabel_6 = new JLabel("gender");
  84.         lblNewLabel_6.setBounds(448, 162, 46, 14);
  85.         frame.getContentPane().add(lblNewLabel_6);
  86.  
  87.         JLabel lblNewLabel_7 = new JLabel("phone");
  88.         lblNewLabel_7.setBounds(448, 222, 46, 14);
  89.         frame.getContentPane().add(lblNewLabel_7);
  90.  
  91.         JLabel lblNewLabel_8 = new JLabel("passwrd");
  92.         lblNewLabel_8.setBounds(448, 278, 46, 14);
  93.         frame.getContentPane().add(lblNewLabel_8);
  94.  
  95.         JButton btnNewButton = new JButton("New button");
  96.         btnNewButton.addActionListener(new ActionListener() {
  97.             public void actionPerformed(ActionEvent e) {
  98.  
  99.                 try {
  100.  
  101.                     String query="INSERT INTO `redu`(`fname`, `lname`, `addr`, `gender`, `email`, `phone`, `username`, `password`) VALUES (?,?,?,?,?,?,?,?)";
  102.                     Connection con= DriverManager.getConnection("jdbc:mysql://localhost:3306/remake","root","");
  103.                     PreparedStatement pst=con.prepareStatement(query);
  104.  
  105.                     pst.setString(1, txtfname.getText());
  106.                     pst.setString(2, txtlname.getText());
  107.                     pst.setString(3, txtadd.getText());
  108.                     pst.setString(4, cmbgender.getSelectedItem().toString());
  109.                     pst.setString(5, txtemail.getText());
  110.                     pst.setString(6, txtphone.getText());
  111.                     pst.setString(7, txtuser.getText());
  112.                     pst.setString(8, txtpass.getText());
  113.                     pst.executeUpdate();
  114.  
  115.                     JOptionPane.showMessageDialog(null, "register successfully");
  116.                 }
  117.                 catch(Exception ex){
  118.                     System.out.println(ex);
  119.                 }
  120.  
  121.  
  122.  
  123.  
  124.             }
  125.         });
  126.         btnNewButton.setBounds(326, 338, 89, 23);
  127.         frame.getContentPane().add(btnNewButton);
  128.  
  129.         txtfname = new JTextField();
  130.         txtfname.setBounds(208, 109, 86, 20);
  131.         frame.getContentPane().add(txtfname);
  132.         txtfname.setColumns(10);
  133.  
  134.         txtadd = new JTextField();
  135.         txtadd.setBounds(208, 159, 86, 20);
  136.         frame.getContentPane().add(txtadd);
  137.         txtadd.setColumns(10);
  138.  
  139.         txtemail = new JTextField();
  140.         txtemail.setBounds(208, 219, 86, 20);
  141.         frame.getContentPane().add(txtemail);
  142.         txtemail.setColumns(10);
  143.  
  144.         txtuser = new JTextField();
  145.         txtuser.setBounds(208, 275, 86, 20);
  146.         frame.getContentPane().add(txtuser);
  147.         txtuser.setColumns(10);
  148.  
  149.         txtlname = new JTextField();
  150.         txtlname.setBounds(564, 109, 86, 20);
  151.         frame.getContentPane().add(txtlname);
  152.         txtlname.setColumns(10);
  153.  
  154.         txtpass = new JPasswordField();
  155.         txtpass.setBounds(564, 275, 86, 20);
  156.         frame.getContentPane().add(txtpass);
  157.  
  158.         txtphone = new JTextField();
  159.         txtphone.setBounds(564, 219, 86, 20);
  160.         frame.getContentPane().add(txtphone);
  161.         txtphone.setColumns(10);
  162.  
  163.         JComboBox cmbgender = new JComboBox();
  164.         cmbgender.setModel(new DefaultComboBoxModel(new String[] {"1", "2", "3"}));
  165.         cmbgender.setBounds(564, 158, 86, 22);
  166.         frame.getContentPane().add(cmbgender);
  167.     }
  168.  
  169. }
May 13 '21 #1
1 2639
Banfa
9,056 Expert Mod 8TB
If you are getting a java.lang.NullPointerException then you should be getting an entire stack trace giving you near enough the line of code that is going wrong. Please post that stack trace.
4 Weeks Ago #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

reply views Thread by gezkk | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.