newcoder1010 Posted June 17, 2017 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 ); } } Link to comment Share on other sites More sharing options...
Ingolme Posted June 17, 2017 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. Link to comment Share on other sites More sharing options...
newcoder1010 Posted June 17, 2017 Author 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 Link to comment Share on other sites More sharing options...
newcoder1010 Posted June 17, 2017 Author 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 ); // // } Link to comment Share on other sites More sharing options...
Ingolme Posted June 17, 2017 Share Posted June 17, 2017 The console should tell you exactly on what line the exception occurred. Link to comment Share on other sites More sharing options...
newcoder1010 Posted June 17, 2017 Author Share Posted June 17, 2017 It did not give me the line no. https://drive.google.com/open?id=1FtMZVH2qm8zFAsONqFRWjvB6QRFlqZ8iXd2EbCRd-Tc Link to comment Share on other sites More sharing options...
Ingolme Posted June 17, 2017 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. Link to comment Share on other sites More sharing options...
RyanLincoln Posted August 14, 2020 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? Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now