-2

I'm in trouble with that thing.

public class Money {
          String girl ;
          Money mon ;
      public void sorry (){
          mon.girl = "Isabel" ; //I want to do that (here is basic part) but...
                                //..I get Null Pointer Exception
      }
}

How can i fix that problem? I really need the solve, if you'll help me I appreciate that guys.

3 Answers3

1

reolace your code with

public class Money {
          String girl ;
          **Money mon = new Money();**
          mon.girl = "Isabel" ; //I want to do that (here is basic part) but...
                                //..I get Null Pointer Exception
     }

It was due to not initializing the object Money mon .

Macrosoft-Dev
  • 2,137
  • 1
  • 8
  • 15
0

You have to create an instance of the Money object before set any property.

Money mon = new Money();

Moreover, it seems it's to simplify, but your code looks strange :

Definition

public class Money {
   String girl ;
}

Usage

Money mon = new Money();
mon.girl = "Isabel" ; 
K4timini
  • 691
  • 2
  • 13
  • 32
0

You must need to initialize i.e need to create a instance of object Money before use.

Try to initialize it in this way.

public class Money {
          String girl ;
          Money mon = new Money(); // created instance for Money.

          mon.girl = "Isabel" ; 
     }
Java
  • 2,371
  • 8
  • 44
  • 82
  • 1
    but this is something you really don't want to do. it'll be a recursive call to the Money constructor, crashing the application within seconds – Stultuske May 15 '14 at 09:36