Writing data to Excel file by using Swing

Viewed 24

I am trying to write data in specific cell (in specific Row and Column) in Excel file by using Swing app the problem i have faced in my code thts he give me this error : (ERROR StatusLogger Log4j2 could not find a logging implementation. Please add log4j-core to the classpath. Using SimpleLogger to log to the console... java.lang.NullPointerException: Cannot invoke "javax.swing.JTextField.getText()" because "this.row" is null) what i have been mistaken can anyone help me

public class muhammed extends JFrame {

private static final long serialVersionUID = 1L;
private JPanel contentPane;
private JTextField tfData;
private JTextField row;

private JTextField column;
private JButton button;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
            try {
                muhammed frame = new ammar();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

public muhammed() throws FileNotFoundException { 
    
    Workbook wb = new HSSFWorkbook();
    OutputStream os = new FileOutputStream("IBER.csv");
    // Creating a sheet using predefined class
    // provided by Apache POI
    Sheet sheet = wb.createSheet("Company Preparation");

    int  x  = Integer.parseInt(row.getText());

    // Specific row number
    Row Row1 = sheet.createRow(x);

    int y  = Integer.parseInt(column.getText());
    // Specific cell number
    Cell cell = Row1.createCell(y);
    
    int   a  = Integer.valueOf(tfData.getText());
    
    cell.setCellValue(a);
    
    // Finding index value of row and column of give
    // cell

    button = new JButton("Click");
    button.setBounds(1, 1, 1, 1);

    JLabel veri = new JLabel("veri GIRINIZ", JLabel.RIGHT);
    JLabel rowIndex1 = new JLabel(" ROW GIRINIZ", JLabel.RIGHT);
    JLabel colIndex = new JLabel(" COLUMN GIRINIZ ", JLabel.RIGHT);
    tfData = new JTextField(10);
    column = new JTextField(10);
    row = new JTextField(10);

    rowIndex1.setLabelFor(row);
    colIndex.setLabelFor(column);
    veri.setLabelFor(tfData);
    contentPane = new JPanel();
    contentPane.setLayout(new GridLayout(5, 5, 5, 5));
    contentPane.add(veri);
    
    contentPane.add(rowIndex1);
    contentPane.add(row);
    contentPane.add(colIndex);
    contentPane.add(column);
    contentPane.add(button);
    JFrame frame_1 = new JFrame();
    frame_1.addWindowListener(null); 
    frame_1.setContentPane(contentPane);
    frame_1.pack();
    frame_1.setVisible(true);

    button.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            
            int row_1 = cell.getRowIndex();
            int column_1 = cell.getColumnIndex();
            String b        = tfData.getText();
            //int row_1 = ((Cell) row).getRowIndex();
            //int column_1 = ((Cell) column).getColumnIndex();
            
             System.out.println(b
                     + "(" + row_1 + ","
                     + column_1 + ")");
            try {

                FileOutputStream fileOut = new FileOutputStream(
                        new File("IBER.csv"));

                HSSFWorkbook workbook = new HSSFWorkbook();
                
                wb.write(fileOut);

                wb.close();
                workbook.close();
            } catch (Exception exception) {
                exception.printStackTrace();
            }

        }
    });
}}
0 Answers
Related