Results 1 to 4 of 4
  1. #1
    Manfizy's Avatar
    Manfizy is offline Senior Member
    Join Date
    Nov 2008
    Location
    Germany
    Posts
    193
    Rep Power
    6

    Post java.lang.NullPointerException Error

    Hi people, my program is compiling successfully but i get the NullPointerException error.Any one with an idea of how to solve this coz its stressing me
    Here is my code
    Java Code:
     package myAudioRecords;
    
     import java.awt.event.WindowAdapter;
     import java.awt.event.WindowEvent;
     import java.awt.BorderLayout;
     import java.awt.Component;
     import java.awt.FlowLayout;
     import javax.swing.event.TableModelEvent;
     import javax.swing.event.TableModelListener;
     import javax.swing.table.DefaultTableCellRenderer;
     import javax.swing.table.TableColumn;
     import javax.swing.JButton;
     import javax.swing.JFrame;
     import javax.swing.JLabel;
     import javax.swing.JPanel;
     import javax.swing.JScrollPane;
     import javax.swing.JTable;
     import javax.swing.JTextField;
     import javax.swing.UIManager;
    import javax.swing.table.TableModel;
    
    public class AudioRecord extends javax.swing.JFrame {
        public static final String[] columnNames = {
            "Title","Artist","Album",""
        };
        protected JTable table;
        protected JScrollPane scroller;
        protected AudioRecord tableModel;
    
        /** Creates new form AudioRecord */
        public AudioRecord() {
            initComponent();
        }
        public void initComponent() {
             tableModel =new InteractiveTableModel(columnNames);
             tableModel.addTableModelListener(new AudioRecord.InteractiveTableModelListener());
             table = new JTable();
             table.setModel((TableModel) tableModel);
             table.setSurrendersFocusOnKeystroke(true);
             if (!tableModel.hasEmptyRow()) {
                 tableModel.addEmptyRow();
             }
             scroller = new javax.swing.JScrollPane(table);
             table.setPreferredScrollableViewportSize(new java.awt.Dimension(500, 300));
             TableColumn hidden = table.getColumnModel().getColumn(InteractiveTableModel.HIDDEN_INDEX);
             hidden.setMinWidth(2);
             hidden.setPreferredWidth(2);
             hidden.setMaxWidth(2);
             hidden.setCellRenderer(new InteractiveRenderer(InteractiveTableModel.HIDDEN_INDEX));
    
             setLayout(new BorderLayout());
             add(scroller, BorderLayout.CENTER);
         }
    
         public void highlightLastRow(int row) {
             int lastrow = tableModel.getRowCount();
             if (row == lastrow - 1) {
                 table.setRowSelectionInterval(lastrow - 1, lastrow - 1);
             } else {
                 table.setRowSelectionInterval(row + 1, row + 1);
             }
    
             table.setColumnSelectionInterval(0, 0);
         }
    
        Object getAlbum() {
            throw new UnsupportedOperationException("Not yet implemented");
        }
    
        Object getArtist() {
            throw new UnsupportedOperationException("Not yet implemented");
        }
    
        void setAlbum(String string) {
            throw new UnsupportedOperationException("Not yet implemented");
        }
    
        void setArtist(String string) {
            throw new UnsupportedOperationException("Not yet implemented");
        }
    
        private void addEmptyRow() {
            throw new UnsupportedOperationException("Not yet implemented");
        }
    
        private void addTableModelListener(InteractiveTableModelListener audioRecord) {
            throw new UnsupportedOperationException("Not yet implemented");
        }
    
        private int getRowCount() {
            throw new UnsupportedOperationException("Not yet implemented");
        }
    
        private boolean hasEmptyRow() {
            throw new UnsupportedOperationException("Not yet implemented");
        }
    
         class InteractiveRenderer extends DefaultTableCellRenderer {
             protected int interactiveColumn;
    
             public InteractiveRenderer(int interactiveColumn) {
                 this.interactiveColumn = interactiveColumn;
             }
    
             public Component getTableCellRendererComponent(JTable table,
                Object value, boolean isSelected, boolean hasFocus, int row,
                int column)
             {
                 Component c = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
                 if (column == interactiveColumn && hasFocus) {
                     if ((AudioRecord.this.tableModel.getRowCount() - 1) == row &&
                        !AudioRecord.this.tableModel.hasEmptyRow())
                     {
                         AudioRecord.this.tableModel.addEmptyRow();
                     }
    
                     highlightLastRow(row);
                 }
    
                 return c;
             }
         }
    public class InteractiveTableModelListener implements TableModelListener {
             public void tableChanged(TableModelEvent evt) {
                 if (evt.getType() == TableModelEvent.UPDATE) {
                     int column = evt.getColumn();
                     int row = evt.getFirstRow();
                     System.out.println("row: " + row + " column: " + column);
                     table.setColumnSelectionInterval(column + 1, column + 1);
                     table.setRowSelectionInterval(row, row);
                 }
             }
         }
    
         public static void main(String[] args) {
             try {
                 UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
                 JFrame frame = new JFrame("Interactive Form");
                 frame.addWindowListener(new WindowAdapter() {
                     public void windowClosing(WindowEvent evt) {
                         System.exit(0);
                     }
                 });
                 frame.getContentPane().add(new AudioRecord());
                 frame.pack();
                 frame.setVisible(true);
             } catch (Exception e) {
               e.printStackTrace();
                //System.out.println(e);
             }
         }
        /** This method is called from within the constructor to
         * initialize the form.
         * WARNING: Do NOT modify this code. The content of this method is
         * always regenerated by the Form Editor.
         */
        @SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
        private void initComponents() {
    
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGap(0, 1012, Short.MAX_VALUE)
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGap(0, 761, Short.MAX_VALUE)
            );
    
            pack();
        }// </editor-fold>                        
    
    }
    your help will be highly appreciated.

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  3. #3
    Manfizy's Avatar
    Manfizy is offline Senior Member
    Join Date
    Nov 2008
    Location
    Germany
    Posts
    193
    Rep Power
    6

    Default

    Quote Originally Posted by Eranga View Post
    Can you send the full error message here to see at least.
    ok,the error message is like below

    init:
    deps-jar:
    Compiling 1 source file to C:\Data\MUNYA00M\Practice\AdioRecords\build\classe s
    compile:
    run:
    java.lang.NullPointerException
    at myAudioRecords.AudioRecord.initComponent(AudioReco rd.java:46)
    at myAudioRecords.AudioRecord.<init>(AudioRecord.java :42)
    at myAudioRecords.AudioRecord.main(AudioRecord.java:1 54)
    BUILD SUCCESSFUL (total time: 1 second)

  4. #4
    NRitH is offline Senior Member
    Join Date
    Feb 2011
    Posts
    118
    Rep Power
    0

    Default

    If I'm counting those lines right, your TableColumn hidden is what's null, because InteractiveTableModel.HIDDEN_INDEX must not be a valid column index.

Similar Threads

  1. Replies: 4
    Last Post: 10-09-2008, 07:08 PM
  2. java.lang.NullPointerException
    By stevemcc in forum AWT / Swing
    Replies: 2
    Last Post: 02-08-2008, 10:01 AM
  3. java.lang.NullPointerException
    By ravian in forum New To Java
    Replies: 1
    Last Post: 01-13-2008, 08:39 PM
  4. java.lang.NullPointerException
    By Felissa in forum Advanced Java
    Replies: 1
    Last Post: 07-05-2007, 07:02 AM
  5. Replies: 2
    Last Post: 07-02-2007, 03:29 AM

Posting Permissions

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