• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Recieving an Exception Error

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 378
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have you initialized the OriginalList variable?
 
Michael Coco
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Marshal
Posts: 76415
365
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 76415
365
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Don't play dumb with me! But you can try this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic