Jump to content
Sign in to follow this  
newcoder1010

Java lang null pointer exception

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 );

				}
	    		
	    	}

 

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
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

Share this post


Link to post
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 );
//
//				}

 

Share this post


Link to post
Share on other sites

The console should tell you exactly on what line the exception occurred.

Share this post


Link to post
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.

Share this post


Link to post
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
Sign in to follow this  

×