aspose file tools*
The moose likes Beginning Java and the fly likes NullPointer Exception...Why? 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...Why?" Watch "NullPointer Exception...Why?" New topic
Author

NullPointer Exception...Why?

Nikhil Sun
Ranch Hand

Joined: Nov 13, 2005
Posts: 108
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

Joined: Nov 24, 2005
Posts: 14688
    
  16

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 ?


[My Blog]
All roads lead to JavaRanch
Nikhil Sun
Ranch Hand

Joined: Nov 13, 2005
Posts: 108
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

Joined: Nov 24, 2005
Posts: 14688
    
  16

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

Joined: Nov 13, 2005
Posts: 108
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

Joined: May 31, 2007
Posts: 3259
your column length is zero. Try this



500+ Java Interview Questions and Answers | Java job hunting know how & Java resumes
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

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

Joined: Jun 03, 2007
Posts: 19
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


SCJP 1.4, SCWCD 1.4<br />email:mobject@gmail.com
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: NullPointer Exception...Why?