Results 1 to 2 of 2
  1. #1
    arielj is offline Member
    Join Date
    Nov 2012
    Posts
    1
    Rep Power
    0

    Default trying to draw up space invader "bunkers" but keep getting an NullPointerException

    hey! I'm writing up some java code and keep getting the same error. I'm working with two classes here, first one is the bunker class, the second one is the testing class:
    it has something to do with the array or the stdDraw class calling on the arrays.
    Any ideas why I keep getting the NullPointerException in the location: test.java.40 ?

    public class Bunker
    {
    public double x, y;
    public boolean isNotDestroyed;

    public Bunker()
    {
    this.x = x;
    this.y = 0.1;
    this.isNotDestroyed = true;
    }


    }


    public class test
    {
    public static void main(String[] args)
    {
    Bunker[] bunkers = new Bunker[100];
    double z = 0.01;
    int t = 1;

    for(int i = 1; i<25; i++){

    Bunker newbunker = new Bunker();

    if (t < 20)
    {
    newbunker.x =(t*0.01);
    bunkers[i] = newbunker;
    t++;

    }

    else
    {
    newbunker.x = 0.1;
    newbunker.y += 0.1;
    t = 1;
    }

    System.out.println("x"+i+" = " + newbunker.x);
    System.out.println("y"+i+" = " + newbunker.y);
    System.out.println("t =" + t);


    }



    for(int i = 1; i < 25; i++)
    {
    Bunker bunker = bunkers[i];
    StdDraw.picture(bunker.x, bunker.y, "23.png", 0.007, 0.007);

    }
    }
    }

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,839
    Rep Power
    8

    Default Re: trying to draw up space invader "bunkers" but keep getting an NullPointerExceptio

    When posting code, please use the code tags to preserve formatting. Also, code should be in the form of an SSCCE, otherwise we can't run it.

    What code is on the line that throws the NPE? What values are you dereferencing? One of them is null- which one is it? Use a debugger or some print statements to figure that out.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Replies: 3
    Last Post: 10-30-2012, 03:06 PM
  2. Replies: 5
    Last Post: 12-21-2011, 06:44 PM
  3. Replies: 2
    Last Post: 10-03-2011, 06:22 PM
  4. How to remove "unused" space in JMenu
    By nowy in forum AWT / Swing
    Replies: 13
    Last Post: 12-07-2009, 03:27 PM
  5. Replies: 1
    Last Post: 10-20-2008, 07:35 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •