Results 1 to 10 of 10
- 01-01-2011, 08:46 PM #1
Senior Member
- Join Date
- Dec 2010
- Location
- The Hague
- Posts
- 114
- Rep Power
- 0
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(); } }
- 01-01-2011, 08:50 PM #2
Senior Member
- Join Date
- Dec 2010
- Location
- The Hague
- Posts
- 114
- Rep Power
- 0
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)
- 01-01-2011, 11:24 PM #3
instead of invoking the r.next() inside the for loop, also us it as a conditiontry {
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();
}
}
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++; } }
- 01-02-2011, 11:32 AM #4
Senior Member
- Join Date
- Dec 2010
- Location
- The Hague
- Posts
- 114
- Rep Power
- 0
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
- 01-02-2011, 11:34 AM #5
Senior Member
- Join Date
- Dec 2010
- Location
- The Hague
- Posts
- 114
- Rep Power
- 0
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"; }
- 01-02-2011, 01:48 PM #6
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,
where I read results into a list and let it tell me how many there are, instead of calling that separate function anymore.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]);
- 01-02-2011, 06:38 PM #7
Senior Member
- Join Date
- Dec 2010
- Location
- The Hague
- Posts
- 114
- Rep Power
- 0
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.
- 01-02-2011, 07:14 PM #8
Senior Member
- Join Date
- Dec 2010
- Location
- The Hague
- Posts
- 114
- Rep Power
- 0
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)
- 01-02-2011, 07:25 PM #9
Senior Member
- Join Date
- Dec 2010
- Location
- The Hague
- Posts
- 114
- Rep Power
- 0
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
- 01-03-2011, 01:06 PM #10
Senior Member
- Join Date
- Dec 2010
- Location
- The Hague
- Posts
- 114
- Rep Power
- 0
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
-
Nullpointer Exception???
By kipcorn91 in forum AWT / SwingReplies: 5Last Post: 10-28-2010, 11:19 PM -
NullPointer exception
By bdario1 in forum New To JavaReplies: 15Last Post: 03-17-2010, 04:44 AM -
NullPointer.exception in main (arrays)
By Jana in forum New To JavaReplies: 5Last Post: 02-20-2009, 06:41 PM -
nullpointer exception in jsp
By fiero in forum JavaServer Pages (JSP) and JSTLReplies: 6Last Post: 11-07-2008, 01:44 PM -
NullPointer Exception
By Preethi in forum New To JavaReplies: 8Last Post: 02-06-2008, 03:40 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks