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

    Default 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:

    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;
                            }
                        });
            }
    What i'm doing is getting the folowing flights:

    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;
        }
    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.
    Can anybody help me a bit to sole this errors on a JTable

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,423
    Rep Power
    20

    Default

    at View.CustomTableCellRenderer.getTableCellRendererC omponent(CustomTableCellRenderer.java:29)
    So where's that class? What's on line 29?

    Also, unless you have very unusual requirements, use a DefaultTableModel instead of messing around with implementing your own concretization of AbstractTableModel.

    db

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

    Default

    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:

    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);
        }
    
    }
    Kind regards,

    André

  4. #4
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    842
    Rep Power
    6

    Default

    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:

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,097
    Rep Power
    20

    Default

    Assuming, based on your OP, that this is line 29:
    Java Code:
    if (value.getClass().isArray()) {
    then value is null.

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

    Default

    Hello Ramya,

    I'm new to debugging this, so where can i put the printStackTrace()?
    Thanks for the other commands, it's a good suggestion.

    Kind regards,

    André

Similar Threads

  1. Replies: 2
    Last Post: 03-12-2011, 04:03 PM
  2. Null pointer exception
    By samuel.roshni in forum Java Servlet
    Replies: 14
    Last Post: 01-22-2011, 02:25 PM
  3. Null pointer Exception
    By peiceonly in forum New To Java
    Replies: 8
    Last Post: 09-05-2010, 06:48 PM
  4. Null Pointer
    By theen3my in forum AWT / Swing
    Replies: 3
    Last Post: 10-03-2009, 02:10 PM
  5. null pointer help
    By mayhewj7 in forum New To Java
    Replies: 5
    Last Post: 02-17-2009, 11:51 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
  •