Jump to content

Java lang null pointer exception


newcoder1010

Recommended Posts

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

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 by newcoder1010
Link to comment
Share on other sites

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

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

  • 3 years later...
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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...