Results 1 to 4 of 4

Thread: Error NullPointerException in my program

  1. #1
    Join Date
    Nov 2009
    Posts
    333

    Error NullPointerException in my program

    Hi
    Do not know what is wrong with the code it is given a java.lang.NullPointerException error when running it. Can any one help because I have no clue what is going wrong with this code. Thanks in advance. I have posted my code below.
    Code:
    private sqr background;
    
     public sqr getbg()
       {
          return background;
       }
    
      public void movebg()    /** this is what method i am trying to run */
         {
            background.setXPos(75);
          }
    This is the sqr class
    Code:
    public void setXPos(int x)
       {
          this.xPos = x;
          this.update();
       }

  2. #2
    Join Date
    Jan 2008
    Posts
    1,521

    Re: Error NullPointerException in my program

    Hello,
    I think there is a problem in object background. It is not properly instantiated. Think that it the object the compiler is referring to. To make the constructor instantiate the object background. Just try this part of code.
    Code:
    public <Class Name> () {
               
             background  = new Square();
     }
    If any problem do post back.

  3. #3
    Join Date
    Nov 2009
    Posts
    333

    Re: Error NullPointerException in my program

    Hi
    Thanks for the reply and for the code part.
    Code:
    public <Class Name> () {
               
             background  = new Square();
     }
    I have tried it but not sure what you are trying to say in the code. Any more explanation will be appreciated. Here I use this part of code to get a dice with one spot.
    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);
    Any explanation. Thanks again.

  4. #4
    Join Date
    Jan 2008
    Posts
    1,521

    Re: Error NullPointerException in my program

    Hi
    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);
    I observed what you have posted in the code. The mistake you have done is that you have not created the background variable. Just take a look at the constructor of the class Dice. If I am not wrong then there should be something like this
    Code:
    background = bg;
    or even you can use this
    Code:
    background = new Square();

Similar Threads

  1. Replies: 3
    Last Post: 28-01-2011, 01:57 PM
  2. Replies: 5
    Last Post: 15-12-2010, 07:20 PM
  3. What is java.lang.NullPointerException
    By Logan 2 in forum Software Development
    Replies: 5
    Last Post: 27-02-2010, 05:30 AM
  4. Could NullPointerException cause some problems
    By Happy46 in forum Software Development
    Replies: 4
    Last Post: 09-11-2009, 06:17 PM
  5. NullPointerException when using array of string in Java
    By Sean J in forum Software Development
    Replies: 2
    Last Post: 25-04-2009, 08:14 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,719,173,881.01199 seconds with 17 queries