cancel
Showing results for 
Search instead for 
Did you mean: 

GET_MANDATORY_ELEMENTS Null Pointer Exception

Former Member
0 Kudos

Hello!

By checking the mandatory field with the following code

data: lt_messages TYPE cl_wd_dynamic_tool=>t_check_result_message_tab.

data : io_view_controller type ref to IF_WD_VIEW_CONTROLLER.
cl_wd_dynamic_tool=>check_mandatory_attr_on_view(
EXPORTING
view_controller = io_view_controller
display_messages = abap_true
IMPORTING
messages = lt_messages ).

We get the following Null pointer exception error:

An exception occurred that is explained in detail below.

The exception, which is assigned to class 'CX_SY_REF_IS_INITIAL', was not

caught in

procedure "GET_MANDATORY_ELEMENTS" "(METHOD)", nor was it propagated by a

RAISING clause.

Since the caller of the procedure could not have anticipated that the

exception would occur, the current program is terminated.

The reason for the exception is:

You attempted to use a 'NULL' object reference (points to 'nothing')

access a component.

An object reference must point to an object (an instance of a class)

before it can be used to access components.

Either the reference was never set or it was set to 'NULL' using the

CLEAR statement.

What do I miss?

Thank you!

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Hi Alex,

Just curious looking at the code-snippet, have you assigned the view controller reference to io_view_controller?

data: lt_messages TYPE cl_wd_dynamic_tool=>t_check_result_message_tab.

data : io_view_controller type ref to IF_WD_VIEW_CONTROLLER.

io_view_controller = wd_this->wd_get_api( ).  <--- Is this missing?
cl_wd_dynamic_tool=>check_mandatory_attr_on_view(
EXPORTING
view_controller = io_view_controller
display_messages = abap_true
IMPORTING
messages = lt_messages ).

Regards,

Priyanka

Former Member
0 Kudos

Hello!

I added the following code into the check_method within COMPONENTCONTROLLER

data: lt_messages TYPE cl_wd_dynamic_tool=>t_check_result_message_tab.
data : io_view_controller type ref to IF_WD_VIEW_CONTROLLER.
io_view_controller ?= wd_this->wd_get_api( ).
cl_wd_dynamic_tool=>check_mandatory_attr_on_view(
EXPORTING
view_controller = io_view_controller
display_messages = abap_true
IMPORTING
messages = lt_messages ).

Unfortunately I get CX_SY_MOVE_CAST_ERROR dump the following description:

Dynamic type conflict when assigning references

source type: "\CLASS=CL_WDR_DELEGATING_COMPONENT"

target type: "\INTERFACE=IF_WD_VIEW_CONTROLLER"

Answers (1)

Answers (1)

former_member185241
Active Participant
0 Kudos

Hey Alex

You can use following code

data :  lref_view_controller Type ref to if_wd_View_controller

          lt_msg                  type          cl_wd_dynamic_tool=>t_check_result_message_tab.

  lref_view_controller ?= wd_this->wd_get_api( ).

  cl_wd_dynamic_tool=>check_mandatory_attr_on_view(

  exporting

  view_controller = lref_view_controller

  display_messages = abap_true

  importing

  messages = lt_msg ). "end of code to check mandatory fields

Thanks

Abhishek

Former Member
0 Kudos

Hello!

It looks pretty good now.

How can I change the error description from standard

"Fill all required files" into my own description "You have to not specified Category"?

I have already something that I can reuse:

lv_msg = WD_ASSIST->IF_WD_COMPONENT_ASSISTANCE~GET_TEXT( 'E15' ).
CALL METHOD lo_message_manager->REPORT_ATTRIBUTE_ERROR_MESSAGE
EXPORTING
MESSAGE_TEXT             
= lv_msg
ELEMENT                  
= lo_el_nd_add_context
ATTRIBUTE_NAME           
= 'CATEGORY'

   

How can I integrate this into the check_mandatory_attr_on_view?

cl_wd_dynamic_tool=>check_mandatory_attr_on_view(
EXPORTING
view_controller = io_view_controller
display_messages = abap_true
IMPORTING
messages = lt_messages ).

amy_king
Active Contributor
0 Kudos

Hi Alex,

To prevent check_mandatory_attr_on_view from issuing its own message, you can pass argument display_messages = false and issue your own message, e.g.,

  cl_wd_dynamic_tool=>check_mandatory_attr_on_view(
    exporting
      view_controller  = view
      display_messages = abap_false
    importing
      messages = lt_messages
  ).

  * Instead of just checking if the table is not initial, you 
  * probably want to read lt_messages for an error where 
  * attribute_name = CATEGORY  
  if lt_messages[] is not initial. 
    lv_msg = wd_assist->if_wd_component_assistance~get_text( 'E15' ).
    call method lo_message_manager->report_attribute_error_message
      exporting
        message_text              = lv_msg
        element                   = lo_el_nd_add_context
        attribute_name            = 'CATEGORY'
  endif.

Cheers,

Amy

Former Member
0 Kudos

Hi Alex, 

1) Disable display_message = abap_false:

cl_wd_dynamic_tool=>check_mandatory_attr_on_view( 

  exporting

  view_controller = lref_view_controller

  display_messages = abap_true

  importing

  messages = lt_msg ).

2) Check the message table and use you custom messages using methods of the IF_WD_MESSAGE_MANAGER . Helpful link: http://scn.sap.com/thread/1275916

Regards,

Priyanka

Former Member
0 Kudos

Hello!

Many thanks again!

Now I get Access via 'NULL' object reference not possible.

My code:

data: lt_messages TYPE cl_wd_dynamic_tool=>t_check_result_message_tab.
data : io_view_controller type ref to IF_WD_VIEW_CONTROLLER.
DATA: lv_msg  type string.
DATA lo_el_nd_add_context TYPE REF TO if_wd_context_element.
data lo_api_controller     type ref to if_wd_controller.
     data lo_message_manager    type ref to if_wd_message_manager.
     lo_api_controller ?= wd_This->Wd_Get_Api( ).
     CALL METHOD lo_api_controller->GET_MESSAGE_MANAGER
       RECEIVING
         MESSAGE_MANAGER = lo_message_manager.

io_view_controller = wd_this->wd_get_api( ).
cl_wd_dynamic_tool=>check_mandatory_attr_on_view(
EXPORTING
view_controller = io_view_controller
display_messages = abap_false
IMPORTING
messages = lt_messages ).

if lt_messages[] is not initial.
     lv_msg = wd_assist->if_wd_component_assistance~get_text( 'E15' ).
     call method lo_message_manager->report_attribute_error_message
       exporting
         message_text              = lv_msg
         element                   = lo_el_nd_add_context
         attribute_name            = 'CATEGORY'.
endif.

former_member185241
Active Participant
0 Kudos

Hey Alex

I think we can not change the message, why i m saying like, Just see the followoing code of method "check_mandatory_attr_on_view".

method check_mandatory_attr_on_view.

data l_mandatory_elements           type cl_wd_dynamic_tool=>tt_ext_check_mandatory.
data l_mandatory_element            type cl_wd_dynamic_tool=>t_ext_check_mandatory.
data l_bound_primary_property       type string.
data l_context                      type ref to if_wd_context.
data l_has_errors                   type wdy_boolean.
data l_last_point                   type i.
data l_message                      type t_check_result_message.
data l_context_node                 type ref to if_wd_context_node.
data l_context_element              type ref to if_wd_context_element.
data l_node_path                    type string.
data l_attribute_name               type string.
data l_context_element_set          type wdr_context_element_set.
data l_message_manager              type ref to if_wd_message_manager.
data l_view                         type ref to if_wd_view.
data l_element_empty_ok             type wdy_boolean.
data l_text                         type string.

*  l_view ?= cl_wdr_proxy_cast_tool=>get_instance( view_controller ).
l_view ?= view_controller.
lcl_mandatory_check=>get_mandatory_elements( exporting view = l_view  importing view_elements = l_mandatory_elements ).
check l_mandatory_elements is not initial.
l_context = view_controller->get_context( ).
if display_messages = abap_true.
l_message_manager = view_controller->get_message_manager( ).
endif.

loop at l_mandatory_elements into l_mandatory_element.
l_bound_primary_property = l_mandatory_element-view_element->bound__primary_property( ).
check l_bound_primary_property is not initial. "if no primary property is defined (fileupload) this is initial
find all occurrences of '.' in l_bound_primary_property match offset l_last_point.
if sy-subrc = 0.
l_node_path = l_bound_primary_property(l_last_point).
add 1 to l_last_point.
l_attribute_name = l_bound_primary_property+l_last_point.
l_context_node = l_context->root_node->path_get_node( l_node_path ).
else.
l_attribute_name = l_bound_primary_property.
l_context_node = l_context->root_node.
endif.
l_element_empty_ok = abap_false.
clear l_context_element_set.
l_context_element_set = lcl_mandatory_check=>get_ctxt_elements_to_check(
mandatory_element   = l_mandatory_element
context_root        = l_context->root_node
context_node        = l_context_node
node_path           = l_node_path
attribute_name      = l_attribute_name ).
loop at l_context_element_set into l_context_element.
l_has_errors = int_check_mandatory_attribute(
element          = l_context_element
attribute        = l_attribute_name
element_empty_ok = l_element_empty_ok  ).

if l_has_errors = abap_true.
l_message-context_element = l_context_element.
l_message-attribute_name = l_attribute_name.
l_message-t100_message-msgty = 'E'.
l_message-t100_message-msgid = 'WEBDYNPRO_RT'.
l_message-t100_message-msgno = '014'.
insert l_message into table messages.
if l_message_manager is bound.
l_message_manager->report_attribute_t100_message(
EXPORTING
msg                       =   l_message-t100_message
element                   =   l_context_element
attribute_name            =   l_attribute_name ).
endif.
endif.
clear: l_has_errors.
endloop.
clear: l_context_element_set, l_element_empty_ok, l_last_point, l_node_path.
endloop.

endmethod.

See internally they are calling the message class with message id WEBDYNPRO_RT and message no '014'.

That means they are throwing error message, I think i can not change.

Please correct me if i m wrong.

Thanks and Regards,

Abhishek 

amy_king
Active Contributor
0 Kudos

Hi Alex,

Check transaction ST22 to see at what line of code the exception is happening. You must be calling a method of some object that is still not instantiated. For example, in the code snippet above, has lo_el_nd_add_context been instantiated?

Cheers,

Amy

Former Member
0 Kudos

Hello Amy,

and finally it looks good.

Meanwhile there are some differences, e.g. the Category field does not highlighted.

If you have some corrections, just put it here.

data: lt_messages TYPE cl_wd_dynamic_tool=>t_check_result_message_tab.
data : io_view_controller type ref to IF_WD_VIEW_CONTROLLER.
DATA lo_el_nd_add_context TYPE REF TO if_wd_context_element.
DATA lo_nd_cc_req_context TYPE REF TO if_wd_context_node.
DATA lo_el_cc_req_context TYPE REF TO if_wd_context_element.
DATA ls_cc_req_context TYPE wd_this->element_cc_req_context.
lo_nd_cc_req_context
= wd_context->get_child_node( name = wd_this->wdctx_cc_req_context ).
lo_el_cc_req_context
= lo_nd_cc_req_context->get_element(  ).
lo_el_cc_req_context
->get_static_attributes(
IMPORTING
static_attributes
= ls_cc_req_context ).
DATA lo_nd_nd_add_context TYPE REF TO if_wd_context_node.
DATA ls_nd_add_context TYPE wd_this->element_nd_add_context.
lo_nd_nd_add_context
= wd_context->get_child_node( name = wd_this->wdctx_nd_add_context ).
lo_el_nd_add_context
= lo_nd_nd_add_context->get_element(  ).
lo_el_nd_add_context
->get_static_attributes(
IMPORTING
static_attributes
= ls_nd_add_context ).
DATA: lv_msg  type string.
*DATA lo_el_nd_add_context TYPE REF TO if_wd_context_element.
data lo_api_controller     type ref to if_wd_controller.
data lo_message_manager    type ref to if_wd_message_manager.
lo_api_controller ?= wd_This
->Wd_Get_Api( ).
CALL METHOD lo_api_controller->GET_MESSAGE_MANAGER
RECEIVING
MESSAGE_MANAGER
= lo_message_manager.

io_view_controller
= wd_this->wd_get_api( ).
cl_wd_dynamic_tool
=>check_mandatory_attr_on_view(
EXPORTING
view_controller
= io_view_controller
display_messages
= abap_false
IMPORTING
messages
= lt_messages ).

if lt_messages[] is not initial.
lv_msg
= wd_assist->if_wd_component_assistance~get_text( 'E15' ).
call method lo_message_manager->report_attribute_error_message
exporting
message_text             
= lv_msg
element                  
= lo_el_nd_add_context
attribute_name           
= 'CATEGORY'.
endif.

amy_king
Active Contributor
0 Kudos

Hi Alex,

If the field is not highlighted, it means the runtime cannot find element lo_el_nd_add_context with an attribute category on the view. Some things you could check...

  • Check in the debugger to be sure element lo_el_nd_add_context is not initial.
  • Check on the view layout that the required field Level 1 is really bound to lo_el_nd_add_context-category.

Cheers,

Amy

Former Member
0 Kudos

Hello,

when I set

display_messages = abap_true I get both the standard error message from check_mandatory_attr_on_view() and the customized error message.

Perhaps it is the workaround for me.