Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

NullPointer Exception...Why?

 
Nikhil Sun
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Friends I am writing code for an utility.I am posting a snippet of the code where I get NullPointerException at runtime at the line which is in bold.I am passing an input array and storing it in another.

public void setInputValues(Object[] inputs){
int inputsLength = inputs.length;
int row = 0;
int column = 0;
System.out.println("no of inputs"+inputsLength);
Object[][] obj1 = new Object[inputsLength][];
for(int i = 0;i<inputs.length;i++){
obj1[i][column] = inputs[i];
}

Do help me with the cause of the exception.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Object[][] obj1 = new Object[inputsLength][];

You have defined only one size of your multi-dimension array (number of rows). What about the number of columns ?
 
Nikhil Sun
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Satou,I now have given the number of columns too as below:

Object[][] obj1 = new Object[inputsLength][column];

But now at runtime I get ArrayIndexOutOfBounds Exception at the same line ie,

obj1[i][column] = inputs[i];

Kindly help.
[ June 13, 2007: Message edited by: Nikhil Sun ]
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Remember that indexes start at 0, and that you have to instanciate arrays with a size >= 1. What is the maximum number of columns ? You should have something looking like the following :

with currentColumn < maxColumns.
 
Nikhil Sun
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Satou,
I have written as follows:

public void setInputValues(Object[] inputs){
int inputsLength = inputs.length;
int row = 0;
int column = 0;
Object[][] obj1 = new Object[row][column];
for(int i = 0;i<inputs.length;i++){
obj1[row][column] = inputs[i];
}

During the first iteration itself I get the ArrayIndexOutOfBounds exception at the line in bold.Also inputs[0] value ie the first value in inputs array is null.But why am I getting ArrayIndexOutOfBounds exception?
 
arulk pillai
Author
Ranch Hand
Posts: 3388
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
your column length is zero. Try this

 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nikhil,
I think you should try to read something first about multidimensional arrays, to understand the concept. You could try here : http://www.cafeaulait.org/javatutorial.html#xtocid2801933
 
Minh Tran
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hehe the problem is that you declared an empty array (size is 0)

int row = 0;
int column = 0;
Object[][] obj1 = new Object[row][column];

But, you are using the first element (the array's length should be 1)

obj1[row][column] = inputs[i];
//obj1[0][0] is the first element in the array


To correct this, you could use
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic