• Post Reply Bookmark Topic Watch Topic
  • New Topic

Why does this give Null Pointer Exception  RSS feed

 
sanpraveen Kumar
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
import java.io.*;

public class B //1
{
public static void main(String arg[]) //2
{
int [][]a=new int[2][];

for(int k=0;k<2;k++)
{a[0][k]=0;}
for(int k=0;k<3;k++)
{a[1][k]=k;}

}
}
 
James Carman
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because you're not initializing the int[] elements of your int[][].
 
fred rosenberger
lowercase baba
Bartender
Posts: 12443
42
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In other words, you've created a REFERENCE to a 2d array, but you don't ever create the elements in it.

it's like saying "Get an envelope. now, take the letter out of the envelope and go to page 3..."
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!