-1

Okay, so I'm having a NullPointerException in this block. I really have no idea why would it throw that exception because I first import csv file into that table, and than read from that table and import in my database. Any help would be greatly appreciated because I need it for my project :)

    private void SaveData() {

            try {
                PreparedStatement pstmt = null;
                Connection conn = null;
                Class.forName("com.mysql.jdbc.Driver");
                conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/Program", "username", "password");
                pstmt = (PreparedStatement) conn.prepareStatement("insert into `novi` values( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
                for (int i = 0; i < table.getRowCount(); i++) {
                    pstmt.setString(1, table.getValueAt(i, 0).toString());
                    pstmt.setString(2, table.getValueAt(i, 1).toString());
                    pstmt.setString(3, table.getValueAt(i, 2).toString());
                    pstmt.setString(4, table.getValueAt(i, 3).toString());
                    pstmt.setString(5, table.getValueAt(i, 4).toString());
                    pstmt.setString(6, table.getValueAt(i, 5).toString());
                    pstmt.setString(7, table.getValueAt(i, 6).toString());
                    pstmt.setString(8, table.getValueAt(i, 7).toString());
                    pstmt.setString(9, table.getValueAt(i, 8).toString());
                    pstmt.setString(10, table.getValueAt(i, 9).toString());
                    pstmt.setString(11, table.getValueAt(i, 10).toString());
                    pstmt.setString(12, table.getValueAt(i, 11).toString());
                    pstmt.setString(13, table.getValueAt(i, 12).toString());
                    pstmt.setString(14, table.getValueAt(i, 13).toString());

                }
                pstmt.executeUpdate();

            } catch (Exception e) {
                JOptionPane.showMessageDialog(null, e);
            }
}

And there is full stacktrace:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at program.MyForm.SaveData(MyForm.java:205)
at program.MyForm.access$000(MyForm.java:39)
at program.MyForm$2.actionPerformed(MyForm.java:116)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6533)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
at java.awt.Component.processEvent(Component.java:6298)
at java.awt.Container.processEvent(Container.java:2236)
at java.awt.Component.dispatchEventImpl(Component.java:4889)
at java.awt.Container.dispatchEventImpl(Container.java:2294)
at java.awt.Component.dispatchEvent(Component.java:4711)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
at java.awt.Container.dispatchEventImpl(Container.java:2280)
at java.awt.Window.dispatchEventImpl(Window.java:2746)
at java.awt.Component.dispatchEvent(Component.java:4711)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:90)
at java.awt.EventQueue$4.run(EventQueue.java:731)
at java.awt.EventQueue$4.run(EventQueue.java:729)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

I would like to share the full code with you guys:

public class MyForm extends javax.swing.JFrame {

    private JTable table;

