为什么我得到java.lang.ArrayIndexOutOfBoundsException:1?

时间:2017-05-24 17:27:47

标签: java swing indexoutofboundsexception

 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);         }

1 个答案:

答案 0 :(得分:0)

问题在于,你在没有验证它们是否有用的情况下消耗线条。

如果您的文件末尾有空行(常见),则line.split(",")为该行提供[""]。索引零是有效访问(空字符串),但索引1无效,一切都崩溃。您应该在进行可能引发错误的访问之前验证数据是否正常(至少包围try-catch)