-3
 try{
    DefaultTableModel tm = (DefaultTableModel) jTable1.getModel();
    FileInputStream FI = new FileInputStream("C://Users//Dell-Pc//Documents//NetBeansProjects//MyBookShop//src//Layouts//book_store.txt");
    DataInputStream DI = new DataInputStream(FI);
    BufferedReader br= new BufferedReader(new InputStreamReader(DI));
    String line="";
    while((line=br.readLine()) != null){
        Map m1 = new HashMap(); 
        String[] data1=line.split(",");

            m1.put("ISBN", data1[0]);
            m1.put("TITLE", data1[1]);
            m1.put("AUTHOR", data1[2]);
            m1.put("PRICE", data1[3]);
            m1.put("NUMBER", data1[4]);
            //System.out.println(data1[1]);
            //System.out.println("\t"+m1);
            Vector v1 = new Vector();
            v1.add(m1.get("ISBN"));
            v1.add(m1.get("TITLE"));
            v1.add(m1.get("AUTHOR"));
            v1.add(m1.get("PRICE"));
            v1.add(m1.get("NUMBER"));

            tm.addRow(v1);

        //v.clear();
       //tm.setDataVector(data, cols);

    }
    }catch(Exception e){

// JOptionPane.showMessageDialog(this, e);

Logger.getLogger(HomePage.class.getName()).log(Level.SEVERE, null, e); }

Cœur
  • 37,241
  • 25
  • 195
  • 267
  • 2
    We are not mind readers. We have no idea what statement is causing the problem. We have no idea what the data in your file looks like. Post a proper [mcve] that demonstrates the problem. If you really need to read data from a file then use a `StringReader` for the data. – camickr May 24 '17 at 17:31
  • In addition to the MCVE suggested by @camickr. 1) See [What is a stack trace, and how can I use it to debug my application errors?](http://stackoverflow.com/q/3988788/418556) 2) Always copy/paste error and exception output! 3) Please use code formatting for code and code snippets, structured documents like HTML/XML or input/output (like stack traces). To do that, select the text and click the `{}` button at the top of the message posting/editing form. 4) Hard code some data to replace the `book_store.txt`. – Andrew Thompson May 24 '17 at 17:39
  • Your code would be easier to read if you followed​the Java naming conventions. And don't use `java.util.Vector`. It's been obsolete for about nineteen years. – Lew Bloch May 24 '17 at 18:16
  • 1
    I took the time to format you code 50 minutes ago, why did you undo the changes? I'm not going to fix the code a second time! Don't use your browser back buttons. Click on the `"edit"` link of your question when you add new information. Still don't know why you edited the question since you haven't added any new information. – camickr May 24 '17 at 18:25
  • 1
    @LewBloch, `And don't use java.util.Vector.` - that is what the DefaultTableModel API uses. The OP needs to learn the basic of how a JTable and TableModel works before creating custom TableModel that uses a different data storage. – camickr May 24 '17 at 18:29
  • well, i am sorry to everybody. I am a new user.lets get into question.i found the solution but still i didn't get it.it was throwing an exception because i had a null record in the text file.please explain me that. – mycodestuffs May 24 '17 at 18:41

1 Answers1

0

The problem, is that you are consuming lines without verifying that they are any good.

If your file has an empty line at the end (common), than line.split(",") gives [""] for that line. Index zero is a valid access (an empty string), but Index one is invalid, and everything crashes. You should verify the data is good before doing accesses that can throw an error (at least surround with a try - catch)

Tezra
  • 8,463
  • 3
  • 31
  • 68