# Error With Array Program

• 03-15-2013, 03:09 AM
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 :

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
System.out.print("give item :");
x = input.nextInt();                                // take one item to add in MyArray in Array Class
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 :

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 ;)
• 03-15-2013, 03:18 AM
jim829
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.

Code:

public int [] MyArray = new int[100];  // or however many ints you need to store;
Regards,
Jim
• 03-15-2013, 07:57 AM
Re: Error With Array Program

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

• 03-15-2013, 11:15 AM
PhHein
Re: Error With Array Program
Uhm, Jim, it's an int array, so all elements are defaulted to 0.
• 03-15-2013, 11:24 AM
PhHein
Re: Error With Array Program
Line 64 is the same. Removing elements won't work either as the index is wrong.
• 03-15-2013, 12:57 PM
jim829
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
• 03-15-2013, 12:59 PM
jim829
Re: Error With Array Program
Yeah! I just saw that. Clearly I was thinking Object and not int. Duh!!

Regards,
Jim
• 03-15-2013, 03:26 PM
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)

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
System.out.print("give item :");
x = input.nextInt();                                // take one item to add in MyArray in Array Class
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?
• 03-15-2013, 03:53 PM
Ronin
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.
• 03-15-2013, 04:14 PM