aspose file tools*
The moose likes Beginning Java and the fly likes Java Help:  NullPointerException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Java Help:  NullPointerException" Watch "Java Help:  NullPointerException" New topic
Author

Java Help: NullPointerException

Levi Lunde
Greenhorn

Joined: Jan 30, 2009
Posts: 5
My assignment requires me to roll a pair of dice for a user input # of times and print a histogram of the results. I have the rolls down and 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. I'm just trying to get this to work right now then I'll actually put in the code for the histogram. Here is my code:



Balu Sadhasivam
Ranch Hand

Joined: Jan 01, 2009
Posts: 874

Welcome to Javaranch

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 ?

Levi Lunde
Greenhorn

Joined: Jan 30, 2009
Posts: 5
This is my driver with my main method:



And this is the error I get.

Exception in thread "main" java.lang.NullPointerException
at DiceSimulation.printReport(DiceSimulation.java:37)
at LundeLeviProg2_3.main(LundeLeviProg2_3.java:33)
Balu Sadhasivam
Ranch Hand

Joined: Jan 01, 2009
Posts: 874

Can you post the order ofnputs provided.

Is Scanner object
Scanner stdIn = new Scanner(System.in);
is really needed inside the DiceSimulation Class ?

The more appropriate way to intialise "numberOfRolls" is should be passed as parameter from main method "diceSimulation.newSimulation(); "

Moreover
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
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39826
    
  28
Yes, the problem is re-declaring rollCounter as a local variable.

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.
Paul Yule
Ranch Hand

Joined: May 12, 2008
Posts: 229
I believe your NPE is due to using a 1 as the start index of an array. Arrays are zero based. It inserts ok but doesn't insert at 0. When you try to print you have a problem. Or, your roll variable is being set back to null once it leaves the method where it was given a 1 value.


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
Sheriff

Joined: Oct 13, 2005
Posts: 39826
    
  28
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.
Paul Yule
Ranch Hand

Joined: May 12, 2008
Posts: 229
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
Sheriff

Joined: Oct 13, 2005
Posts: 39826
    
  28
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.
Paul Yule
Ranch Hand

Joined: May 12, 2008
Posts: 229
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.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39826
    
  28
It saves you remembering to subtract 1 if your index starts from 1. But you are right about the problem that element 1 is always 0 (or null).
Justin Fox
Ranch Hand

Joined: Jan 24, 2006
Posts: 802
try this code and see if the output is ok:



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.

Justin>


You down with OOP? Yeah you know me!
Levi Lunde
Greenhorn

Joined: Jan 30, 2009
Posts: 5
Thanks for all the ideas guys, I'll plug away at it and see if I can't get it working. My next task is taking all the stored rolls from the array and putting them in a histogram! WooHoooo!!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39826
    
  28
I am not convinced about the if (rollCounter == null) test, which by the way looks like what is called "lazy initialisation." Although it is good syntax, it is bad design; it simply hides an error elsewhere if you do that sort of thing. Also you can only ever get one result out of that print statement: I shall leave it to you to workout what it will print. You can predict that without having to try it.

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.
Levi Lunde
Greenhorn

Joined: Jan 30, 2009
Posts: 5
Ok guys I got it working, histogram and all. The only thing I have a question about is some of text formatting to make the output look a little prettier. Here is the script:

Driver:


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!!

-Levi
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39826
    
  28
That's a lot better.
Find the Formatter class and you get all the details of the % tags. Their use is described there, and more simply here.
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
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:
Levi Lunde
Greenhorn

Joined: Jan 30, 2009
Posts: 5
Thanks for the help guys, and I also appreciate not doing the problem for me and just pointing me in the right direction. I'll probably finish this up tonight. Thanks again!!

-Levi
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39826
    
  28
You're welcome

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
Sheriff

Joined: Oct 13, 2005
Posts: 39826
    
  28
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.
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
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:

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39826
    
  28
Yes, I am sorry; I didn't read it properly.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Java Help: NullPointerException