cancel
Showing results for 
Search instead for 
Did you mean: 

java.lang.NullPointerException : A solution for it.

Former Member
0 Kudos

Hi All

In my application I'm displaying data in form of table and also added a column of input type for input from user.

e.g.

Month AvailVolume ReqdVol

Information of Month and AvailVolume coming from the backend and ReqdVol has to be given by the user.

After the user fills the data.

On click of button (in same view) only those rows of data should be displayed for which the user has entered.

e.g. Month AvailVol ReqVol

June 10,000 9000

July 10,000

August 10,000 6000

I want the output should be like this:

Month ReqVol

June 9000

August 6000

For this I've written the following code in action handler which we'll execute this action


try{
     
		IPrivateStartView.IEnterTableElement enterTableElement ;
		IPrivatePreBuyPricesStartView.ITableDisplayElement displayTableElement ;
		int size = 0;
		int sizeMax = wdContext.nodeEnterTable().size();
    	wdComponentAPI.getMessageManager().reportSuccess("Max Size: "+ sizeMax);
	
		BigDecimal InputVol = null;
		String Month = null;
		String input = null; 
		
		for(; size<sizeMax;size++)
		{
			
			enterTableElement = wdContext.nodeEnterTable().getEnterTableElementAt(size);
			displayTableElement = wdContext.createTableDisplayElement();
			Input =   enterTableElement.getCInput();
			Month = enterTableElement.getCmpDate();
			input = Input.toString();	
						
			 if (input != null || Month != null || input.length()>=0)
			 { 			 
				displayTableElement.setVolume(input);
				wdComponentAPI.getMessageManager().reportSuccess(input);
				displayTableElement.setMonth(Month);
				wdComponentAPI.getMessageManager().reportSuccess(Month);
			}
			else  
			{
				continue;
			}
			wdContext.nodeTableDisplay().addElement(displayTableElement);
			
		
		}
		}
		catch(Exception ex)
		{
			wdComponentAPI.getMessageManager().reportException(ex.toString(),false);
		}

I've used the above code snippet for my purpose and its giving me NullPointerException if I leave any row blank.

Can some body provide me a solution. its bit urgent.

Thanks in Advance

Srikant

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Hi,

In addition to the solutions described below you could 'catch' the exception so that your application does not halt.

e.g:

if (input != null || Month != null || input.length()>=0)

{

try {

displayTableElement.setVolume(input);

wdComponentAPI.getMessageManager().reportSuccess(input);

displayTableElement.setMonth(Month);

wdComponentAPI.getMessageManager().reportSuccess(Month);

} catch (Exception e) {

e.printStackTrace() // display error in standard out

log.error(e.getMessage()) // print error in log

} finally {

// place code here to make the application continue, if necessary

}

}

This is acceptable if the error rarely occors. If the error occurs moreoften, it is better to test precondition as the other posts suggest.

Good luck!

Roelof

Former Member
0 Kudos

Catching NullPointerExceptions is not good programming style IMHO.

Armin

Answers (4)

Answers (4)

Former Member
0 Kudos

Hello,

in my opinion your code throws the NullPointerException at this line:

input = Input.toString();

because in some cases (when the user enters no data in that row) Input is null.

So you'd have to check if Input is not null before you call the toString() method.

Bye,

Bernd

Former Member
0 Kudos

Hi Srikant,

It would be helpfull if you would point out the line of code where the exception occurs (You find this in the stack trace). I suppose that the enterTableElement, which is returned by wdContext.nodeEnterTable().getEnterTableElementAt(size); is NULL. Then you get a NullPointerException when accessing enterTableElement.getCInput();

Try to put something like if (enterTableElement != null) around the whole block after the line where you assign the variable enterTableElement.

regards,

Martin

Former Member
0 Kudos

Hi,

What is INPUT type..If its a numeric type why cant u check INPUT!=0 (somthin similar) instead of input.length().


input = Input.toString();	
if (input != null || Month != null || input.length()>=0)

input.length will give nullpointer exception id input is null..

Quite obvious.. But i guess you have overlooked it..

Check it..

Thanks and Regards

Bharathwaj

Former Member
0 Kudos

Hi Srikant,

It would be very helpful if you could specifically point out the line where you found the exception and provide us the stacktrace.

Regards,

Noufal

Former Member
0 Kudos

Hi Noufal,

I give you example with following situations

Month AvailVol ReqVol

June 10,000 9000

July 10,000 8000

Aug 20,000 6000

Sep 15,000 7000

Oct 10,000 5000

The output from server consists of 5 values as displayed from month of June to Oct.

If the user fills all the rows under ReqVol. On click of button the whole data is displayed.

a) If you don't fill any data --> it gives NullPointerException.

b) If you fill data for 1st row and leaves other --> it displays first row and after that its gives the error.

c) If you fill data for say 2 rows and leave the next row blank and then fill --> it displays only 2 rows and then gives exception.

d)If i leave and fill the rest of rows as i like --> it gives exception.

So, what I understood that unless I don't fill all rows with some data the exception doesn't go.

My question, What kind of condition should i give for checking so that it takes up the blank rows as well.

Thanks in advance

Srikant

Former Member
0 Kudos

Hi Srikant,

Try this condition

if(input != null && input.length()> 0)
{
   displayTableElement.setVolume(input); 
}
if(Month != null && Month.length() > 0)
{
  displayTableElement.setMonth(Month);   
}

If the input or month is NULL it will not execute the second part of the expression.

Thanks

Senthil

P.S: Reward Points for Useful answers