• Post Reply Bookmark Topic Watch Topic
  • New Topic

NullPointerException and array-things  RSS feed

 
V��r� Tunnus
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've got a problem with one array-thing (again). What I want is this: I've an array, let's say it would be big enough for 10 double values, but I only have x amount of values in it (all the "blank values" I've set to 0.0). I need to do such a system, which always moves those older values so that the newest value is automatically set to the first index of that array (which is array[0]). I'll copy my code here, and explain some more after that:



So what I do in that code? I'll start to check values in the array and when it comes to first index with value 0.0 (that means that there is no value set by user), I'll read the index number to "tyhja". Then I start to move my old values, one by one, as you can see from the code. And after that I finally put the new value "myynti" to this arrays first index (taulukko[0]), so I always have the newest value with the lowest index number.

Ok, then what I get in compiler is this:



There is two separate classes, Markkinointiosasto, which contains the main method and other things, and it should work fine. So I think the problem is in my program. And by the way, if it helps at all, line number 103 in my program is:



So, I'm pretty much stuck with this thing so any help is highly appreciated.
 
Stephen Foy
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

[ May 28, 2006: Message edited by: Stephen Foy ]
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Did you initialize taulukko?
 
V��r� Tunnus
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, I changed that "i = tyhja" to "tyhja = i" but it doesn't help. And yes I've initialized my array in this way:



where jaksojenMaaraMax is any integer value I want (it is declared already earlier in the program). Then I go through all the indexes and set their values as 0.0 (just to make my life easier in one point of the program):



I know that there is sure something unnecessary in the code, but at least it should be initialize the array properly.
 
V��r� Tunnus
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmm... This look like a hard one, I still haven't resolved my problem... any help is appreciated.
 
V��r� Tunnus
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok now I know where my problems in this program are. So, all those errors etc happen in this method (this method adds new valuas to my array after it has moved all previous values to next indexes so that new valua can be added to index 0):

public void lisaaMyynti(double myynti) {
int tyhja = 0;

for (i = 0; i > jaksojenMaksimimaara; i++) {
taulukko[i] = apumuuttuja;
if (apumuuttuja == 0.0) {
tyhja = i;
break;
}
}

for(j = tyhja; j < 1; j--) {
taulukko[j] = taulukko[(j + 1)];
}

myynti = taulukko[0];
}
}


And I initialize my array like this (I also put value 0.0 to every index):

public Myyntitilasto(int jaksojenMaaraMax, String tuote) {
double[] taulukko = new double[jaksojenMaaraMax];
int i = 0;
jaksojenMaaraMax = jaksojenMaksimimaara;

for (i = 0; i > (jaksojenMaksimimaara); i++) {
taulukko[i] = 0.0;
}
}


So what's the problem with my code. I get nullPointerException in that section which should add new value to my array.
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i > jaksojenMaksimimaara;

It's all Finnish to me. But you will never get it to finish; surely it should read

CR
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
. . . and you ought to write i < jaksojenMaksimimaaraArray.length; rather than putting a number in.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!