Results 1 to 6 of 6
- 05-13-2011, 10:03 AM #1
Senior Member
- Join Date
- Dec 2010
- Location
- The Hague
- Posts
- 114
- Rep Power
- 0
how to solve debug this null pointer
Here is the following nullpointer:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at View.CustomTableCellRenderer.getTableCellRendererC omponent(CustomTableCellRenderer.java:29)
at javax.swing.JTable.prepareRenderer(JTable.java:572 9)
at javax.swing.plaf.basic.BasicTableUI.paintCell(Basi cTableUI.java:2069)
at javax.swing.plaf.basic.BasicTableUI.paintCells(Bas icTableUI.java:1971)
at javax.swing.plaf.basic.BasicTableUI.paint(BasicTab leUI.java:1767)
at javax.swing.plaf.ComponentUI.update(ComponentUI.ja va:143)
at javax.swing.JComponent.paintComponent(JComponent.j ava:751)
at javax.swing.JComponent.paint(JComponent.java:1017)
at javax.swing.JComponent.paintChildren(JComponent.ja va:852)
at javax.swing.JComponent.paint(JComponent.java:1026)
at javax.swing.JViewport.paint(JViewport.java:747)
at javax.swing.JComponent.paintChildren(JComponent.ja va:852)
at javax.swing.JComponent.paint(JComponent.java:1026)
at javax.swing.JComponent.paintChildren(JComponent.ja va:852)
at javax.swing.JComponent.paint(JComponent.java:1026)
at javax.swing.JComponent.paintChildren(JComponent.ja va:852)
at javax.swing.JComponent.paint(JComponent.java:1026)
at javax.swing.JLayeredPane.paint(JLayeredPane.java:5 67)
at javax.swing.JComponent.paintChildren(JComponent.ja va:852)
at javax.swing.JComponent.paint(JComponent.java:1026)
at javax.swing.JComponent.paintChildren(JComponent.ja va:852)
at javax.swing.JComponent.paint(JComponent.java:1026)
at javax.swing.JComponent.paintChildren(JComponent.ja va:852)
at javax.swing.JComponent.paint(JComponent.java:1026)
at javax.swing.JLayeredPane.paint(JLayeredPane.java:5 67)
at javax.swing.JComponent.paintToOffscreen(JComponent .java:5112)
at javax.swing.BufferStrategyPaintManager.paint(Buffe rStrategyPaintManager.java:278)
at javax.swing.RepaintManager.paint(RepaintManager.ja va:1220)
at javax.swing.JComponent._paintImmediately(JComponen t.java:5060)
at javax.swing.JComponent.paintImmediately(JComponent .java:4870)
at javax.swing.RepaintManager.paintDirtyRegions(Repai ntManager.java:803)
at javax.swing.RepaintManager.paintDirtyRegions(Repai ntManager.java:714)
at javax.swing.RepaintManager.seqPaintDirtyRegions(Re paintManager.java:694)
at javax.swing.SystemEventQueueUtilities$ComponentWor kRequest.run(SystemEventQueueUtilities.java:125)
at java.awt.event.InvocationEvent.dispatch(Invocation Event.java:209)
at java.awt.EventQueue.dispatchEvent(EventQueue.java: 597)
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)
When we go to the problem we get the following if statement:
What i'm doing is getting the folowing flights:Java Code:if (value.getClass().isArray()) { final Object[] passed = (Object[]) value; //Calculate the height for this row. minHeight = table.getRowHeight(); if(passed.length * minHeight > currHeight) currHeight = passed.length * minHeight; table.setRowHeight(row, currHeight); /* We create the table that will hold the multivalue *fields and that will be embedded in the main table */ return new JTable( new AbstractTableModel() { public int getColumnCount() { return 1; } public int getRowCount() { return passed.length; } public Object getValueAt(int rowIndex, int columnIndex) { return passed[rowIndex]; } @Override public boolean isCellEditable(int row, int col) { return false; } }); }
I have evaluated the code, looked at the date format, but just don't quite know where i have to look to solve this error.Java Code:public static HashMap<Integer, Flight> getFlights() { HashMap<Integer, Flight> flights = new HashMap<Integer, Flight>(); try { PreparedStatement pstmt = con.prepareStatement("Select * from flight;"); ResultSet rs = pstmt.executeQuery(); while (rs.next()) { Flight f = new Flight(); f.setNumber(rs.getInt("flightnumber")); SimpleDateFormat sdf = new SimpleDateFormat(Flight.FlightDateFormat); try { f.setDate(sdf.parse(rs.getString("Date"))); } catch (ParseException ex) { Logger.getLogger(DatabaseConnectie.class.getName()).log(Level.SEVERE, null, ex); continue; } Airport destination = Controller.Controller.Instance().getAirportByName(rs.getString("airportNameDestination")); Airport from = Controller.Controller.Instance().getAirportByName(rs.getString("airportNameFrom")); Staff[] pilots = new Staff[2]; pilots[0] = Controller.Controller.Instance().getStaffById(rs.getInt("pilotNumber")); pilots[1] = Controller.Controller.Instance().getStaffById(rs.getInt("copilotNumber")); Plane plane = Controller.Controller.Instance().getPlaneByNumber(rs.getInt("planeNumber")); if (destination != null && from != null && pilots[0] != null && pilots[1] != null && plane != null) { f.setDestination(destination); f.setFrom(from); f.setPilots(pilots); f.setPlane(plane); } ArrayList<Staff> otherPersonal = new ArrayList<Staff>(); pstmt = con.prepareStatement("Select * from flightstaff where flightNumber = ?;"); pstmt.setInt(1, f.getNumber()); ResultSet rs2 = pstmt.executeQuery(); while (rs2.next()) { Staff s = Controller.Controller.Instance().getStaffById(rs2.getInt("staffNumber")); if (s != null) { otherPersonal.add(s); } } f.setOtherPersonal(otherPersonal); ArrayList<Airport> airports = new ArrayList<Airport>(); pstmt = con.prepareStatement("Select airport from flightstops where flight = ?;"); pstmt.setInt(1, f.getNumber()); ResultSet rs3 = pstmt.executeQuery(); while (rs3.next()) { Airport a = Controller.Controller.Instance().getAirportByName(rs3.getString("airport")); if (a != null) { airports.add(a); } } f.setStops(airports); flights.put(f.getNumber(), f); } } catch (SQLException ex) { Logger.getLogger(DatabaseConnectie.class.getName()).log(Level.SEVERE, null, ex); } return flights; }
Can anybody help me a bit to sole this errors on a JTable
- 05-13-2011, 10:46 AM #2
So where's that class? What's on line 29?at View.CustomTableCellRenderer.getTableCellRendererC omponent(CustomTableCellRenderer.java:29)
Also, unless you have very unusual requirements, use a DefaultTableModel instead of messing around with implementing your own concretization of AbstractTableModel.
db
- 05-13-2011, 11:25 AM #3
Senior Member
- Join Date
- Dec 2010
- Location
- The Hague
- Posts
- 114
- Rep Power
- 0
Hi Darryl,
This is an application written by someone else. There are more choices i'm not proud of. Here is the whole code of the class and thanks for the tip:
Kind regards,Java Code:/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package View; import java.awt.Component; import java.util.ArrayList; import javax.swing.JLabel; import javax.swing.JTable; import javax.swing.table.AbstractTableModel; import javax.swing.table.TableCellRenderer; /** * * @author Jeroen */ public class CustomTableCellRenderer extends JLabel implements TableCellRenderer { private int minHeight = 1; private int currHeight = 1; public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { /* If what we're displaying isn't an array of values we return the normal renderer*/ if (value.getClass().isArray()) { final Object[] passed = (Object[]) value; //Calculate the height for this row. minHeight = table.getRowHeight(); if(passed.length * minHeight > currHeight) currHeight = passed.length * minHeight; table.setRowHeight(row, currHeight); /* We create the table that will hold the multivalue *fields and that will be embedded in the main table */ return new JTable( new AbstractTableModel() { public int getColumnCount() { return 1; } public int getRowCount() { return passed.length; } public Object getValueAt(int rowIndex, int columnIndex) { return passed[rowIndex]; } @Override public boolean isCellEditable(int row, int col) { return false; } }); } if(value instanceof ArrayList){ ArrayList al = (ArrayList)value; final Object[] passed = al.toArray(); //Calculate the height for this row. minHeight = table.getRowHeight(); if(passed.length * minHeight > currHeight) currHeight = passed.length * minHeight; table.setRowHeight(row, currHeight); /* We create the table that will hold the multivalue *fields and that will be embedded in the main table */ return new JTable( new AbstractTableModel() { public int getColumnCount() { return 1; } public int getRowCount() { return passed.length; } public Object getValueAt(int rowIndex, int columnIndex) { return passed[rowIndex]; } @Override public boolean isCellEditable(int row, int col) { return false; } }); } return table.getDefaultRenderer( value.getClass()).getTableCellRendererComponent( table, value, isSelected, hasFocus, row, column); } }
André
- 05-13-2011, 11:30 AM #4
Please format your code and then put printStackTrace() to know the error history.
One more issue in your code is without knowing the ResultSet return value ,you are tring to directlly retrieve rs.get() ...if the value is null or space in database row problem will come.
One more performance issue i noted is for static queries without any arguments u are trying to use PreparedStatements.
You have to write comments above the method and proper debugging statements for easy debugging.Ramya:cool:
- 05-13-2011, 11:43 AM #5
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Assuming, based on your OP, that this is line 29:
then value is null.Java Code:if (value.getClass().isArray()) {
- 05-13-2011, 11:48 AM #6
Senior Member
- Join Date
- Dec 2010
- Location
- The Hague
- Posts
- 114
- Rep Power
- 0
Similar Threads
-
[Debug] Force the pointer point to the right position after inserting new line
By nxhoaf in forum EclipseReplies: 2Last Post: 03-12-2011, 04:03 PM -
Null pointer exception
By samuel.roshni in forum Java ServletReplies: 14Last Post: 01-22-2011, 02:25 PM -
Null pointer Exception
By peiceonly in forum New To JavaReplies: 8Last Post: 09-05-2010, 06:48 PM -
Null Pointer
By theen3my in forum AWT / SwingReplies: 3Last Post: 10-03-2009, 02:10 PM -
null pointer help
By mayhewj7 in forum New To JavaReplies: 5Last Post: 02-17-2009, 11:51 PM


LinkBack URL
About LinkBacks

Bookmarks