Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

NullPointer exception

 
Adam Blais
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Adam Blais
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic