By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,242 Members | 1,084 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,242 IT Pros & Developers. It's quick & easy.

Can someone tell me whats wrong with my code, it gives me the following error

thatos
100+
P: 105
The code is below
Expand|Select|Wrap|Line Numbers
  1. import java.io.*;
  2. class Storage{
  3.     int num = 0;
  4.     String name = null;
  5. }
  6.  
  7. public class gift1 {
  8.     public static void main(String args[]) throws IOException{
  9.  
  10.         BufferedReader in = new BufferedReader(new FileReader("e:/gift1.in"));
  11.         PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("e:/gift1.out")));
  12.         int number = Integer.parseInt(in.readLine());
  13.         Storage array[] = new Storage[number];
  14.         String inp;
  15.         for(int i =0;i<number;i++){
  16.             inp = in.readLine();
  17.             array[i].name = inp;
  18.         }
  19.  
  20.         for (int i = 0;i<number;i++){
  21.             String person = in.readLine();
  22.             String content[] = in.readLine().split(" ");
  23.             int amount = Integer.parseInt(content[0]);
  24.             int divider = Integer.parseInt(content[1]);
  25.             int givenamount;
  26.             if (divider == 0){
  27.                 givenamount = 0;
  28.             }
  29.             else{
  30.                 givenamount = amount/divider;
  31.             }
  32.             for(int j=0;j<divider;j++){
  33.                 String given = in.readLine();
  34.                 for(Storage k:array){
  35.                     if (given.equals(k.name)){
  36.                         k.num += givenamount;
  37.                     }
  38.                 }
  39.  
  40.             }
  41.             for(Storage j:array){
  42.                 if (j.name.equals(person)){
  43.                     j.num += amount - divider*givenamount;
  44.                 }
  45.             }
  46.  
  47.         }
  48.         for (Storage i:array){
  49.             out.write(i.name+" "+i.num+"\n");
  50.         }
  51.         out.close();
  52.         System.exit(0);    
  53.     }
  54.  
  55. }
  56.  
This is the error which it gives me
Expand|Select|Wrap|Line Numbers
  1. Exception in thread "main" java.lang.NullPointerException
  2.     at gift1.main(gift1.java:23)
  3.  
Apr 29 '08 #1
Share this Question
Share on Google+
11 Replies


10K+
P: 13,264
When you do
Expand|Select|Wrap|Line Numbers
  1.  Storage array[] = new Storage[number];
every element in the array is null.
So when you proceed with array[i].name = ... you are essentially doing
null.name = ... Now always remember that you will always get null pointer exception if you dot a null.
You need to intialize all the objects in the array first before you start dereferencing (dotting) them.
Apr 29 '08 #2

Expert 10K+
P: 11,448
In the following line you allocate an array that can hold Storage objects:

Expand|Select|Wrap|Line Numbers
  1.         Storage array[] = new Storage[number];
  2.  
... but none of the elements is pointing to a Storage object yet, they all point to
nothing, i.e. null; hence the runtime exception.

kind regards,

Jos
Apr 29 '08 #3

Expert 10K+
P: 11,448
Darn, too slow again; are people peeking over my shoulder when I type a reply?

kind regards,

Jos
Apr 29 '08 #4

10K+
P: 13,264
Darn, too slow again; are people peeking over my shoulder when I type a reply?

kind regards,

Jos
Not many people, just me.
Apr 29 '08 #5

thatos
100+
P: 105
So how can I solve the problem please help
Apr 29 '08 #6

10K+
P: 13,264
So how can I solve the problem please help
You need to intialize all the objects in the array first before you start dereferencing (dotting) them.
. .
Apr 29 '08 #7

thatos
100+
P: 105
How can I initialize them? What I know is that those names cannot be more than ten and must be greater than or equals two.


. .
Apr 29 '08 #8

thatos
100+
P: 105
How can I initialize since I just know that the number of peoples in the group are greater or equals 2 but may not be more than 10?
. .
Apr 29 '08 #9

100+
P: 539
How can I initialize since I just know that the number of peoples in the group are greater or equals 2 but may not be more than 10?
Will you post your gift1.in and gift1.out file?

regards,
sukatoa
Apr 30 '08 #10

Expert 100+
P: 849
If it can't be more than 10, initialize the array to 10 elements. Then set as many as you can, and anytime you access from the array, check to make sure the element isn't null.
Apr 30 '08 #11

10K+
P: 13,264
Why not create a constructor for your Storage class that takes a String.

Expand|Select|Wrap|Line Numbers
  1. public Storage(String name) {
  2.       this.name = name;
  3. }
Then instead of
Expand|Select|Wrap|Line Numbers
  1. array[i].name = imp;
yo do
Expand|Select|Wrap|Line Numbers
  1. array[i] = new Storage(imp); 
The last line above there creates a new Storage object, sets its name to imp and initializes the ith element of the array to be that new Storage object.
Apr 30 '08 #12

Post your reply

Sign in to post your reply or Sign up for a free account.