Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7598789
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T22:29:47+00:00 2026-05-30T22:29:47+00:00

I am using the Apache poi api for java to write into an existing

  • 0

I am using the Apache poi api for java to write into an existing sheet in an excel workbook. My problem is that in the worksheet that I am trying to edit, if any of the cell in the row is not null i.e. has some some sort of value in it, then the program runs fine and edits any any particular cell in that row. For example if the cell in my worksheet (1,1) contains a value then the program will have no problem editing other cells of the same row say (1,5) or (1,12) and so on. But if my entire row is empty i.e. all the cells contain null then the code throws a nullpointer exception which I cannot figure out how to remove. Here is the write method I used for my project to write a particular cell in my excel class.

public void write(int row, int column, String label) {


    try {
        InputStream inp = new FileInputStream(filePath);
        Workbook wb = WorkbookFactory.create(inp);
        Sheet sheet = wb.getSheetAt(0);
        Row sheetRow = sheet.getRow(row);
        Cell cell = sheetRow.getCell(column);
        //String cellContents = cell.getStringCellValue();
        //        //Modify the cellContents here
        //        // Write the output to a file
        if (cell == null) {
            cell = sheetRow.createCell(column);
            cell.setCellType(Cell.CELL_TYPE_STRING);
        }
        cell.setCellType(Cell.CELL_TYPE_STRING);
        cell.setCellValue(label);
        FileOutputStream fileOut = new FileOutputStream(filePath);
        wb.write(fileOut);
        fileOut.close();
    } catch (IOException ex) {
        Logger.getLogger(ExcelManipulator.class.getName()).log(Level.SEVERE, null, ex);
    } catch (InvalidFormatException ex) {
        Logger.getLogger(ExcelManipulator.class.getName()).log(Level.SEVERE, null, ex);
    }
}

The exception that java throws on editing a cell in an untouched i.e. a null row is :

Exception in thread "main" java.lang.NullPointerException
at seleniumtest.ExcelManipulator.write(ExcelManipulator.java:76)
at seleniumtest.SeleniumTest.main(SeleniumTest.java:28)

Java Result: 1

Can anybody help me get rid of this so that my code also writes a cell even if the whole row is null? Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T22:29:48+00:00Added an answer on May 30, 2026 at 10:29 pm

    I don’t know exactly where the NPE is thrown, but I’m pretty sure line 76 of ExcelManipulator.java is this line: Cell cell = sheetRow.getCell(column);?

    As stated in the API for Sheet you get a null if you ask for a undefined row – which is the case, if the whole row is empty.

    So you could insert if(sheetRow == null){sheetRow = sheet.createRow(row);} before above mentioned line and you should be fine.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to set Excel cell colors using Apache POI in Java. I played
I am using Apache POI for writing content into excel sheet. after generating an
Here's my problem. I'm now using using Java Apache POI to read an Excel
I am beginner to Apache POI api. I am trying to create excel sheet
I have just Extracted the cells from the excel sheet using Apache POI, everything
In Java, we're using the following package to programmatically create excel documents: org.apache.poi.hssf If
I am writing an Excel File using Apache POI. I want to write in
I have opened a large excel file as a Apache POI workbook (using VM
I am using Apache POI to store data in excel sheet. I can store
I'm trying to read .xlsm file using Apache POI library (3.8-beta5): Workbook wb =

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.