0

I am trying to register a user (using java, Java swing). I am able to get it to be inserted into the database but its throwing null pointer. I checked the internet for null pointer to handle it and what it said was not initializing a variable and using it. And referring to a null object. But in my code i cant find it. Please help.

public class registerr extends javax.swing.JFrame {

Connection conn;

OracleResultSet rs = null; 
OraclePreparedStatement pst= null;

public registerr() {
    initComponents();
    connect();
}

  public void connect()
 {
  // connection with Oracle DB
  }

Clicking the register button, the value of texfield gets added to Database but then throws null pointer exception

  private void regActionPerformed(java.awt.event.ActionEvent evt) {                                    

   try{

    String fname = FnameTF.getText().trim();
    String lname = LnameTF.getText().trim();
    String uname = UnameTF.getText().trim();
    String pass1 = Pass1TF.getText().trim();
    String pass2 = Pass2TF.getText().trim();

    String sqli = "insert into login"
            + "(fname, lname,uname, pass)"
            + " values ('"+FnameTF.getText()+"','"+LnameTF.getText()+"','"+UnameTF.getText()+"','"+Pass1TF.getText()+"')";
   pst = (OraclePreparedStatement) conn.prepareStatement(sqli);


   pst.executeUpdate(sqli);
   pst.close();

   int count = 0;
   if(uname.equals("") || fname.equals("") || lname.equals("") || Pass1TF.getText().equals("") ||Pass2TF.getText().equals(""))
   {
        System.out.println("Please fill in all the fields");

   }

   else if(pass1.equals(pass2))
   {

   }

   else {
       System.out.println("Passwords do not match");
   }
   while (rs.next())
   {
       count++;

   }
    if(count == 1)
    {
        JOptionPane.showMessageDialog(null, "Please use new value for username");
        System.out.println("Success");

    }

    else 
    {
        System.out.println("Successfully registered");
   JOptionPane.showMessageDialog(null, "Successfully registered!");
    }

    }
    catch(Exception ex)
    {
          ex.printStackTrace(System.out);
    }


    }    

Here is the stack trace

    run:
    Connected
    java.lang.NullPointerException
    at dbms.registerr.regActionPerformed(registerr.java:225)
    at dbms.registerr.access$000(registerr.java:18)
    at dbms.registerr$1.actionPerformed(registerr.java:87)
     at   
   javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
    at 
   javax.swing.AbstractButton$Handler.actionPerformed
  (AbstractButton.java:2348)
   at javax.swing.DefaultButtonModel.fireActionPerformed
  (DefaultButtonModel.java:402)
    at javax.swing.JToggleButton$ToggleButtonModel.setPressed
   (JToggleButton.java:308)
   at javax.swing.plaf.basic.BasicButtonListener.mouseReleased
  (BasicButtonListener.java:252)
  at java.awt.Component.processMouseEvent
  (Component.java:6533)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
   at java.awt.Component.processEvent(Component.java:6298)
   at java.awt.Container.processEvent(Container.java:2236)
   at java.awt.Component.dispatchEventImpl(Component.java:4889)
   at java.awt.Container.dispatchEventImpl(Container.java:2294)
   at java.awt.Component.dispatchEvent(Component.java:4711)
   at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
   at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525)
   at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
   at java.awt.Container.dispatchEventImpl(Container.java:2280)
   at java.awt.Window.dispatchEventImpl(Window.java:2746)
   at java.awt.Component.dispatchEvent(Component.java:4711)
   at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
   at java.awt.EventQueue.access$500(EventQueue.java:97)
   at java.awt.EventQueue$3.run(EventQueue.java:709)
   at java.awt.EventQueue$3.run(EventQueue.java:703)
    at java.security.AccessController.doPrivileged(Native Method)
   at java.security.ProtectionDomain$JavaSecurityAccessImpl.
    doIntersectionPrivilege(ProtectionDomain.java:76)
   at java.security.ProtectionDomain$JavaSecurityAccessImpl.
   doIntersectionPrivilege(ProtectionDomain.java:86)
   at java.awt.EventQueue$4.run(EventQueue.java:731)
   at java.awt.EventQueue$4.run(EventQueue.java:729)
   at java.security.AccessController.doPrivileged(Native Method)
   at java.security.ProtectionDomain$JavaSecurityAccessImpl.
   doIntersectionPrivilege(ProtectionDomain.java:76)
   at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
    at java.awt.EventDispatchThread.pumpOneEventForFilters
   (EventDispatchThread.java:201)
  at java.awt.EventDispatchThread.pumpEventsForFilter
   (EventDispatchThread.java:116)
   at java.awt.EventDispatchThread.pumpEventsForHierarchy              

  (EventDispatchThread.java:105)
   at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
   at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
   at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
    BUILD SUCCESSFUL (total time: 25 seconds)
Andrew Thompson
  • 163,965
  • 36
  • 203
  • 405
nikiii
  • 3
  • 3
  • Please add the full stack trace of the exception: `ex.printStackTrace();` – Arnaud Oct 18 '16 at 15:33
  • which line of your code is registerr.java:225? that's where the stack trace says you're throwing a `NullPointerException`. – Dan O Oct 18 '16 at 15:55
  • 3
    1) See [What is a stack trace, and how can I use it to debug my application errors?](http://stackoverflow.com/q/3988788/418556) & [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/q/218384/418556) 2) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). – Andrew Thompson Oct 18 '16 at 16:16
  • .. 3) Use a logical and consistent form of indenting code lines and blocks. The indentation is intended to make the flow of the code easier to follow! 4) Please learn common Java nomenclature (naming conventions - e.g. `EachWordUpperCaseClass`, `firstWordLowerCaseMethod()`, `firstWordLowerCaseAttribute` unless it is an `UPPER_CASE_CONSTANT`) and use it consistently. – Andrew Thompson Oct 18 '16 at 16:18
  • @Dan O -- the line 225 is this : while (rs.next()) – nikiii Oct 18 '16 at 16:22

1 Answers1

0

rs is null because it's not being set to anything else within the method.

As you're using executeUpdate, then you can use its return value for count:

int count = pst.executeUpdate(sqli);

instead of trying to use rs.

fgb
  • 17,739
  • 2
  • 33
  • 50
  • i did this. `int count = pst.executeUpdate(sqli);` and then `while(rs.next()){count++}` but now i m getting SQLRecoverableException : Closed statement. – nikiii Oct 18 '16 at 16:43
  • @nikiii Try removing the while loop completely. – fgb Oct 18 '16 at 16:47
  • yes i did that too. still getting this `SQLRecoverableException : Closed statement` – nikiii Oct 18 '16 at 16:50
  • i found the issue. I had done `pst.close()` the statement before `int count = pst.executeUpdate(sqli);` The error is gone now. Thank you very much !! – nikiii Oct 18 '16 at 17:11