method not working outside of class
I have a 2D-array in a class, and methods that affect the array.
From another class I can call a method to add to the array which works fine.
My method to get a value from an array is:
Code :
public int getValue(int col, int row){
return myArray[col][row];
}
I can call it in that class and it works fine (returns correct int), but from another class I always get a null pointer exception. All other methods work from another class (ie to set a value in the array). Why cant I return the value?
int Number = class.getValue(); -- null pointer.
Re: method not working outside of class
Sounds like your reference to "class" is null. Are you sure you're initializing it?
That's just a guess though. If you want more help, you'll have to provide an SSCCE that demonstrates the problem. That doesn't mean you should post your whole program- just the bare essentials.
Re: method not working outside of class
I can use other methods, for example
Which puts a value in row 2 and then prints the array. These work fine.
It is definitely initialised. I cannot post code.
I have tried
Code :
public int getValue(int col, int row) {
int Number = myArray[col][row];
return Number;
}
Code :
state state;
state.getValue(1,2);
also
state teststate = new state();
teststate.getvaule(1,2);
for a copy of state
But I get the same error. I dont see why that would work if the other one didnt.
Re: method not working outside of class
If you can't post an SSCCE, then we can't really help you. Are you sure the array is initialized?