线程“AWT-EventQueue-0”中的异常java.lang.NullPointerException TreeMap

时间:2016-07-20 17:33:12

标签: java netbeans netbeans-8

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at dacia.Principale.jButton10ActionPerformed(Principale.java:1204)
    at dacia.Principale.access$500(Principale.java:44)

代码:

   XSSFWorkbook wg= new XSSFWorkbook();
    XSSFSheet sht=wg.createSheet();

    TreeMap<String,Object[]> data= new TreeMap<>();
  //  data.put("-1",new Object[]){"chasis","marque","couleur","nom client","date d entree","date sortie","telephone","numero WW","position","mode paiment","gamme","version"});
    data.put("-1",new Object[]{jTable2.getColumnName(0),jTable2.getColumnName(1),jTable2.getColumnName(2),jTable2.getColumnName(3),jTable2.getColumnName(4),jTable2.getColumnName(5),jTable2.getColumnName(6),jTable2.getColumnName(7),jTable2.getColumnName(8),jTable2.getColumnName(9),jTable2.getColumnName(10),jTable2.getColumnName(11)});

    for(int i=0;i<jTable2.getRowCount();i++)
   {

       data.put(Integer.toString(i),new Object[]{jTable2.getValueAt(i,0).toString(),jTable2.getValueAt(i,1).toString(),jTable2.getValueAt(i,2).toString(),jTable2.getValueAt(i,3).toString(),jTable2.getValueAt(i,4).toString(),jTable2.getValueAt(i,5).toString(),jTable2.getValueAt(i,6).toString(),jTable2.getValueAt(i,7).toString(),jTable2.getValueAt(i,8).toString(),jTable2.getValueAt(i,9).toString(),jTable2.getValueAt(i,10).toString(),jTable2.getValueAt(i,11).toString()});
   }
   //write to excel
   Set <String> ids=data.keySet();
   XSSFRow row ;
   int roow=0 ;
   for (String st : ids)
   {
       row=sht.createRow(roow++);
       Object[] values=data.get(st);
       int cellId=0 ;
       for(Object o : values)
       {
           Cell cell=row.createCell(cellId++);
           cell.setCellValue(o.toString());

       }
   }
    try {
        //write to file
        FileOutputStream fos= new FileOutputStream(new File("/home/elprincipe/Desktop/dacia.xls"));
        wg.write(fos);
        fos.flush();
        fos.close();

    } catch (FileNotFoundException ex) {
        Logger.getLogger(Principale.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(Principale.class.getName()).log(Level.SEVERE, null, ex);
    }

问题在于:

   data.put(Integer.toString(i),new Object[]{jTable2.getValueAt(i,0).toString(),jTable2.getValueAt(i,1).toString(),jTable2.getValueAt(i,2).toString(),jTable2.getValueAt(i,3).toString(),jTable2.getValueAt(i,4).toString(),jTable2.getValueAt(i,5).toString(),jTable2.getValueAt(i,6).toString(),jTable2.getValueAt(i,7).toString(),jTable2.getValueAt(i,8).toString(),jTable2.getValueAt(i,9).toString(),jTable2.getValueAt(i,10).toString(),jTable2.getValueAt(i,11).toString()});

2 个答案:

答案 0 :(得分:0)

从我的代码中可以看出,jTable2中的某些内容未正确初始化。

NullPointerException的想法是你正在尝试使用尚未创建的对象。既然你能够遍历jTable2,并且你没有索引超出范围的异常,我会说jTable2的其中一个元素没有被创建

我建议在调试器中对此进行检查,在尝试将其转换为String之前仔细分析表格的每一行

另外,(这更像是一种风格,但我认为从长远来看它会帮助你)你应该将这种功能转移到另一种方法中,这样它会更容易阅读:

for(int i = 0; i < jTable2.getRowCount(); i++) {
    data.put(convertRow(jTable2, i));
}

public Object[] convertRow(Table jTable2, int row) {
    rowLength = jTable2[0].length;
    Object[] row = new Object[rowLength];
    for (int i = 0; i < rowLength; i++) {
        Object datum = jTable2.getValueAt(row, i);
        if (datum != null) {
            row[i] = datum.toString();
        }
        else {
            row[i] = "Null entry"
        }
    }

    return row         
}

我向你保证这将使调试变得更加容易

答案 1 :(得分:0)

Java文档https://docs.oracle.com/javase/8/docs/api/java/util/TreeMap.html#put-K-V-

  

返回:与key关联的先前值,如果有,则返回null   没有密钥的映射。 (null返回也可以指示地图   以前使用密钥关联null。)

     

NullPointerException - 如果指定的键为null且此映射使用   自然排序,或其比较器不允许空键

我不知道你想要放入什么...但我会检查你是否可能放入一个空值。