Results 1 to 10 of 10
  1. #1
    aborgeld is offline Senior Member
    Join Date
    Dec 2010
    Location
    The Hague
    Posts
    114
    Rep Power
    0

    Default Nullpointer & sql exception - Combobox databinding

    Hi Guys,

    I was figuring out how to get data in the combobox, that works, but now i get 2 exceptions and i hope you can help me to let me see where i go wrong.




    View (Form) code:
    Java Code:
    private void setInitWaarden(){
    
        initComponents();
        
        codeComboBox.removeAllItems();
        students = Student.getAllStudenten();
        for (int teller=0;teller<students.length;teller++)
          System.out.println(students[teller].getCode());
        
        codeComboBox.addItem("");
        Student student;
          for (int teller=0;teller<students.length;teller++) {
          student = students[teller];
          System.out.println(student.getCode());
          codeComboBox.addItem(student.getCode());
    
         }

    Methods in class Student
    Java Code:
    public static Student[] getAllStudenten() {
        return getStudentCode ("select * from student order by code");
      }
    
        private static Student[] getStudentCode(String query){
    
        ResultSet r = Database.executeSelectQuery(query);
    
        int aantalStudenten = Database.getAmountResultSet(r);
        Student[] studenten = new Student[aantalStudenten];
    
         try {
            r.next();
             for (int teller=0; teller < aantalStudenten; teller++) {
                Student value = new Student();
                value.code = r.getInt("code");
                value.setAchternaam(r.getString("achternaam"));
                //System.out.println("Student: getStudenten: betaald " + r.getInt("betaald"));
                value.setBetaald(r.getInt("betaald"));
    
                 studenten[teller] = value;
                 r.next();
            }
          }

  2. #2
    aborgeld is offline Senior Member
    Join Date
    Dec 2010
    Location
    The Hague
    Posts
    114
    Rep Power
    0

    Default The error

    Error: he gets his results: like 3, 4, 5. But he has to start with 2:

    See attachtment, database file (2 names are grade out due privacy).

    init:
    Deleting: C:\Users\Andre\Documents\NetBeansProjects\academie \build\built-jar.properties
    deps-jar:
    Updating property file: C:\Users\Andre\Documents\NetBeansProjects\academie \build\built-jar.properties
    compile:
    run:
    Student: getAllStudenten: java.sql.SQLException: After end of result set
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    After end of result set
    S1000
    0
    3
    4
    5
    at view.OverzBetaalStudent.setInitWaarden(OverzBetaal Student.java:36)
    at view.OverzBetaalStudent.<init>(OverzBetaalStudent. java:25)
    at view.Hoofdscherm.studentPaylButtonActionPerformed( Hoofdscherm.java:177)
    at view.Hoofdscherm.access$700(Hoofdscherm.java:14)
    at view.Hoofdscherm$8.actionPerformed(Hoofdscherm.jav a:99)
    at javax.swing.AbstractButton.fireActionPerformed(Abs tractButton.java:1995)
    at javax.swing.AbstractButton$Handler.actionPerformed (AbstractButton.java:2318)
    at javax.swing.DefaultButtonModel.fireActionPerformed (DefaultButtonModel.java:387)
    at javax.swing.DefaultButtonModel.setPressed(DefaultB uttonModel.java:242)
    at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(BasicButtonListener.java:236)
    at java.awt.Component.processMouseEvent(Component.jav a:6267)
    at javax.swing.JComponent.processMouseEvent(JComponen t.java:3267)
    at java.awt.Component.processEvent(Component.java:603 2)
    at java.awt.Container.processEvent(Container.java:204 1)
    at java.awt.Component.dispatchEventImpl(Component.jav a:4630)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2099)
    at java.awt.Component.dispatchEvent(Component.java:44 60)
    at java.awt.LightweightDispatcher.retargetMouseEvent( Container.java:4577)
    at java.awt.LightweightDispatcher.processMouseEvent(C ontainer.java:4238)
    at java.awt.LightweightDispatcher.dispatchEvent(Conta iner.java:4168)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2085)
    at java.awt.Window.dispatchEventImpl(Window.java:2478 )
    at java.awt.Component.dispatchEvent(Component.java:44 60)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java: 599)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:269)
    at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:184)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:174)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:169)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:161)
    at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:122)
    BUILD SUCCESSFUL (total time: 7 minutes 53 seconds)
    Attached Thumbnails Attached Thumbnails Nullpointer &amp; sql exception - Combobox databinding-problem.jpg  

  3. #3
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Location
    Canada
    Posts
    684
    Rep Power
    5

    Default

    try {
    r.next();
    for (int teller=0; teller < aantalStudenten; teller++) {
    Student value = new Student();
    value.code = r.getInt("code");
    value.setAchternaam(r.getString("achternaam"));
    //System.out.println("Student: getStudenten: betaald " + r.getInt("betaald"));
    value.setBetaald(r.getInt("betaald"));

    studenten[teller] = value;
    r.next();
    }
    }
    instead of invoking the r.next() inside the for loop, also us it as a condition
    Java Code:
     try {
            int teller  = 0;
            while (r.next() && teller < aantalStudenten) {
                Student value = new Student();
                value.code = r.getInt("code");
                value.setAchternaam(r.getString("achternaam"));
                //System.out.println("Student: getStudenten: betaald " + r.getInt("betaald"));
                value.setBetaald(r.getInt("betaald"));
    
                 studenten[teller] = value;
                 teller++;
            }
          }

  4. #4
    aborgeld is offline Senior Member
    Join Date
    Dec 2010
    Location
    The Hague
    Posts
    114
    Rep Power
    0

    Default Nullpointer

    Thanks, one down, one to go. I understand the problem with the sql exception now. But how why do i get the null pointer exception with this code in the View(form)?
    Java Code:
    private void setInitWaarden(){
    
        initComponents();
        
        codeComboBox.removeAllItems();
        
        students = Student.getAllStudenten();
        for (int teller=0;teller<students.length;teller++)
            System.out.println(students[teller].getCode());
        
        codeComboBox.addItem("");
        Student student;
          for (int teller=0;teller<students.length;teller++) {
          student = students[teller];
          System.out.println(student.getCode());
          codeComboBox.addItem(student.getCode());
    
         }
        }

    Exception:

    init:
    Deleting: C:\Users\Andre\Documents\NetBeansProjects\academie \build\built-jar.properties
    deps-jar:
    Updating property file: C:\Users\Andre\Documents\NetBeansProjects\academie \build\built-jar.properties
    Compiling 1 source file to C:\Users\Andre\Documents\NetBeansProjects\academie \build\classes
    compile:
    run:
    Student: getStudenten: betaald 1
    Student: getStudenten: betaald 1
    Student: getStudenten: betaald 0
    3
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    4
    5

  5. #5
    aborgeld is offline Senior Member
    Join Date
    Dec 2010
    Location
    The Hague
    Posts
    114
    Rep Power
    0

    Default ToString

    These are the to String methods
    Java Code:
     public String toString() {
    
        String output = "";
        output += getCode();
        output += getCode() + "  " + getAchternaam() + "  " ;
        output += getGeboortedatum() + "  " + getOpleiding() + "  ";
        output += slber.getAchternaam();
        return output;
      }
    
      public String toDescriptiveString() {
        return achternaam + " heeft " + (heeftBetaald ? "wel " : "niet ") + "betaald";
      }

  6. #6
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Location
    Canada
    Posts
    684
    Rep Power
    5

    Default

    what is the line number in this that generates the nill pointer exception?

    my guess is somehow a student in the array of results is null, because the number of students returned from the Database.getAmountResultSet(r); is not giving the same number as we get from the while (r.next()) part. its possible the get amount is reading the values first.

    maybe combine these,

    Java Code:
        ResultSet r = Database.executeSelectQuery(query);
    
        List<Student> list = new ArrayList<Student>()
    
          try {
            while (r.next() && teller < aantalStudenten) {
                Student value = new Student();
                value.code = r.getInt("code");
                value.setAchternaam(r.getString("achternaam"));
                //System.out.println("Student: getStudenten: betaald " + r.getInt("betaald"));
                value.setBetaald(r.getInt("betaald"));
    
                 list.add(value);
                 teller++;
            }
          }
          // if you need this list as an array and the total count, 
          int aantalStudenten = list.size();
          Student[] studenten = list.toArray(new Student[0]);
    where I read results into a list and let it tell me how many there are, instead of calling that separate function anymore.

  7. #7
    aborgeld is offline Senior Member
    Join Date
    Dec 2010
    Location
    The Hague
    Posts
    114
    Rep Power
    0

    Default

    Hello Travishein,

    Thanks for your help!

    The exception comes in the view (form) in line 37

    for (int teller=0;teller<students.length;teller++)
    System.out.println(students[teller].getCode()); //exception start


    I think your right i get another result


    I got the arraylist method working in this way but with no results and then ofcourse no exception

    Java Code:
    private static Student[] getStudentCode(String query){
    
        ResultSet r = Database.executeSelectQuery(query);
        List aList = new ArrayList<Student>();
        
        int aantalStudenten = aList.size();
        Student[] studenten = (Student[]) aList.toArray(new Student[0]);
       
       
     try {
         
         
            int teller  = 0;
            while (r.next() && teller < aantalStudenten) {
                Student value = new Student();
                value.code = r.getInt("code");
                value.setAchternaam(r.getString("achternaam"));
                System.out.println("Student: getStudenten: betaald " + r.getInt("betaald"));
                value.setBetaald(r.getInt("betaald"));
    
                 aList.add(value);
                 teller++;
    
            }
          }
    
         catch (SQLException e) {
          System.out.println("Student: getAllStudenten: " + e.toString());
          System.out.println(e.getMessage() + "\n" + e.getSQLState() + "\n" + e.getErrorCode());
        }
    
    
        return studenten;
    
      }
    Last edited by aborgeld; 01-02-2011 at 06:43 PM.

  8. #8
    aborgeld is offline Senior Member
    Join Date
    Dec 2010
    Location
    The Hague
    Posts
    114
    Rep Power
    0

    Default

    the whole exception:

    init:
    Deleting: C:\Users\Andre\Documents\NetBeansProjects\academie \build\built-jar.properties
    deps-jar:
    Updating property file: C:\Users\Andre\Documents\NetBeansProjects\academie \build\built-jar.properties
    compile:
    run:
    3
    4
    5
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at view.OverzBetaalStudent.setInitWaarden(OverzBetaal Student.java:37)
    at view.OverzBetaalStudent.<init>(OverzBetaalStudent. java:25)
    at view.Hoofdscherm.studentPaylButtonActionPerformed( Hoofdscherm.java:177)
    at view.Hoofdscherm.access$700(Hoofdscherm.java:14)
    at view.Hoofdscherm$8.actionPerformed(Hoofdscherm.jav a:99)
    at javax.swing.AbstractButton.fireActionPerformed(Abs tractButton.java:1995)
    at javax.swing.AbstractButton$Handler.actionPerformed (AbstractButton.java:2318)
    at javax.swing.DefaultButtonModel.fireActionPerformed (DefaultButtonModel.java:387)
    at javax.swing.DefaultButtonModel.setPressed(DefaultB uttonModel.java:242)
    at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(BasicButtonListener.java:236)
    at java.awt.Component.processMouseEvent(Component.jav a:6267)
    at javax.swing.JComponent.processMouseEvent(JComponen t.java:3267)
    at java.awt.Component.processEvent(Component.java:603 2)
    at java.awt.Container.processEvent(Container.java:204 1)
    at java.awt.Component.dispatchEventImpl(Component.jav a:4630)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2099)
    at java.awt.Component.dispatchEvent(Component.java:44 60)
    at java.awt.LightweightDispatcher.retargetMouseEvent( Container.java:4577)
    at java.awt.LightweightDispatcher.processMouseEvent(C ontainer.java:4238)
    at java.awt.LightweightDispatcher.dispatchEvent(Conta iner.java:4168)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2085)
    at java.awt.Window.dispatchEventImpl(Window.java:2478 )
    at java.awt.Component.dispatchEvent(Component.java:44 60)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java: 599)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:269)
    at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:184)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:174)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:169)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:161)
    at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:122)
    BUILD SUCCESSFUL (total time: 4 seconds)

  9. #9
    aborgeld is offline Senior Member
    Join Date
    Dec 2010
    Location
    The Hague
    Posts
    114
    Rep Power
    0

    Default

    when i do this the problem is almost solved:

    int aantalStudenten = Database.getAmountResultSet(r) - 1;

    I get the last 3 records. Only where is my first record
    '3', 'Kezer', '1991-06-19', '1', 'i', '1'
    '4', 'Oosting, '1968-06-06', '1', 'bi', '2'
    '5', 'Bruins', '1970-08-08', '0', 'idm', '4'

    First record in the database i'm not getting:
    '2', 'Borgeld', '1978-05-19', '1', 'i', '1'

    I only get:
    3
    4
    5

  10. #10
    aborgeld is offline Senior Member
    Join Date
    Dec 2010
    Location
    The Hague
    Posts
    114
    Rep Power
    0

    Default

    Issue Solved. Thanks for getting me in the right direction.

    Solution:
    Java Code:
    public static Student[] getAllStudenten() {
        return getStudentCode ("select * from student order by code");
      }
    
    
    
        private static Student[] getStudentCode(String query){
    
        ResultSet r = Database.executeSelectQuery(query);
    
        int aantalStudenten = Database.getAmountResultSet(r);
        Student[] studenten = new Student[aantalStudenten];
    
       try {
       
            int teller  = 0;
    
       {
            while (teller < aantalStudenten) {
                
                Student value = new Student();
                value.code = r.getInt("code");
                value.setAchternaam(r.getString("achternaam"));
                //System.out.println("Student: getStudenten: betaald " + r.getInt("betaald"));
                value.setBetaald(r.getInt("betaald"));
    
                 studenten[teller] = value;
                 teller++;
                 r.next();
             }
           }
          }

Similar Threads

  1. Nullpointer Exception???
    By kipcorn91 in forum AWT / Swing
    Replies: 5
    Last Post: 10-28-2010, 11:19 PM
  2. NullPointer exception
    By bdario1 in forum New To Java
    Replies: 15
    Last Post: 03-17-2010, 04:44 AM
  3. NullPointer.exception in main (arrays)
    By Jana in forum New To Java
    Replies: 5
    Last Post: 02-20-2009, 06:41 PM
  4. nullpointer exception in jsp
    By fiero in forum JavaServer Pages (JSP) and JSTL
    Replies: 6
    Last Post: 11-07-2008, 01:44 PM
  5. NullPointer Exception
    By Preethi in forum New To Java
    Replies: 8
    Last Post: 02-06-2008, 03:40 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •