I am having a Null pointer exception in line 66.
It seems that the line.draw randomInt has a Null pointer exception.
The program is supposed to randomly draw and color lines and rectangles.
I have had this program running before without the MyRect class and it worked fine.
import java.awt.Color;
import java.awt.Graphics;
import java.util.Random;
import javax.swing.JPanel;
import java.awt.*;
public class DrawPanel extends JPanel
{
private Random randomNumbers = new Random();
private MyLine lines[]; // array on lines
private MyRect1 line[] ; // array on rectangle coordinates
// constructor, creates a panel with random shapes
public DrawPanel()
{
setBackground( Color.WHITE );
lines = new MyLine[ 50 + randomNumbers.nextInt( 50 ) ];
// create lines
for ( int count = 0; count < lines.length - 1; count++ )
{
// generate random coordinates
int x1 = randomNumbers.nextInt( 300 );
int y1 = randomNumbers.nextInt( 300 );
int x2 = randomNumbers.nextInt( 300 );
int y2 = randomNumbers.nextInt( 300 );
// generate a random color
Color color = new Color( randomNumbers.nextInt( 256 ),
randomNumbers.nextInt( 256 ), randomNumbers.nextInt( 256 ) );
// add the line to the list of lines to be displayed
lines[ count ] = new MyLine( x1, y1, x2, y2, color );
} // end for
line = new MyRect1[ 50 + randomNumbers.nextInt( 50 ) ];
// create lines
for ( int count = 0; count < lines.length - 1; count++ )
{
// generate random coordinates
int x = randomNumbers.nextInt( 300 );
int y = randomNumbers.nextInt( 300 );
// generate a random color
Color color = new Color( randomNumbers.nextInt( 254 ),
randomNumbers.nextInt( 254 ), randomNumbers.nextInt( 254 ) );
// add the line to the list of lines to be displayed
this.line[ count ] = new MyRect1( x, y, color );
}
} // end DrawPanel constructor
// for each shape array, draw the individual shapes
public void paintComponent( Graphics g )
{
super.paintComponent( g );
for ( MyLine line : lines )<line 66
line.draw( g );
for ( MyRect1 line : this.line )
line.draw( g );
} // end method paintComponent
} // end class DrawPanel
Thanks.
ad3
[2396 byte] By [
ad3a] at [2007-11-26 15:39:42]

It looks like you only create line objects for all positions in lines except the last one.
So when you reach the end of the array, you get a null pointer exception, although it wouldn't be on the line you indicated.
When you post code, please wrap it in [code][/code] tags so it's legible. Also when you get an exception, post the stack trace as well.
thanks paulcw.
So I would need to create a line object for the last position within the array in MyLine... or should I through a boolean that catches the line object that goes out of bounds.
To my understanding this is what is going on
x1=[1,2,3,4]
x2=[1,2,3,4]
y1=[1,2,3,4]
y2=[1,2,3,4,5]
When I run the program the first couple of times this exception is shown.
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 55
at DrawPanel.<init>(DrawPanel.java:55)
at TestDraw.main(TestDraw.java:9)
Press any key to continue . . .
The stack trace listed below happens after a few times of running the program.
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at DrawPanel.paintComponent(DrawPanel.java:67)
at javax.swing.JComponent.paint(JComponent.java:1005)
at javax.swing.JComponent.paintChildren(JComponent.java:842)
at javax.swing.JComponent.paint(JComponent.java:1014)
at javax.swing.JComponent.paintChildren(JComponent.java:842)
at javax.swing.JComponent.paint(JComponent.java:1014)
at javax.swing.JLayeredPane.paint(JLayeredPane.java:559)
at javax.swing.JComponent.paintChildren(JComponent.java:842)
at javax.swing.JComponent.paintWithOffscreenBuffer(JComponent.java:4970)
at javax.swing.JComponent.paintDoubleBuffered(JComponent.java:4916)
at javax.swing.JComponent.paint(JComponent.java:995)
at java.awt.GraphicsCallback$PaintCallback.run(GraphicsCallback.java:21)
at sun.awt.SunGraphicsCallback.runOneComponent(SunGraphicsCallback.java:
60)
at sun.awt.SunGraphicsCallback.runComponents(SunGraphicsCallback.java:97
)
at java.awt.Container.paint(Container.java:1709)
at sun.awt.RepaintArea.paintComponent(RepaintArea.java:248)
at sun.awt.RepaintArea.paint(RepaintArea.java:224)
at sun.awt.windows.WComponentPeer.handleEvent(WComponentPeer.java:254)
at java.awt.Component.dispatchEventImpl(Component.java:4031)
at java.awt.Container.dispatchEventImpl(Container.java:2024)
at java.awt.Window.dispatchEventImpl(Window.java:1778)
at java.awt.Component.dispatchEvent(Component.java:3803)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:463)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchTh
read.java:242)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThre
ad.java:163)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:157)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:149)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)
Press any key to continue . . .
thanks.
ad3
ad3a at 2007-7-8 21:58:07 >

