Results 1 to 10 of 10
  1. #1
    ShadowWalker is offline Senior Member
    Join Date
    Mar 2013
    Location
    Greece
    Posts
    113
    Rep Power
    0

    Default Error With Array Program

    Hello All.. i am new in this forum ;)
    i am working on a task for university right now.. the exercise was about making an normal array a kind of dinamic array.. but something strange happen with my code when i try to execute it.. and i can't really find the mistake.. :(

    here is my code :

    Java Code:
    /**
    Make an Array like a Dinamic array 
    in this example the zero item in an Array is an Empty place.. so the input from user should will be non zero
    */
    
    import java.util.Scanner;
    
    class Array{
    	
    	public int [] MyArray;
    	private int c;
    	private int p;
    	
    	public Array(){ //  Constructor without c parameter
    		c = 4;    // i use on c to keep the first size
    		p = 4;    // and one p to increase it so i would know how much time the array has beed increased
    		int [] MyArray = new int[c];
    	}
    	
    	public Array(int c){           // Constructor with c parameter
    		this.c = c;
    		this.p = c;
    		int [] MyArray = new int[c];  // all item in MyArray Should be 0 (zero) right? 
    		
    	}
    	
    
    	public void add(int x){   // add an item when find zero in a place and if the array is full make the array 
    		                      // with double capacity and then add the item
    		for(int i = 0; i < p ; i++){
    			
    			if (MyArray[i] == 0){         //  <---- Here the code Crash! if i use this method
    				MyArray[i] = x;
    				break;
    			}
    			if (i == p ){
    				p = 2*c;
    				int [] MyArray = new int[p];
    				MyArray[i] = x;
    			}
    		}
    	
    	}
    
    	
    	public void delete(){    // delete method! which checks and remove the last non 0 item in array and resize the
    		int i = 0;           // array if the items on the array is 1/4 of the capacity..
    		while (MyArray[i]!=0 && i < MyArray.length){    //  <---- Here the code Crash! if i use this method
    			i ++;
    		}
    		if(i == MyArray.length){
    			if (MyArray[i] == 0){
    				System.out.println("I can't remove an item from an empy Array Sorry.. ");
    				
    			}else{
    				MyArray[i]=0;
    			}
    			
    		}else{
    			MyArray[i]=0;
    		}
    		if (i == (p/4) && p != c ){
    			p = p /2;
    			int [] MyArray = new int[p];
    		}
    		
    	}
    	
    	
    	
    	public void get(int i ){    // method whice print the item in array on index i 
    		System.out.println(MyArray[i]);
    	}
    	
    	public void getSize(){        // method which print the number of items in array 
    		int i = 0;
    		while (MyArray[i]!=0){    
    			i ++;
    		}
    		System.out.println("Size is : "+i);
    	}
    	
    	public void getCapacity(){   // method whice print the Capacity of the array
    		System.out.println(p);   
    	}
    }
    
    
    class ArrayTest{ //  create a main class to check if all in Array class works fine!
    	public static void main(String args[]){
    		String apanthsh ;
    		int c;
    		int x ;
    		Scanner input = new Scanner(System.in);  // create a scanner for user input
    		System.out.print("Give Capacity :");
    		c = input.nextInt();
    		Array MyArray = new Array(c);  // create an object of array class
    		for(int i = 1 ; i <= 20; i++ ){ // to check all the methods of array class
    			System.out.print("do you want to add or to remove an item ? ( add - remove) ");
    			apanthsh = input.next() ; // take the ansert
    			if (apanthsh.equals("Add") || apanthsh.equals("add")){ // check if the input was add 
    				System.out.print("give item :"); 
    				x = input.nextInt();         			// take one item to add in MyArray in Array Class
    				MyArray.add(x);							// call method which add the item
    				MyArray.getCapacity();					// print the Capacity of the Array (MyArray )
    				MyArray.getSize();						// print the Size 
    			}else{                                                 // or was remove 
    				MyArray.delete();						// delete the last item (non zero) in MyArray 
    				MyArray.getCapacity();					// print the Capacity of the Array (MyArray )
    				MyArray.getSize();						// print the Size 
    			}
    			
    		}
    	}
    }

    when i execute it all is going right until i must choose about add or remove.. if i choose to add and item then when i put an int item something happent and i got this error :

    Exception in thread "main" java.lang.NullPointerException
    at Array.add(ArrayTest.java:32)
    at ArrayTest.main(ArrayTest.java:104)

    i check what's going on at line 32 and all i have understant is that for some reason the method add(x) at this statement if (MyArray[i] == 0) crash because the value of MyArray is null .. but the questing is WHY?!?!?! maybe is the time .. it's 03:08 am here so maybe i am too tired to find it out :P but i am not going to sleep if i don't find it!! :P

    if someone knows what's going on pls give an advice ;)

    apologize for english which is rather poor.. i am from Greece so pls be lenient ;)

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,786
    Rep Power
    5

    Default Re: Error With Array Program

    Welcome to the forum!

    You only declare MyArray. You didn't allocate any storage to it. So all you have is a null reference.

    Java Code:
    public int [] MyArray = new int[100];  // or however many ints you need to store;
    Regards,
    Jim
    Last edited by jim829; 03-15-2013 at 12:56 PM. Reason: corrected my error. I was tired!!
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    ShadowWalker is offline Senior Member
    Join Date
    Mar 2013
    Location
    Greece
    Posts
    113
    Rep Power
    0

    Default Re: Error With Array Program

    Thx you for your reply!!

    The problem is that's i must no use public variables! it's one of the rules of my exercise... :(
    but i know that at line one i just make a private int [] MyArray but in construct i allocate the storage of the array if you can see : int [] MyArray = new int[c];
    isn't that correct? if i try to print the Array in the constructor all was right .. but in everyother method is like my MyArray isn't allocated.. strange.. i should search more.. :(

    Edit : i just found it! the all problem was at line 17 and at line 23 in whice i was declare the array type again.. the correct is that
    Java Code:
    1. int [] MyArray;
    17. MyArray = new int[c];  // now i allocate the memory of the Array
    
    23. MyArray = new int[c]; // now i allocate the memory of the Array
    Last edited by ShadowWalker; 03-15-2013 at 08:14 AM. Reason: Correct

  4. #4
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default Re: Error With Array Program

    Uhm, Jim, it's an int array, so all elements are defaulted to 0.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  5. #5
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default Re: Error With Array Program

    Line 64 is the same. Removing elements won't work either as the index is wrong.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,786
    Rep Power
    5

    Default Re: Error With Array Program

    I corrected my error in the post. I was thinking MyArray was a class. But you got the idea.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  7. #7
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,786
    Rep Power
    5

    Default Re: Error With Array Program

    Yeah! I just saw that. Clearly I was thinking Object and not int. Duh!!

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  8. #8
    ShadowWalker is offline Senior Member
    Join Date
    Mar 2013
    Location
    Greece
    Posts
    113
    Rep Power
    0

    Default Re: Error With Array Program

    I solved it and now is working great! here is the code with all correct ( i find out that i had some other mistakes in some methods)

    Java Code:
    /**
    Make an Array like a Dinamic array 
    in this example the zero item in an Array is an Empty place.. so the input from user should will be non zero
    */
    
    import java.util.Scanner;
    
    class Array{
    	
    	private int c;
    	private int p;
    	private int[] MyArray;
    	private int[] CopyOfMyArray;
    	
    	public Array(){ //  Constructor without c parameter
    		c = 4;    // i use on c to keep the first size
    		p = 4;    // and one p to increase it so i would know how much time the array has beed increased
    		MyArray = new int[c];
    		CopyOfMyArray = new int[c];
    	}
    	
    	public Array(int c){           // Constructor with c parameter
    		this.c = c;
    		this.p = c;
    		MyArray = new int[c];  // all item in MyArray Should be 0 (zero) 
    		CopyOfMyArray = new int[c];
    		
    	}
    	
    
    	public void add(int x){   // add an item when find zero in a place and if the array is full make the array 
    								// with double capacity and then add the item
    		int i ;
    		if(MyArray[MyArray.length - 1] ==0){
    			i = 0;
    			while(i<p && MyArray[i]!=0){
    				i = i + 1 ;
    			}
    			MyArray[i] = x;
    			CopyOfMyArray[i] = x;
    		}else{
    			p=2*p;
    			MyArray = new int[p];
    			for(int j = 0; j < CopyOfMyArray.length; j++){
    				MyArray[j]= CopyOfMyArray[j];
    			}
    			CopyOfMyArray = new int[p];
    			for(int k = 0; k <MyArray.length; k++){
    				CopyOfMyArray[k] = MyArray[k];
    			}
    			i = 0;
    			while(i<p && MyArray[i]!=0){
    				i = i + 1 ;
    			}
    			MyArray[i] = x;
    			CopyOfMyArray[i] = x;
    		}
    	
    	}
    
    	
    	public void delete(){    // delete method! which checks and remove the last non 0 item in array and resize the
    		int i = 0;           // array if the items on the array is 1/4 of the capacity..
    		if(MyArray[i] ==0  ){
    			System.out.println("I can't remove item from an empty array. Sorry ");
    		}else{
    			while (i < p && MyArray[i]!=0){
    				i++;
    			}
    			MyArray[i-1] = 0 ;
    			CopyOfMyArray[i-1] = 0;
    			if ( (i-1) <= (p/4) && p >= 2*c){
    				p = p / 2;
    				MyArray = new int[p];
    				for(int j = 0; j < CopyOfMyArray.length/2; j++){
    					MyArray[j]= CopyOfMyArray[j];
    				}
    				CopyOfMyArray = new int[p];
    				for(int k = 0; k <MyArray.length; k++){
    					CopyOfMyArray[k] = MyArray[k];
    				}
    		
    			}
    		}
    	}
    	
    
    	
    	public void get(int i ){    // method whice print the item in array on index i 
    		System.out.println(MyArray[i]);
    	}
    	
    	
    	public void set(int i , int x){       // replace the item at index i with x 
    		if (i< MyArray.length && i >= 0){ // to check if the index exist in the Array
    			MyArray[i] = x;
    		}else{
    			System.out.println("You try to replace an item in wrong index! Sorry");
    		}
    	}
    	
    
    	public void getSize(){        // method which print the number of items in array 
    		int i = 0;
    		while (i < MyArray.length && MyArray[i]!=0){    
    			i ++;
    		}
    		System.out.println("the number of elements on the array : "+i);
    	}	
    	
    	
    	public void getCapacity(){   // method whice print the Capacity of the array
    		System.out.println("The Capacity of the Array is : "+p);   
    	}
    	
    	public void PrintArray(){   // a method to print the Array 
    		System.out.println("The Array is ...");
    		for(int i = 0 ; i < MyArray.length; i ++){
    			System.out.println( (int)(i+1)+". " + MyArray[i]);
    		}
    	}
    }
    
    
    class ArrayTest{ //  create a main class to check if all in Array class works fine!
    	public static void main(String args[]){
    		String apanthsh ;
    		int c;
    		int x ;
    		Scanner input = new Scanner(System.in);  // create a scanner for user input
    		System.out.print("Give Capacity :");
    		c = input.nextInt();
    		Array MyArray = new Array(c);  // create an object of array class
    		apanthsh = "no";
    		while (!apanthsh.equals("exit")){ // to check all the methods of array class
    			System.out.print("do you want to add or to remove an item ? (if you want to exit press exit) ( add - remove)  : ");
    			apanthsh = input.next() ; // take the ansert
    			if (apanthsh.equals("Add") || apanthsh.equals("add")){ // check if the input was add 
    				System.out.print("give item :"); 
    				x = input.nextInt();         			// take one item to add in MyArray in Array Class
    				MyArray.add(x);							// call method which add the item
    				MyArray.getCapacity();					// print the Capacity of the Array (MyArray )
    				MyArray.getSize();						// print the Size 
    				MyArray.PrintArray();					// print all the Array to make sure that all was right
    				MyArray.set(0,5);                       // just a try of set method
    			}else{                                      // or was remove 
    				MyArray.delete();						// delete the last item (non zero) in MyArray 
    				MyArray.getCapacity();					// print the Capacity of the Array (MyArray )
    				MyArray.getSize();						// print the Size 
    				MyArray.PrintArray();							// print all the Array to make sure that all was right
    			}
    			
    		}
    	}
    }
    if you try to execute it you will see that all is correct (i hope so)
    now i have one questing.. i search a lot in web but i didn't find out what i want..
    there any way to make an EMPTY Array - int type - non with zero something like that = " "
    i find out about null but this works only at String Array type.. so the only way is to set values at zero as the empty or to something like -1 right?
    Last edited by ShadowWalker; 03-15-2013 at 03:29 PM. Reason: correcting

  9. #9
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    393
    Rep Power
    5

    Default Re: Error With Array Program

    All primitive data types have a default value which is automatically assigned upon declaration and used if no value is assigned.
    Primitive Data Types (The Java™ Tutorials > Learning the Java Language > Language Basics)

    Regards.

  10. #10
    ShadowWalker is offline Senior Member
    Join Date
    Mar 2013
    Location
    Greece
    Posts
    113
    Rep Power
    0

    Default Re: Error With Array Program

    yeah i understand so there is no way to have an empty array.. there must be mistake with my exercise .. because it tell me to make an empty array and then put or remove items.. so that's must be a mistake..

    Thx You all very much you help me a lot ;)

Similar Threads

  1. 2D array program
    By shaq in forum New To Java
    Replies: 21
    Last Post: 03-30-2011, 10:53 PM
  2. changing my program to array working program
    By Chewart in forum New To Java
    Replies: 39
    Last Post: 11-18-2009, 07:53 PM
  3. Help with an Array program please?
    By EternalSolitude in forum New To Java
    Replies: 3
    Last Post: 10-30-2008, 03:39 AM
  4. Array program help
    By adelgado0723 in forum New To Java
    Replies: 2
    Last Post: 04-16-2008, 02:19 PM
  5. Need help with my 1st array program
    By Phobos0001 in forum New To Java
    Replies: 5
    Last Post: 03-22-2008, 07:23 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •