Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

Getting error for null object reference, while implementing class in module pool ?

nbitish
Participant
0 Kudos

Hello everyone,

Please help me eliminate this error, I am new to abap. Would be very thankful for that.

SPAN { font-family: "Courier New"; font-size: 10pt; color: #000000; background: #FFFFFF; } .L0S31 { font-style: italic; color: #808080; } .L0S33 { color: #4DA619; } .L0S52 { color: #0000FF; } .L0S55 { color: #800080; } .L0S70 { color: #808080; }

class ZCL_NB_CLASS definition
public
create public .

public section.
*"* public components of class ZCL_NB_CLASS
*"* do not include other source files here!!!

methods DEPOSIT
importing
value(AMOUNT) type DMBTR
returning
value(NEW_BALANCE) type DMBTR .
methods SET_BALANCE
importing
value(AMOUNT) type DMBTR
returning
value(NEW_BALANCE) type DMBTR .
methods WITHDRAW
importing
value(AMOUNT) type DMBTR
returning
value(NEW_BALANCE) type DMBTR
exceptions
INSUFFICIENT_FUNDS .
methods MESSAGE
exporting
!MESS type BAPIRET2 .
protected section.
*"* protected components of class ZCL_NB_CLASS
*"* do not include other source files here!!!
private section.
*"* private components of class ZCL_NB_CLASS
*"* do not include other source files here!!!

class-data BALANCE type DMBTR .
ENDCLASS.



CLASS ZCL_NB_CLASS IMPLEMENTATION.


* <SIGNATURE>---------------------------------------------------------------------------------------+
* | Instance Public Method ZCL_NB_CLASS->DEPOSIT
* +-------------------------------------------------------------------------------------------------+
* | [--->] AMOUNT TYPE DMBTR
* | [<-()] NEW_BALANCE TYPE DMBTR
* +--------------------------------------------------------------------------------------</SIGNATURE>
method DEPOSIT.

BALANCE = balance + AMOUNT.
new_balance = balance.

endmethod.


* <SIGNATURE>---------------------------------------------------------------------------------------+
* | Instance Public Method ZCL_NB_CLASS->MESSAGE
* +-------------------------------------------------------------------------------------------------+
* | [<---] MESS TYPE BAPIRET2
* +--------------------------------------------------------------------------------------</SIGNATURE>
method MESSAGE.

MESS-message = 'Message has been called'.
MESS-type = 'S'.

endmethod.


* <SIGNATURE>---------------------------------------------------------------------------------------+
* | Instance Public Method ZCL_NB_CLASS->SET_BALANCE
* +-------------------------------------------------------------------------------------------------+
* | [--->] AMOUNT TYPE DMBTR
* | [<-()] NEW_BALANCE TYPE DMBTR
* +--------------------------------------------------------------------------------------</SIGNATURE>
method SET_BALANCE.

balance = new_balance.

endmethod.


* <SIGNATURE>---------------------------------------------------------------------------------------+
* | Instance Public Method ZCL_NB_CLASS->WITHDRAW
* +-------------------------------------------------------------------------------------------------+
* | [--->] AMOUNT TYPE DMBTR
* | [<-()] NEW_BALANCE TYPE DMBTR
* | [EXC!] INSUFFICIENT_FUNDS
* +--------------------------------------------------------------------------------------</SIGNATURE>
method WITHDRAW.

IF balance GE AMOUNT.
balance = balance - AMOUNT.
NEW_BALANCE = BALANCE.
ELSE.
RAISE INSUFFICIENT_FUNDS.
ENDIF.
endmethod.
ENDCLASS.

9 REPLIES 9

geert-janklaps
Active Contributor

Hi,

Did you instantiate your object in the module pool before calling a method of the class?

"Classic notation":
DATA: lo_nb_class TYPE REF TO zcl_nb_class.
CREATE OBJECT lo_nb_class.
lo_nb_class->withdraw( 500 ).

"New short syntax":
DATA(lo_nb_class) = NEW zcl_nb_class( ).
lo_nb_class->withdraw( 500 ).

0 Kudos
Dear Greet,
Thanks for reply, I am using the below code for that, Also I have already created the object.

**************************************Init**********************************
  DATA OBJ TYPE REF TO ZCL_NB_CLASS.
  DATA TRANS_BALANCE TYPE DMBTR.
  DATA WITHDRAW TYPE DMBTR.
  DATA BALANCE_LEFT TYPE DMBTR.

START-OF-SELECTION.
  CREATE OBJECT OBJ .
DATA LV_MESSAGE TYPE BAPIRET2.

***********************************Init**************************************

***********************************PAI******************************************
CASE SY-UCOMM.
  WHEN 'DRAW'.
  CALL METHOD  OBJ->deposit( EXPORTING AMOUNT = TRANS_BALANCE

                                  RECEIVING  NEW_BALANCE = BALANCE_LEFT ).
  WHEN 'SET'.
  CALL METHOD  OBJ->set_balance( EXPORTING AMOUNT = TRANS_BALANCE
                                  RECEIVING  NEW_BALANCE = BALANCE_LEFT ).

  WHEN 'DEPO'.
  CALL METHOD  OBJ->withdraw( EXPORTING AMOUNT = TRANS_BALANCE

                                  RECEIVING  NEW_BALANCE = BALANCE_LEFT ).
  WHEN 'MESS'.
  CALL METHOD  OBJ->MESSAGE( IMPORTING MESS = LV_MESSAGE ).

ENDCASE.

************************************PAI***************************************

0 Kudos

This is the error analysis that I am getting...

An exception occurred that is explained in detail below. The exception, which is assigned to class 'CX_SY_REF_IS_INITIAL', was not caught and therefore caused a runtime error. The reason for the exception is: You attempted to use a 'NULL' object reference (points to 'nothing') access a component (variable: "OBJ"). 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.

But I have already created the object above.

matt
Active Contributor

Use the code button in the editor to provide your code in a nicely formatted and readable way, and then I might take the time to help you.

nbitish
Participant
0 Kudos

Hi Greet,

My issue is solved. When I created the object in PAI include then it is working fine. But when I am defining it in program it is telling null object

reference. Does it mean that when I am defining it in the PROGRAM the object is not created for the whole " That is not for the includes " ?

matt
Active Contributor

Runtime knows nothing about includes.

Main program:

... some code ...
INCLUDE sub_program.

With include sub_program

...some more code...

Is EXACTLY the same as

... some code ...
... some more code ...

Includes are just ways of organising source code. That's it. Nothing else.

0 Kudos

Thanks Mathew,

So could you please tell me at which point should I declare my object then.

PROGRAM  ZNB_CLASS_MOD_POO_IMP.
DATA LV_MESSAGE TYPE BAPIRET2.
DATA OBJ TYPEREFTO ZCL_NB_CLASS.
DATA TRANS_BALANCE TYPE DMBTR.
DATA BAL TYPE DMBTR.
DATA BALANCE_LEFT TYPE DMBTR.
*************************** If I declare the object here then I gets error*************************

INCLUDE ZNB_CLASS_MOD_POO_IMP_STATUO01.
INCLUDE ZNB_CLASS_MOD_PAI.                          " I have declared my object in here, then its' working fine.

Regards.

Probably he wouldn't, because Matthew told you he would help you after you improve the formatting of the code in your initial question. If I had the same question as yours, I would first look at your post and then ran away because it's simply unreadable.

idk what is your whole code but i tried with what you gave here, it run as normal where ever i create object. if you declared it before the include, it will become a global variable which available in your whole program. maybe you should re-organize your code first. beside, you are using sy-ucomm here which you should use okcode instead, FYI.