Skip to Content
avatar image
Former Member

java.lang.NullPointerException : A solution for it.

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

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

5 Answers

  • Best Answer
    avatar image
    Former Member
    Jan 03, 2006 at 10:33 AM

    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

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Jan 03, 2006 at 07:47 AM

    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

    Add comment
    10|10000 characters needed characters exceeded

    • Former Member Former Member

      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

  • avatar image
    Former Member
    Jan 03, 2006 at 08:49 AM

    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

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Jan 03, 2006 at 08:59 AM

    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

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Jan 03, 2006 at 03:00 PM

    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

    Add comment
    10|10000 characters needed characters exceeded