Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: java.lang.NullPointerException - Help

  1. #1
    Member
    Join Date
    Nov 2009
    Posts
    57
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Default java.lang.NullPointerException - Help

    Im trying to run this code i have written but cannot see whats wrong with it. i get the java.lang.NullPointerException error when running it.

    Basically what im trying to do is to move a background x position by using a new method i have created in Dice class (moveBackground).

    The code i am running is

    di.moveBackground(); where di is the object name

    can any one help as i havent got a clue as im new to java

    Class Called Dice only part of code is pasted here
    private Square background;
     
     public Square getBackground()
       {
          return background;
       }
     
      public void moveBackground()    /** this is what method i am trying to run */
         {
            background.setXPos(75);
          }


    Square class
     public void setXPos(int x)
       {
          this.xPos = x;
          this.update();
       }

    Thanks in advance

  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: java.lang.NullPointerException - Help

    Is the background variable instantiated prior to calling moveBackground, for example in the constructor or using a setter?

  3. #3
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: java.lang.NullPointerException - Help

    yah i think the object background is not properly instantiated... i think thats the object that the compiler is reffering to..


    try to make a constructor that will instantiate the object background as your new object for the square class
     
     public <Class Name> () {
     
             background  = new Square();
     }
    Last edited by chronoz13; November 28th, 2009 at 09:22 PM.

  4. #4
    Member
    Join Date
    Nov 2009
    Posts
    57
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Default Re: java.lang.NullPointerException - Help

    not sure what you mean all my code is below


    To get a dice with 1 spot i run the code:
    Square s = new Square();
    Circle c1 = new Circle();
    Circle c2 = new Circle();
    Circle c3 = new Circle();
    Circle c4 = new Circle();
    Circle c5 = new Circle();
    Circle c6 = new Circle();
    Circle c7 = new Circle();
    Dice di = new Dice(s, c1, c2, c3, c4, c5, c6, c7);

    then the idea would be to run:
    di.moveBackground();

    to move the square created above to the right by 75
    Last edited by mds1256; December 3rd, 2009 at 07:13 PM.

  5. #5
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: java.lang.NullPointerException - Help

    You never set or create the background variable. Look at the constructor of the class Dice, there should be something like
    background = bg;
    or
    background = new Square();

  6. The Following 2 Users Say Thank You to copeg For This Useful Post:

    JavaPF (November 30th, 2009), mds1256 (November 30th, 2009)

  7. #6
    Member
    Join Date
    Nov 2009
    Posts
    57
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Default Re: java.lang.NullPointerException - Help

    Quote Originally Posted by copeg View Post
    You never set or create the background variable. Look at the constructor of the class Dice, there should be something like
    background = bg;
    or
    background = new Square();
    brilliant. thanks very much for this

    Really appreciate it

Similar Threads

  1. Replies: 16
    Last Post: August 27th, 2010, 03:30 PM
  2. Please help! Exception in thread "main" java.lang.NullPointerException
    By Arutha2321 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 18th, 2009, 02:25 AM
  3. Replies: 2
    Last Post: November 3rd, 2009, 06:28 AM
  4. Getting "AWT-EventQueue-0" java.lang.NullPointerException error
    By tryingtoJava in forum AWT / Java Swing
    Replies: 9
    Last Post: September 21st, 2009, 10:46 PM
  5. [SOLVED] Facing java error "cannot be applied to (java.lang.String)"
    By tazjaime in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 23rd, 2009, 10:19 AM