Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Using method in Eclipse IDE

  1. #1
    Junior Member
    Join Date
    Sep 2014
    Posts
    4
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Using method in Eclipse IDE

    Hi, I would like to seek for help because right now I am very confused about why my code is not working..

    The function of my code will add records in my database (MySQL), Swing components of my frame..

    btnUserAdd = jButton
    txtUname, txtPword, txtLname, txtFname, txtMname, txtEmpNo = jTextField
    cbAccType = jComboBox
    EmpNo, Uname, Pword, Lname, Fname, Mname, AccType = MySQL fields

    I created a method to be called when btnUserAdd is clicked. here is the code for the method:

    public void UserAdd(){

    String sql = "SELECT * FROM User WHERE Uname = ? and EmpNo = ?";

    try {

    pst = conn.prepareStatement(sql);
    pst.setString(1, txtUname.getText().toString());
    pst.setString(2, txtEmpNo.getText().toString());
    rs = pst.executeQuery();

    if (rs.next()){

    JOptionPane.showMessageDialog(null, "Record Exists! Please Try Again.");
    txtUname.requestFocus();
    cbAccType.setSelectedItem("Operator");
    UserClear();

    }
    else {

    try {

    String sql1 = "INSERT INTO user (EmpNo, Uname, Pword, Lname, Fname, Mname, AccType)" + "VALUES (?, ?, ?, ?, ?, ?, ?)";
    String cb1 = cbAccType.getSelectedItem().toString();

    pst = conn.prepareStatement(sql1);
    pst.setString(1, txtEmpNo.getText());
    pst.setString(2, txtUname.getText());
    pst.setString(3, txtPword.getText());
    pst.setString(4, txtLname.getText());
    pst.setString(5, txtFname.getText());
    pst.setString(6, txtMname.getText());
    pst.setString(7, cb1);
    pst.execute();

    JOptionPane.showMessageDialog(null, "Record Added!");

    }
    catch (Exception e){

    JOptionPane.showMessageDialog(null, e);

    }

    }

    }
    catch (Exception e){

    JOptionPane.showMessageDialog(null, e);

    }

    }

    then here is the code inside the btnUserAdd:

    btnUserAdd.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent evt) {

    if (txtUname.getText().trim().equals("")||txtPword.ge tText().trim().equals("")||txtEmpNo.getText().trim ().equals("")||txtLname.getText().trim().equals("" )){

    JOptionPane.showMessageDialog(null, "Please Enter a Username, Password and Employee No, Try Again!");

    if (txtUname.getText().trim().equals("")){

    txtUname.requestFocus();

    }
    else if (txtPword.getText().trim().equals("")){

    txtPword.requestFocus();

    }
    else if (txtLname.getText().trim().equals("")){

    txtLname.requestFocus();

    }
    else if (txtEmpNo.getText().trim().equals("")){

    txtEmpNo.requestFocus();

    }

    }
    else{

    UserAdd();


    }

    }
    });

    the error is that it does not add the record it will display JAVA.LANG.NULLPOINTEREXCEPTION...

    please help me with my problem..


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Using method in Eclipse IDE

    Welcome to the forum! Please read this topic to learn how to post code in code or highlight tags and other useful info for new members.

    Please post your code correctly using code or highlight tags per the above link and post the complete error message with the stack trace.

    Posting multiple threads on the same topic is not to your benefit. Your duplicate threads have been deleted.

  3. The Following User Says Thank You to GregBrannon For This Useful Post:

    abuk10 (September 7th, 2014)

  4. #3
    Junior Member
    Join Date
    Sep 2014
    Posts
    4
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Using method in Eclipse IDE

    THANK YOU FOR THE PROPER GUIDELINES..


    Hi, I would like to seek for help because right now I am very confused about why my code is not working..

    The function of my code will add records in my database (MySQL), Swing components of my frame..

    btnUserAdd = jButton
    txtUname, txtPword, txtLname, txtFname, txtMname, txtEmpNo = jTextField
    cbAccType = jComboBox
    EmpNo, Uname, Pword, Lname, Fname, Mname, AccType = MySQL fields

    I created a method to be called when btnUserAdd is clicked. here is the code for the method:

    public void UserAdd(){
     
    String sql = "SELECT * FROM User WHERE Uname = ? and EmpNo = ?";
     
         try {
     
              pst = conn.prepareStatement(sql);
              pst.setString(1, txtUname.getText().toString());
              pst.setString(2, txtEmpNo.getText().toString());
              rs = pst.executeQuery();
     
                   if (rs.next()){
     
                           JOptionPane.showMessageDialog(null, "Record Exists! Please Try Again.");
                           txtUname.requestFocus();
                           cbAccType.setSelectedItem("Operator");
                           UserClear();
     
                    }
                    else {
     
                            try {
     
                                   String sql1 = "INSERT INTO user (EmpNo, Uname, Pword, Lname, Fname, Mname, AccType)" + "VALUES (?, ?, ?, ?, ?, ?, ?)";
                                   String cb1 = cbAccType.getSelectedItem().toString();
     
                                   pst = conn.prepareStatement(sql1);
                                   pst.setString(1, txtEmpNo.getText());
                                   pst.setString(2, txtUname.getText());
                                   pst.setString(3, txtPword.getText());
                                   pst.setString(4, txtLname.getText());
                                   pst.setString(5, txtFname.getText());
                                   pst.setString(6, txtMname.getText());
                                   pst.setString(7, cb1);
                                   pst.execute();
     
                                   JOptionPane.showMessageDialog(null, "Record Added!");
     
                         }
                         catch (Exception e){
     
                                      JOptionPane.showMessageDialog(null, e);
     
                          }
     
                     }
     
          }
          catch (Exception e){
     
                      JOptionPane.showMessageDialog(null, e);
     
           }
     
    }

    then here is the code inside the btnUserAdd:

     
    btnUserAdd.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent evt) {
     
             if (txtUname.getText().trim().equals("")||txtPword.ge tText().trim().equals("")||txtEmpNo.getText().trim ().equals("")||txtLname.getText().trim().equals("" )){
     
                    JOptionPane.showMessageDialog(null, "Please Enter a Username, Password and Employee No, Try Again!");
     
                   if (txtUname.getText().trim().equals("")){
     
                          txtUname.requestFocus();
     
                   }
                  else if (txtPword.getText().trim().equals("")){
     
                          txtPword.requestFocus();
     
                   }
                  else if (txtLname.getText().trim().equals("")){
     
                          txtLname.requestFocus();
     
                   }
                  else if (txtEmpNo.getText().trim().equals("")){
     
                          txtEmpNo.requestFocus();
     
                   }
     
              }
              else{
     
                    UserAdd();	 //method for add
     
     
               }
     
    }
    });

    the problem is that it does not add the record it will display JAVA.LANG.NULLPOINTEREXCEPTION...

    please help me with my problem..

  5. #4
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: Using method in Eclipse IDE

    Find the variable that is the nullpointer and then backtrack to find out why it is not properly initialized. Then decide how you would want to initialize it with a value properly.

  6. The Following User Says Thank You to Cornix For This Useful Post:

    abuk10 (September 7th, 2014)

  7. #5
    Junior Member
    Join Date
    Sep 2014
    Posts
    4
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Using method in Eclipse IDE

    Quote Originally Posted by Cornix View Post
    Find the variable that is the nullpointer and then backtrack to find out why it is not properly initialized. Then decide how you would want to initialize it with a value properly.
    the catch is I call the method right? when I copy the code inside the method and paste it inside the actionperformed instead of calling the method, IT WORKS!! how is that working? thats my problem..

  8. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Using method in Eclipse IDE

    how is that working?
    There can be many reasons. One for an NPE would be when the code is in one place a variable is still not initialized and when it is in another place, the variable has a valid value.

    You have to find what caused the error and fix it.
    If you don't understand my answer, don't ignore it, ask a question.

  9. The Following User Says Thank You to Norm For This Useful Post:

    abuk10 (September 7th, 2014)

  10. #7
    Junior Member
    Join Date
    Sep 2014
    Posts
    4
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Using method in Eclipse IDE

    I will post if I fix this problem thanks by the way

Similar Threads

  1. How to call methods from another class using Eclipse IDE? Please help!
    By thedarkhitman in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 3rd, 2013, 06:28 PM
  2. IDE Eclipse Exporting Problem
    By DanielJamesCollier in forum Java IDEs
    Replies: 2
    Last Post: January 2nd, 2013, 06:26 PM
  3. How to Compile JSP in Eclipse (or other IDE)?
    By KevinWorkman in forum JavaServer Pages: JSP & JSTL
    Replies: 8
    Last Post: October 31st, 2011, 12:37 PM
  4. Porting from Processing IDE to Eclipse
    By kafka82 in forum What's Wrong With My Code?
    Replies: 12
    Last Post: June 4th, 2011, 10:18 AM