newcoder1010 3 Posted June 17, 2017 Report Share Posted June 17, 2017 Hello, Hello, I am a beginner of java. I wrote this code. When I run it in eclipse, I get an error: java lang null point exception. It does not tell me which line I have wrong code. Please advise on how to fix it. // Open the Excel file FileInputStream fis = new FileInputStream(file); // Access the required test data sheet HSSFWorkbook wb = new HSSFWorkbook(fis); HSSFSheet sheet = wb.getSheet("Sheet1"); int TotalCol; int TotalRow; // TotalCol = 0; TotalRow = sheet.getLastRowNum(); TotalCol = sheet.getRow(0).getLastCellNum(); System.out.println("row " + TotalRow + " col " + TotalCol); int resultcol; resultcol = 0; for(int col = 0;col<=TotalCol;col++){ HSSFRow row1 = sheet.getRow(0); System.out.println("column names " + row1.getCell(col).toString() ); if("result".equalsIgnoreCase(row1.getCell(col).toString().trim())) { resultcol = col; System.out.println("result column number " + resultcol ); } } Quote Link to post Share on other sites
Ingolme 1,020 Posted June 17, 2017 Report Share Posted June 17, 2017 You're going to have to tell me what line its on, but usually that means you tried to use an object that wasn't initialized. Quote Link to post Share on other sites
newcoder1010 3 Posted June 17, 2017 Author Report Share Posted June 17, 2017 (edited) I am not a good debugger. I need to learn how to debug in eclipse. First, I commented all code. Then I un-commented one line at a time and ran it. I dont have any error until I reach this line: System.out.println("column names " + row1.getCell(col).toString() ); Even though I get the error, it still printed all the messages from the above code. Edited June 17, 2017 by newcoder1010 Quote Link to post Share on other sites
newcoder1010 3 Posted June 17, 2017 Author Report Share Posted June 17, 2017 This block of the code causing it. Please advise. //System.out.println("column names " + row1.getCell(col).toString() ); // if("result".equalsIgnoreCase(row1.getCell(col).toString().trim())) // { // resultcol = col; // System.out.println("result column number " + resultcol ); // // } Quote Link to post Share on other sites
Ingolme 1,020 Posted June 17, 2017 Report Share Posted June 17, 2017 The console should tell you exactly on what line the exception occurred. Quote Link to post Share on other sites
newcoder1010 3 Posted June 17, 2017 Author Report Share Posted June 17, 2017 It did not give me the line no. https://drive.google.com/open?id=1FtMZVH2qm8zFAsONqFRWjvB6QRFlqZ8iXd2EbCRd-Tc Quote Link to post Share on other sites
Ingolme 1,020 Posted June 17, 2017 Report Share Posted June 17, 2017 It says in your editor that the exception occurred in LoginTest.java at line 77. You should check to see if the variable row1 is null. Quote Link to post Share on other sites
RyanLincoln 0 Posted August 14, 2020 Report Share Posted August 14, 2020 On 6/17/2017 at 10:57 PM, newcoder1010 said: Hello, Hello, I am a beginner of java. I wrote this code. When I run it in eclipse, I get an error: java lang null point exception. It does not tell me which line I have wrong code. Please advise on how to fix it. // Open the Excel file FileInputStream fis = new FileInputStream(file); // Access the required test data sheet HSSFWorkbook wb = new HSSFWorkbook(fis); HSSFSheet sheet = wb.getSheet("Sheet1"); int TotalCol; int TotalRow; // TotalCol = 0; TotalRow = sheet.getLastRowNum(); TotalCol = sheet.getRow(0).getLastCellNum(); System.out.println("row " + TotalRow + " col " + TotalCol); int resultcol; resultcol = 0; for(int col = 0;col<=TotalCol;col++){ HSSFRow row1 = sheet.getRow(0); System.out.println("column names " + row1.getCell(col).toString() ); if("result".equalsIgnoreCase(row1.getCell(col).toString().trim())) { resultcol = col; System.out.println("result column number " + resultcol ); } } Have you tried to debug your code? Quote Link to post Share on other sites
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.