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

Code:

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

The problem is in:

   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()});
Dylan Wheeler
  • 6,928
  • 14
  • 56
  • 80
EL hassane
  • 134
  • 1
  • 9

2 Answers2

0

From what I can tell by your code, something in your jTable2 is not getting initialized properly.

The idea of a NullPointerException is that you are trying to use an object that hasn't been created yet. Since you are able to iterate over the jTable2, and you don't have an index out of bounds exception, I would say that one of the elements of the jTable2 is not being created

What I would suggest is going over this in the debugger, carefully analyzing each row of the table before you try to convert it to a String


Also, (this is more of a style thing, but I think it'll help you in the long run) you should move this sort of functionality into another method, so that it'll be easier to read:

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         
}

I promise you this will make debugging much much easier

Community
  • 1
  • 1
Jeeter
  • 5,887
  • 6
  • 44
  • 67
0

Java documentation 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

I have no idea what your trying to put in.. but i would check if you might be putting in a null value.

Javant
  • 1,009
  • 10
  • 17