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

Recieving an Exception Error

Michael Coco
Greenhorn

Joined: Sep 15, 2009
Posts: 5
First post here and am I am using Eclipse and am receiving the following error:
Exception in thread "main" java.lang.NullPointerException
at Hw1.createrandoms(Hw1.java:26)
at Hw1.main(Hw1.java:16)


By the way the errors at 8 and 18 below

I know java fairly well, but for whatever reason i cannot figure out what im doing wrong.

My code


Thanks in advance
Gamini Sirisena
Ranch Hand

Joined: Aug 05, 2008
Posts: 347
Have you initialized the OriginalList variable?
Michael Coco
Greenhorn

Joined: Sep 15, 2009
Posts: 5
sorry forgot this

public static int[] OriginalList;

have it inside my public class
Gamini Sirisena
Ranch Hand

Joined: Aug 05, 2008
Posts: 347
Ok. You have declared the variable. Where are you initializing it?
Michael Coco
Greenhorn

Joined: Sep 15, 2009
Posts: 5
I guess i havent initialized OriginalList... can you give me some hints on how? I tried int[] OriginalList = new int[N];

im much better with vb and c#, sorry
Michael Coco
Greenhorn

Joined: Sep 15, 2009
Posts: 5
Sorry it seems as if i move

before i call createrandoms(N-1) it will work properly. Can someone tell me why? why cant i initialize after i call create randoms?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36508
    
  16
You will escape the NullPointerException wherever your initialisation is, provided it is before you try to use the reference. It is probably better design to initialise every instance field in your constructor.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36508
    
  16
And why have you got all your methods static? You have got too much inside your main method. You ought to be creating an object, then calling instance methods on it.
Michael Coco
Greenhorn

Joined: Sep 15, 2009
Posts: 5
Campbell Ritchie wrote:And why have you got all your methods static? You have got too much inside your main method. You ought to be creating an object, then calling instance methods on it.


No real reason, just how i set it up. When it tried calling createrandoms, i had to create it as static or i kept receiving an error
 
Consider Paul's rocket mass heater.
 
subject: Recieving an Exception Error
 
Similar Threads
Question on java.util.Random
Triangular Output
random number
compilation errors: possible loss of precision, incompatible types
Increasing Array Size