    public MyForm() {
        // Create Form Frame
        super("Ananas");
        setSize(668, 345);
        setLocation(500, 280);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        getContentPane().setLayout(null);

        // Label Result
        final JLabel lblResult = new JLabel("Result", JLabel.CENTER);
        lblResult.setBounds(150, 22, 370, 14);
        getContentPane().add(lblResult);

        // Data Source
        final CustomModel model = new CustomModel();

        // Table
        JTable table = new JTable(model);
        getContentPane().add(table);

        // ScrollPane
        JScrollPane scroll = new JScrollPane(table);
        scroll.setBounds(84, 98, 506, 79);
        getContentPane().add(scroll);

        // Create Button Open JFileChooser
        JButton btnButton = new JButton("Izaberite fajl");
        btnButton.setBounds(268, 47, 135, 23);
        btnButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {

                JFileChooser fileopen = new JFileChooser();
                FileFilter filter = new FileNameExtensionFilter(
                        "Text/CSV file", "txt", "csv");
                fileopen.addChoosableFileFilter(filter);

                int ret = fileopen.showDialog(null, "Izaberite fajl");

                if (ret == JFileChooser.APPROVE_OPTION) {

                    // Read Text file
                    File file = fileopen.getSelectedFile();

                    try {
                        BufferedReader br = new BufferedReader(new FileReader(
                                file));
                        String line;
                        while ((line = br.readLine()) != null) {
                            String[] arr = line.split(",");
                            model.addRow(arr[0], arr[1], arr[2], arr[3], arr[4], arr[5], arr[6], arr[7], arr[8], arr[9],
                                    arr[10], arr[11], arr[12], arr[13]);
                        }
                        br.close();
                    } catch (IOException ex) {
                        // TODO Auto-generated catch block
                        ex.printStackTrace();
                    }

                    lblResult.setText(fileopen.getSelectedFile().toString());
                }

            }
        });
        getContentPane().add(btnButton);

        // Button Save
        JButton btnSave = new JButton("Save");
        btnSave.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                SaveData(); // save Data
            }
        });
        btnSave.setBounds(292, 228, 89, 23);
        getContentPane().add(btnSave);

    }

    private void SaveData() {
        try {
            PreparedStatement pstmt = null;
            Connection conn = null;
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection("jdbc:mysql://192.168.1.4:3306/Program", "aleksa", "al3ksa123");
            pstmt = (PreparedStatement) conn.prepareStatement("insert into `novi` values( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
            for (int i = 0; i < table.getRowCount(); i++) {
                pstmt.setString(1, table.getValueAt(i, 0).toString());
                pstmt.setString(2, table.getValueAt(i, 1).toString());
                pstmt.setString(3, table.getValueAt(i, 2).toString());
                pstmt.setString(4, table.getValueAt(i, 3).toString());
                pstmt.setString(5, table.getValueAt(i, 4).toString());
                pstmt.setString(6, table.getValueAt(i, 5).toString());
                pstmt.setString(7, table.getValueAt(i, 6).toString());
                pstmt.setString(8, table.getValueAt(i, 7).toString());
                pstmt.setString(9, table.getValueAt(i, 8).toString());
                pstmt.setString(10, table.getValueAt(i, 9).toString());
                pstmt.setString(11, table.getValueAt(i, 10).toString());
                pstmt.setString(12, table.getValueAt(i, 11).toString());
                pstmt.setString(13, table.getValueAt(i, 12).toString());
                pstmt.setString(14, table.getValueAt(i, 13).toString());

            }
            pstmt.executeUpdate();

        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, e);
        }
    }

    class CustomModel extends AbstractTableModel {

        List<Customer> dataRow;
        String[] columnHeader = {"Diler", "Poddiler", "ID",
            "Imeiprezime", "Brojug", "Pretplata", "Uredjaj", "Paket", "Cena", "Valuta", "Nacin",
            "Datum", "Datum uplate", "novID"};
        int id = 0;

        public CustomModel() {
            dataRow = new ArrayList<Customer>();
        }

        @Override
        public String getColumnName(int column) {
            return columnHeader[column];
        }

        @Override
        public int getColumnCount() {
            return columnHeader.length;
        }

        @Override
        public int getRowCount() {
            return dataRow.size();
        }

        @Override
        public Object getValueAt(int rowIndex, int columnIndex) {
            Customer customer = dataRow.get(rowIndex);
            switch (columnIndex) {
                case 0:
                    return customer.getDiler();
                case 1:
                    return customer.getPoddiler();
                case 2:
                    return customer.getID();
                case 3:
                    return customer.getImeiprezime();
                case 4:
                    return customer.getBrojug();
                case 5:
                    return customer.getPretplata();
                case 6:
                    return customer.getUredjaj();
                case 7:
                    return customer.getPaket();
                case 8:
                    return customer.getCena();
                case 9:
                    return customer.getValuta();
                case 10:
                    return customer.getNacin();
                case 11:
                    return customer.getDatum();
                case 12:
                    return customer.getDatumuplate();
                case 13:
                    return customer.getnovID();
                default:
                    return null;
            }
        }

        public void addRow(String Diler, String Poddiler, String ID,
                String Imeiprezime, String Brojug, String Pretplata, String Uredjaj,
                String Paket, String Cena, String Valuta, String Nacin, String Datum,
                String Datumuplate, String novID) {
            dataRow.add(new Customer(Diler, Poddiler, ID, Imeiprezime,
                    Brojug, Pretplata, Uredjaj, Paket, Cena, Valuta, Nacin, Datum, Datumuplate,
                    novID));
            int rowCount = getRowCount();
            fireTableRowsInserted(rowCount, rowCount);
        }

    }

    class Customer {

        private String Diler;
        private String Poddiler;
        private String ID;
        private String Imeiprezime;
        private String Brojug;
        private String Pretplata;
        private String Uredjaj;
        private String Paket;
        private String Cena;
        private String Valuta;
        private String Nacin;
        private String Datum;
        private String Datumuplate;
        private String novID;

        public Customer(String sDiler, String sPoddiler, String sID,
                String sImeiprezime, String sBrojug, String sPretplata, String sUredjaj,
                String sPaket, String sCena, String sValuta, String sNacin, String sDatum,
                String sDatumuplate, String snovID) {
            this.Diler = sDiler;
            this.Poddiler = sPoddiler;
            this.ID = sID;
            this.Imeiprezime = sImeiprezime;
            this.Brojug = sBrojug;
            this.Pretplata = sPretplata;
            this.Uredjaj = sUredjaj;
            this.Paket = sPaket;
            this.Cena = sCena;
            this.Valuta = sValuta;
            this.Nacin = sNacin;
            this.Datum = sDatum;
            this.Datumuplate = sDatumuplate;
            this.novID = snovID;
        }

        public String getDiler() {
            return Diler;
        }

        public void setDiler(String tDiler) {
            this.Diler = tDiler;
        }

        public String getPoddiler() {
            return Poddiler;
        }

        public void setPoddiler(String tPoddiler) {
            this.Poddiler = tPoddiler;
        }

        public String getID() {
            return ID;
        }

        public void setID(String tID) {
            this.ID = tID;
        }

        public String getImeiprezime() {
            return Imeiprezime;
        }

        public void setImeiprezime(String tImeiprezime) {
            this.Imeiprezime = tImeiprezime;
        }

        public String getBrojug() {
            return Brojug;
        }

        public void setBrojug(String tBrojug) {
            this.Brojug = tBrojug;
        }

        public String getPretplata() {
            return Pretplata;
        }

        public void setPretplata(String tPretplata) {
            this.Pretplata = tPretplata;
        }

        public String getUredjaj() {
            return Uredjaj;
        }

        public void setUredjaj(String tUredjaj) {
            this.Uredjaj = tUredjaj;
        }

        public String getPaket() {
            return Paket;
        }

        public void setPaket(String tPaket) {
            this.Paket = tPaket;
        }

        public String getCena() {
            return Cena;
        }

        public void setCena(String tCena) {
            this.Cena = tCena;
        }

        public String getValuta() {
            return Valuta;
        }

        public void setValuta(String tValuta) {
            this.Valuta = tValuta;
        }

        public String getNacin() {
            return Nacin;
        }

        public void setNacin(String tNacin) {
            this.Nacin = tNacin;
        }

        public String getDatum() {
            return Datum;
        }

        public void setDatum(String tDatum) {
            this.Datum = tDatum;
        }

        public String getDatumuplate() {
            return Datumuplate;
        }

        public void setDatumuplate(String tDatumuplate) {
            this.Datumuplate = tDatumuplate;
        }

        public String getnovID() {
            return novID;
        }

        public void setnovID(String tnovID) {
            this.novID = tnovID;
        }

    }
Federico klez Culloca
  • 26,308
  • 17
  • 56
  • 95
Aleksa Terzic
  • 35
  • 1
  • 10

1 Answers1

0

The line number being at the first line of that method is weird. It suggests that your source code may be out of sync with the code that you are running.

However, looking at the code of that method, I can see a couple of potential causes:

  • table could be null.
  • table.getValueAt(i, j) could return null for some i and j.

And there could be other potential causes if my assumptions about some other things are not correct. (A lot of potentially relevant code is not included in your question ....)

You should be able to find out where exactly the NPE is occurring by setting breakpoints and single-stepping with the debugger.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • Thank you Stephen, I will try to check those things once again. Check the full code I provided. I import the .csv file into table and then get all data from table to import into database. Importing into table works perfectly, it reads all data and fills the table. But I keep getting this error at importing to database – Aleksa Terzic Jun 26 '18 at 10:08