Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Recieving an Exception Error

 
Michael Coco
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 378
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you initialized the OriginalList variable?
 
Michael Coco
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry forgot this

public static int[] OriginalList;

have it inside my public class
 
Gamini Sirisena
Ranch Hand
Posts: 378
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok. You have declared the variable. Where are you initializing it?
 
Michael Coco
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49411
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49411
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic