线程异常 "AWT-EventQueue-0" java.lang.NullPointerException TreeMap

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException TreeMap

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at dacia.Principale.jButton10ActionPerformed(Principale.java:1204)
    at dacia.Principale.access0(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()});

根据你的代码我可以看出,你的 jTable2 中的某些内容没有正确初始化。

NullPointerException 的意思是您正在尝试使用尚未创建的对象。由于您能够遍历 jTable2,并且您没有索引越界异常,我会说 jTable2 的元素之一没有被创建

我建议在调试器中检查它,在尝试将其转换为 String

之前仔细分析 table 的每一行

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

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         
}

我向你保证,这将使调试变得容易得多

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

Returns: the previous value associated with key, or null if there was no mapping for key. (A null return can also indicate that the map previously associated null with key.)

NullPointerException - if the specified key is null and this map uses natural ordering, or its comparator does not permit null keys

我不知道你想输入什么..但我会检查你是否输入了空值。