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 null pointer and paint dirty regions error

  1. #1
    Member
    Join Date
    Aug 2011
    Posts
    86
    My Mood
    Lurking
    Thanks
    16
    Thanked 4 Times in 4 Posts

    Default java null pointer and paint dirty regions error

    I have a project for class. It compiles but does not run and I just cannot understand why.

    It is a not so simple starscape.

    my folder set up is StarScape/Star/

    I have my main applet in StarScape, and a package called star in star.

    This is the command I use to compile it:
    C:\JSource\StarScape>javac StarScape.java Star/Star.java

    code for StarScape.java in the starscape folder:

    // starscape.java
    // by Jon Crawford
    import Star.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.awt.geom.*;
     
    public class StarScape extends JApplet 
    {
      final int maxStars = 100;
      String key = "";
      int index;
      int myX;
      int myY;
      int myRadius;
      int myLayer;
      int rectX;
      int rectY;
      int rectXX;
      int rectYY;
     
      public void paint(Graphics g)
      {
        Star stars[] = new Star[maxStars];//make 100 star objects
        g.setColor(Color.black);
        g.fillRect(0,0,600,600);// make the background black
        do
        {
          for (index = 0; index < maxStars; index++) 
          {
            stars[index].getStarX(myX);
            stars[index].getStarY(myY);
            stars[index].getStarRadius(myRadius);//get data for star at index number
            rectX = myX - myRadius + 399;//myX and myY are based on a 0,0 in the center grid
            rectY = myY - myRadius + 299;//as well as converting to a 0,0 at top left grid
            rectXX = myX + myRadius + 399;//these lines setup the boundaries of
            rectYY = myY + myRadius + 299;//a rectangular star
            g.setColor(Color.black);
            g.fillRect(rectX, rectY, rectXX, rectYY);//erase old star
            stars[index].moveStar();//move the star
            stars[index].getStarX(myX);
            stars[index].getStarY(myY);
            stars[index].getStarRadius(myRadius);//get data agian because star was moved
            stars[index].getStarLayer(myLayer);//layer just denotes shade of star. 
            if (myLayer == 1) 
            {
              g.setColor(Color.white);
            }else if (myLayer == 2) 
            {
              g.setColor(Color.lightGray);
            }else if (myLayer == 3) 
            {
              g.setColor(Color.darkGray);
            }else 
            {
              g.setColor(Color.white);//these set color based on layer
            }
            rectX = myX - myRadius + 399;
            rectY = myY - myRadius + 299;
            rectXX = myX + myRadius + 399;
            rectYY = myY + myRadius + 299;//set up the new star I mean rectangle
            g.fillRect(rectX, rectY, rectXX, rectYY);//draw star
          }//end for
        }while (key == "");//should implement key listener
      }//end paint
    }//end starscape

    Code for star.java in the star folder:
    // star.java
    // by Jon Crawford
    package Star;
     
    import java.util.*;
     
    public class Star {
     
      private final int starSpeed = 4;
      private final int starRadius = 1;
      private final int starSteps = 0;
      private final int[] invalidValues = new int[]{ -399, -299, 399, 299 };
      private int xPos;
      private int yPos;
      private int starAngle;
      private int starLayer;
      private int starField[] = new int[8];
      Random rand = new Random();
     
      Star(){
        xPos = rand.nextInt(399) - 199;//star starts in a small square
        yPos = rand.nextInt(299) - 149;//in the center
        starAngle = (int)(Math.sqrt(xPos * xPos)+(yPos * yPos));// calculate the hypotenuse   
        starLayer = rand.nextInt(3);//random layer
        starField[0] = xPos;//set data to star's arrary
        starField[1] = yPos;//this is for current position
        starField[2] = xPos / starAngle * starSpeed;//these set the next position
        starField[3] = yPos / starAngle * starSpeed;
        starField[4] = starLayer;//back, mid, or foreground layer
        starField[5] = starRadius;//star starting size
        starField[6] = starSpeed;//the starting speed
        starField[7] = starSteps;//star has taken 0 steps, every 5 steps star grows and speeds up
        starField[8] = starAngle;//store the hypotenuse
      }//end default constructor
     
      public void moveStar() 
      {//set or mutator method
        if(starField[2] > invalidValues[0] && starField[3] > invalidValues[1] && starField[2] < 
     
    invalidValues[1] && starField[3] < invalidValues[3])
        {//if star is not to close to the edge
          starField[0] = starField[2];//next position becomes the curent position 
          starField[1] = starField[3];       
          starField[2] = starField[0] / starField[8] * starField[6];//new next position
          starField[3] = starField[1] / starField[8] * starField[6];
          starField[7]++;//increment steps
          if (starField[7] / 5 == (int)(starField[7] / 5) && starField[5] < 15)
          {//if star has taken 5 steps and is not larger than 30x30
            starField[5]++;//increment size
            starField[6]++;//increment speed
          }//end if      
        }else
        {//if star is too close to the edge
          xPos = rand.nextInt(399) - 199;//set new starting position
          yPos = rand.nextInt(299) - 149;
          starAngle = (int)(Math.sqrt(xPos * xPos)+(yPos * yPos));//calculate new hypotenuse
          starLayer = rand.nextInt(3);//new random layer
          starField[0] = xPos;//set data to star's array
          starField[1] = yPos;//same as default constructor
          starField[2] = xPos / starAngle * starSpeed;
          starField[3] = yPos / starAngle * starSpeed;
          starField[4] = starLayer;
          starField[5] = starRadius;
          starField[6] = starSpeed;
          starField[7] = starSteps;
          starField[8] = starAngle;
        }//end if
      }//end moveStar
     
      public int getStarX(int myX) 
      {//get method
        return starField[0];
      }//end getStarX
     
      public int getStarY(int myY) 
      {//get method
        return starField[1];
      }//end getStarY
     
      public int getStarRadius(int myRadius) 
      {//get method
        return starField[5];
      }//end getStarRadius
     
      public int getStarLayer(int myLayer) 
      {//get method
        return starField[4];
      }//end getStarLayer
     
    }//end class star

    the runtime errors i get:
     
    C:\JSource\StarScape>javac StarScape.java Star/Star.java
     
    C:\JSource\StarScape>appletviewer starscape.html
    Exception in thread "AWT-EventQueue-1" java.lang.NullPointerException
            at StarScape.paint(StarScape.java:32)
            at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:796)
     
            at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:713)
     
            at javax.swing.RepaintManager.seqPaintDirtyRegions(RepaintManager.java:6
    93)
            at javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(System
    EventQueueUtilities.java:125)
            at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
            at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:641)
            at java.awt.EventQueue.access$000(EventQueue.java:84)
            at java.awt.EventQueue$1.run(EventQueue.java:602)
            at java.awt.EventQueue$1.run(EventQueue.java:600)
            at java.security.AccessController.doPrivileged(Native Method)
            at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessCo
    ntrolContext.java:87)
            at java.awt.EventQueue.dispatchEvent(EventQueue.java:611)
            at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThre
    ad.java:269)
            at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.
    java:184)
            at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThre
    ad.java:174)
            at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
     
            at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
     
            at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
    Exception in thread "AWT-EventQueue-1" java.lang.NullPointerException
            at StarScape.paint(StarScape.java:32)
            at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:796)
     
            at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:713)
     
            at javax.swing.RepaintManager.seqPaintDirtyRegions(RepaintManager.java:6
    93)
            at javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(System
    EventQueueUtilities.java:125)
            at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
            at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:641)
            at java.awt.EventQueue.access$000(EventQueue.java:84)
            at java.awt.EventQueue$1.run(EventQueue.java:602)
            at java.awt.EventQueue$1.run(EventQueue.java:600)
            at java.security.AccessController.doPrivileged(Native Method)
            at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessCo
    ntrolContext.java:87)
            at java.awt.EventQueue.dispatchEvent(EventQueue.java:611)
            at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThre
    ad.java:269)
            at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.
    java:184)
            at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThre
    ad.java:174)
            at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
     
            at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
     
            at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
     
    C:\JSource\StarScape>

    I know that there may be better ways to do this, but it is for class. The rules for the project:

    1)create a package or class for stars. Stars should have properties like location, size, speed, angle, color.
    2)at least 3 colors, basically three starfields/layers on top of one another. Should run as one starfield not three, just look like 3.
    2)must have a moveStar setter method; stars must grow and accelerate toward edge of screen as part of the moveStar Method.
    3)must have get methods for x and y positions, color/layer, and size
    4)main applet must call the methods, and draw the starfield (painting must be done in the main applet. Must use rectangle, other shapes have not been taught yet.
    5)keyListener to be learned later, this must run until user exits the window. Even if I know keyListener it is NOT to be used, Teach does not want anyone to be far ahead of anyone else.
    6)can use vector math if known (I do, can only use math.sqrt since math.hypot has not been gone over in class) else can move star on the four diagonals (i.e. x-1,y-1 AND x-1,y+1 AND x+1,y+1 AND x+1,y-1)

    Please help. Code as been commented for easy read.
    Last edited by Spidey1980; August 16th, 2011 at 04:12 PM. Reason: grammatical


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: java null pointer and paint dirty regions error

    appletviewer starscape.html
    Exception in thread "AWT-EventQueue-1" java.lang.NullPointerException
    at StarScape.paint(StarScape.java:32)
    There is a null variable at line 32 in your code. Find that variable and back track to see why it has a null value and then change the code so it has a valid value.

       Star stars[] = new Star[maxStars];//make 100 star objects
    Here you define an array to hold some Star objects. Now you need to fill the array.
    This definition should be outside of the paint method so other code can fill it with the needed Star objects.

  3. The Following User Says Thank You to Norm For This Useful Post:

    Spidey1980 (August 16th, 2011)

  4. #3
    Member
    Join Date
    Aug 2011
    Posts
    86
    My Mood
    Lurking
    Thanks
    16
    Thanked 4 Times in 4 Posts

    Default Re: java null pointer and paint dirty regions error

    so I just tried this:
        Star stars[] = new Star[maxStars];//make 100 star objects
        for (index = 0; index < maxStars; index++)
        {
          stars[index] = new Star[];//fill array with stars
        }//end for

    and it tells me that
     
    C:\JSource\StarScape>javac StarScape.java Star/Star.java
    StarScape.java:28: Star() is not public in Star.Star; cannot be accessed from ou
    tside package
          stars[index] = new Star();//fill array with stars
                         ^
    1 error
     
    C:\JSource\StarScape>

    lol so i'm getting closer. Thank you so much for your help.


    edit: yup I forgot the "public void" infront of star() in my package. This issue is solved, but now I have another issue with this code that does not fit in this thread.

    SOLVED so far
    Last edited by Spidey1980; August 16th, 2011 at 08:34 PM.

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: java null pointer and paint dirty regions error

    Yes you could use a loop to fill the array.
    In the loop assign each element a new Star object.

  6. The Following User Says Thank You to Norm For This Useful Post:

    Spidey1980 (August 16th, 2011)

  7. #5
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: java null pointer and paint dirty regions error

    Star(){

    If you do not provide an access modifier it will use the default of package private. This means that only classes in the same package can access it.
    Improving the world one idiot at a time!

  8. The Following User Says Thank You to Junky For This Useful Post:

    Spidey1980 (August 16th, 2011)

  9. #6
    Member
    Join Date
    Aug 2011
    Posts
    86
    My Mood
    Lurking
    Thanks
    16
    Thanked 4 Times in 4 Posts

    Default Re: java null pointer and paint dirty regions error

    null pointer and dirty regions issue SOLVED Thank you all for your help.

Similar Threads

  1. Null Pointer exception
    By Demetrius82 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: June 2nd, 2011, 07:32 PM
  2. Array of strings Null Pointer error
    By FredrichGauss in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 3rd, 2011, 12:43 AM
  3. JComboBox null pointer Exception error
    By F_Arches in forum AWT / Java Swing
    Replies: 2
    Last Post: November 29th, 2009, 02:32 PM
  4. Null Pointer Exception
    By MysticDeath in forum Exceptions
    Replies: 2
    Last Post: October 24th, 2009, 01:49 PM
  5. Getting Null Pointer Exception in runtime
    By RoadRunner in forum Exceptions
    Replies: 1
    Last Post: April 26th, 2009, 01:21 PM