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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • thatos
    New Member
    • Aug 2007
    • 105

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

    The code is below
    Code:
    import java.io.*;
    class Storage{
    	int num = 0;
    	String name = null;
    }
    
    public class gift1 {
    	public static void main(String args[]) throws IOException{
    		
    		BufferedReader in = new BufferedReader(new FileReader("e:/gift1.in"));
    		PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("e:/gift1.out")));
    		int number = Integer.parseInt(in.readLine());
    		Storage array[] = new Storage[number];
    		String inp;
    		for(int i =0;i<number;i++){
    			inp = in.readLine();
    			array[i].name = inp;
    		}
    		
    		for (int i = 0;i<number;i++){
    			String person = in.readLine();
    			String content[] = in.readLine().split(" ");
    			int amount = Integer.parseInt(content[0]);
    			int divider = Integer.parseInt(content[1]);
    			int givenamount;
    			if (divider == 0){
    				givenamount = 0;
    			}
    			else{
    				givenamount = amount/divider;
    			}
    			for(int j=0;j<divider;j++){
    				String given = in.readLine();
    				for(Storage k:array){
    					if (given.equals(k.name)){
    						k.num += givenamount;
    					}
    				}
    				
    			}
    			for(Storage j:array){
    				if (j.name.equals(person)){
    					j.num += amount - divider*givenamount;
    				}
    			}
    			
    		}
    		for (Storage i:array){
    			out.write(i.name+" "+i.num+"\n");
    		}
    		out.close();
    		System.exit(0);	
    	}
    
    }
    This is the error which it gives me
    Code:
    Exception in thread "main" java.lang.NullPointerException
    	at gift1.main(gift1.java:23)
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    When you do [CODE=java] Storage array[] = new Storage[number];[/CODE]
    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.

    Comment

    • JosAH
      Recognized Expert MVP
      • Mar 2007
      • 11453

      #3
      In the following line you allocate an array that can hold Storage objects:

      [code=java]
      Storage array[] = new Storage[number];
      [/code]

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

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Darn, too slow again; are people peeking over my shoulder when I type a reply?

        kind regards,

        Jos

        Comment

        • r035198x
          MVP
          • Sep 2006
          • 13225

          #5
          Originally posted by JosAH
          Darn, too slow again; are people peeking over my shoulder when I type a reply?

          kind regards,

          Jos
          Not many people, just me.

          Comment

          • thatos
            New Member
            • Aug 2007
            • 105

            #6
            So how can I solve the problem please help

            Comment

            • r035198x
              MVP
              • Sep 2006
              • 13225

              #7
              Originally posted by thatos
              So how can I solve the problem please help
              Originally posted by ME
              You need to intialize all the objects in the array first before you start dereferencing (dotting) them.
              . .

              Comment

              • thatos
                New Member
                • Aug 2007
                • 105

                #8
                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.


                Originally posted by r035198x
                . .

                Comment

                • thatos
                  New Member
                  • Aug 2007
                  • 105

                  #9
                  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?
                  Originally posted by r035198x
                  . .

                  Comment

                  • sukatoa
                    Contributor
                    • Nov 2007
                    • 539

                    #10
                    Originally posted by thatos
                    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

                    Comment

                    • Laharl
                      Recognized Expert Contributor
                      • Sep 2007
                      • 849

                      #11
                      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.

                      Comment

                      • r035198x
                        MVP
                        • Sep 2006
                        • 13225

                        #12
                        Why not create a constructor for your Storage class that takes a String.

                        [CODE=java]public Storage(String name) {
                        this.name = name;
                        }[/CODE]

                        Then instead of
                        [CODE=java]array[i].name = imp;[/CODE]
                        yo do
                        [CODE=java]array[i] = new Storage(imp); [/CODE]
                        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.

                        Comment

                        Working...