Where did you create the object and call the method ? Please post the main method too. The one and only reason for the null pointer exception is that you are trying to invoke methods on the object which is 'null' .
I can print the results if they are in their own method but when I try to call the results of the array from my printReport method I get a null pointer exception
Are you calling the methods with the same object ?
Is Scanner object
is really needed inside the DiceSimulation Class ?
Scanner stdIn = new Scanner(System.in);
The more appropriate way to intialise "numberOfRolls" is should be passed as parameter from main method "diceSimulation.newSimulation(); "
public int rollGenerator() int rollCounter = new int[numberOfRolls + 1];
Here you are creating a new local int array , whihc has to be stored , but the returning int array, is nowhere stored
And when you print
You have several other logic errors, I am afraid. When I corrected that error, your app ran and printed out 0 regardless.
What you want is to know how many rolsl return 11, 12, 13, 14 up to 66, but you are doing it on totals, so 15 24 33 42 and 51 are equivalent to one another, as all being 6. You therefore want a 12-member array each member representing the values 1 to 12 (or maybe a 13-member array, then you don't need to bother about subtracting 1 from the roll, and forget about 4 bytes of memory unused for 0.
You need to print every value in the array, so return the array, not a single int.
Write down with a pencil, what the initial values in an array are, then work out how to increment element 6 if you throw 15 24 or 33.
A couple things you may want to think about. It looks like your program is going to become troublesome with the amount of things you have to keep up with globally. You may want to re-think using an Array as your data structure because it seems tiresome to me to have to keep up with all those indexes and future indexes and have to resize your array every time. I would use some type of collection.
Then when they enter the number in you can call more reusable methods and you won't need to keep up with all those global variables. Try to keep methods to doing 1 job if it's at all possible.
your print would be the same for your collection no matter the size and it might be easier to keep up with, YMMV.
hope that helps.
Campbell Ritchie wrote:No, I don't think starting at 1 in an array is the cause of the NPE. It is an int array, so it cannot contain nulls, only 0s. The NPE was caused by the array itself being null.
Good idea about the rolling method.
ah, you're right. I should take the time to test other's code.
Still may want to watch out for that though because
will technically work as long as you print from index 1 as well. The first index will always be zero. Sort of a weird bug that won't affect anything if you don't do anything with it.
But, yea the NPE is from the array being null when you call the print.
Campbell Ritchie wrote:You might get ArrayOutOfBoundsExceptions from that sort of loop, but in this case, I would agree with the trick of making the array "one too big." That has been done correctly, too.
you would make an array 1 too big so that you can start your array at the 1 index? Why? Anytime you do anything with it or anyone handles that array from then on they would need to remember that the very first index never holds anything.
I think you were getting the NPE from when you call print without actually ever
doing a new roll or additional rolls, Thats why i added the if (rollCounter == null)...
in the printreport() function..
also I changed the index in the for loop in rollgenerator to:
for (int roll=0; roll><numberOfRolls; roll++)
the roll block variable in your for loop is shadowing your
public instance variable roll also. that was part of the printing rollCounter[rol] problem. (might want to change that)
and when i print i get a value outputted instead of a zero now.
Actually, I think you will always get a different Exception from that block
Now you have worked out what is causing the Exception, you need to work out how to get the values into the array in the first place. Note the discussion between Paul and myself last night about how many members you need in your array.
and here is the driven program:
The output is functional but I was wondering how to line up rolls, frequency, and graph with the results? Or if it's even possible? Thanks again guys!!
Find the Formatter class and you get all the details of the % tags. Their use is described there, and more simply here.
Levi Lunde wrote:
The output is functional but I was wondering how to line up rolls, frequency, and graph with the results? Or if it's even possible?
Yes, it's possible. I'd use a format string and the System.out.printf(...) method. Here's a possible format String:
I hope it's not too late, but can I suggest you delete the //************************************** bits. They are not a customary Java format (at least I think not) and some people (myself included) find them hard on the eye, and they might lose you marks.
It's %s not s% in a format String. You can inline the format String as the first argument for printf, if you prefer. And the Java Tutorial link I posted earlier will have told you to use %n rather than \n in a format String.
Campbell Ritchie wrote:I think you will need to go back to that Java Tutorials page and look at format Strings again; the example you showed won't work, I am afraid.
I think that you were referring to my example, correct?
Yes, I am aware that it is %4S, but if you look closely at this:
You'll see that it becomes "%5s%11s%22s\n" which is a correct and functioning format String, and of course I tested my code before posting it as always.
You are right though that %n is better than \n.
for instance run this to see the format string does work: