*
The moose likes Beginning Java and the fly likes NullPointer exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "NullPointer exception" Watch "NullPointer exception" New topic
Author

NullPointer exception

Adam Blais
Greenhorn

Joined: Dec 02, 2004
Posts: 27


This code compiles fine but when I try to create an object I get a NullPointer Exception for the indicated line. I don't see what the problem is, since c[0][0] = 1 seems to make sense.
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
I assume you declared the array, otherwise you would have a compiler error. However, it looks like you didn't initialize the array using the "new" keyword. When you initialize the array depends on how you determine its size. Typically, you can initialize it when you declare it, like this:

If you need more help than this example gives, please post the declaration for this array.

HTH

Layne
[ January 20, 2005: Message edited by: Layne Lund ]

Java API Documentation
The Java Tutorial
Adam Blais
Greenhorn

Joined: Dec 02, 2004
Posts: 27
The array declaration is:

private int[][] c;

It's supposed to be a ragged array for calculating binomial coefficients, so the length varies depending on the value the constructor is given.
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Then you will need to initialize the array in your constructor with something like this:

If each row has a different number of columns (or vice versa), you will need to come up with a slightly more complex initialization, probably with a for loop.

The point is you HAVE to initialize the array somehow. You are getting a NullPointerException because the array is a member variable and defaults to null unless you initialize it explicitly.

HTH

Layne
[ January 20, 2005: Message edited by: Layne Lund ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: NullPointer exception