WOW I see what you mean with the !!!This does make it more legible.By the way the JFrame is set at 300, 300.ad3
ad3a at 2007-7-8 21:58:07 >

You're doing this:
for ( int count = 0; count < lines.length - 1; count++ )
{
// generate random coordinates
int x1 = randomNumbers.nextInt( 300 );
...(etc)
lines[ count ] = new MyLine( x1, y1, x2, y2, color );
} // end for
Look at the loop condition. You loop until count equals lines.length - 1.But (lines.length - 1) is the index of the last position in the array. So if there are 50 positions in the array, then they have indices 0 up to and including 49, but you're looping from 0 to 48.
You probably want to do either this:
for ( int count = 0; count <= lines.length - 1; count++ )
or this:
for ( int count = 0; count < lines.length; count++ )
Preferably the latter.
thanks paulcw ;
After playing with the code a bit I finally got the program behaving properly.
I just wanted to say thanks for the help.
here is what I finally went with.
import java.awt.Color;
import java.awt.Graphics;
import java.util.Random;
import javax.swing.JPanel;
public class DrawPanel1 extends JPanel
{
private Random randomNumbers = new Random();
private MyLine1 lines[]; // array on lines
private MyRect1 rects[]; // array on lines
private MyOval1 ovals[]; // array on lines
// constructor, creates a panel with random shapes
public DrawPanel1()
{
setBackground( Color.WHITE );
lines = new MyLine1[ 50 + randomNumbers.nextInt( 50 ) ];
// create lines
for ( int count = 0; count < lines.length; count++ )
{
// generate random coordinates
int x1 = randomNumbers.nextInt( 500 );
int y1 = randomNumbers.nextInt( 500 );
int x2 = randomNumbers.nextInt( 500 );
int y2 = randomNumbers.nextInt( 500 );
// generate a random color
Color color = new Color( randomNumbers.nextInt( 256 ),
randomNumbers.nextInt( 256 ), randomNumbers.nextInt( 256 ) );
// add the line to the list of lines to be displayed
lines[ count ] = new MyLine1( x1, y1, x2, y2, color );
} // end for
rects = new MyRect1[ 50 + randomNumbers.nextInt( 50 ) ];
// create lines
for ( int count = 0; count < rects.length; count++ )
{
// generate random coordinates
int x1 = randomNumbers.nextInt( 500 );
int y1 = randomNumbers.nextInt( 500 );
int x2 = randomNumbers.nextInt( 500 );
int y2 = randomNumbers.nextInt( 500 );
// generate a random color
Color color = new Color( randomNumbers.nextInt( 256 ),
randomNumbers.nextInt( 256 ), randomNumbers.nextInt( 256 ) );
Boolean filled = randomNumbers.nextBoolean();
// add the line to the list of lines to be displayed
rects[ count ] = new MyRect1( x1, y1, x2, y2, color, filled );
} // end for
ovals = new MyOval1[ 50 + randomNumbers.nextInt( 50 ) ];
// create lines
for ( int count = 0; count < ovals.length; count++ )
{
// generate random coordinates
int x1 = randomNumbers.nextInt( 500 );
int y1 = randomNumbers.nextInt( 500 );
int x2 = randomNumbers.nextInt( 500 );
int y2 = randomNumbers.nextInt( 500 );
// generate a random color
Color color = new Color( randomNumbers.nextInt( 256 ),
randomNumbers.nextInt( 256 ), randomNumbers.nextInt( 256 ) );
Boolean filled = randomNumbers.nextBoolean();
// add the line to the list of lines to be displayed
ovals[ count ] = new MyOval1( x1, y1, x2, y2, color, filled );
} // end for
} // end DrawPanel constructor
public String numShapes()
{
String statusText;
statusText = "Lines: " + lines.length + "Ovals: " + ovals.length + "Rects:" + rects.length;
return statusText;
}
// for each shape array, draw the individual shapes
public void paintComponent( Graphics g )
{
super.paintComponent( g );
// draw the lines
for ( MyLine1 line : lines )
line.draw( g );
for ( MyRect1 rect : rects )
rect.draw( g );
for ( MyOval1 oval : ovals )
oval.draw( g );
} // end method paintComponent
} // end class DrawPanel1
thanks again.ad3
ad3a at 2007-7-8 21:58:07 >

