Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: unsorted array list (Exception in thread "main" java.lang.NullPointerException)

  1. #1
    Junior Member
    Join Date
    Oct 2013
    Posts
    5
    My Mood
    Goofy
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default unsorted array list (Exception in thread "main" java.lang.NullPointerException)

    this is the assignment:

    1. Create an Abstract DataElement Class as described in the Lecture.

    2. Inherit an IntElement from DataElement Class as described in the Lecture.

    3. Create an Abstract ArrayList Class as described in the Lecture.

    4. Inherit an UnsortedArrayList Class as described in the Lecture.

    5. Add methods to UnsortedArrayList Class:
    a) removeAt() - rewrite to remove element by swapping
    b) min() - retrieve smallest value in list
    c) max() - retrieve largest value in list
    d) sum() - compute sum all values in list.
    e) avg() - compute average of all values in list.

    6. Create an application:
    Generate a list of 100 values between 0 and 100
    using the generator (int)(Math.random()*101).
    Allow duplicate values.

    Find:
    a) smallest value
    b) largest value
    c) sum of all values
    d) average of all values

    Challenge Question: Design a Method to get an answer.

    The Management Team would like to know how many data
    items are within One Standard Deviation.
    How does this compare with a Normal Distribution?


    My question: Why im getting a nullpointer error at runtime? i cant find where the problem is.





    This is what i have so far... im thinking that once i fixed the problem, i can keep going with the calculations on sum, average etc.

    Main:

    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package lab6;
     
    public class Lab6 
    {
        public static void main(String[] args) 
        {
            int n;
     
            ArrayList log = new UnsortedArrayList(100); //created a new object which declares an array of 100 DataElement objects
     
            for(int i = 0; i<100; i++)
            {
     
                int randomNum = (int)(Math.random() *101); //generates a random number
                IntElement temp = new IntElement(randomNum); //creates a new IntElement object which sets the number
                System.out.println(i+1 + ") " + temp); //prints out what the random number is
                log.insert(temp); //inserts the IntElement object into the log array
     
            }
            n = log.max();
            System.out.println(n);
            //    System.out.println("Inserted 100 randomly generated numbers into the log.");
            //    System.out.println("min number is: " + log.min());
       //         System.out.println("min number is: " + log.max());
        }
    }



    DataElement class

    package lab6;
     
    public abstract class DataElement 
    {
        public abstract boolean equals(DataElement otherElement);
     
        public abstract int compareTo(DataElement otherElement);
     
        public abstract void makeCopy(DataElement otherElement);
     
        public abstract DataElement getCopy();
     
    }

    IntElement Class

    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package lab6;
     
    public class IntElement extends DataElement
    {
        protected int num;
     
        //default constructor
        public IntElement()
        {
            num = 0;
        }
     
        //constructor with parameters
        public IntElement(int x)
        {
            num = x;
        }
     
        //copy constructor
        public IntElement(IntElement otherElement)
        {
            num = otherElement.num;
        }
     
        //method to set the value of the instance variable num
        //postcondition: num = x;
        public void setNum(int x)
        {
            num = x;
        }
     
        //method to return the value of the instance variable num
        //postcondition: the value of num is returned
        public int getNum()
        {
            return num;
        }
     
        Override
        public boolean equals(DataElement otherElement)
        {
            IntElement temp = (IntElement) otherElement;
            return(num == temp.num);
        }
     
        Override
        public int compareTo(DataElement otherElement)
        {
            IntElement temp = (IntElement) otherElement;
    //        System.out.println("In IntElement.compareTo(obj): obj = " + temp.num + " and " + num);
            if (num < temp.num) 
                return -1;
            else 
                if (num > temp.num) 
                    return 1;        
                else
                    return 0;
        }
     
        Override
        public void makeCopy(DataElement otherElement)
        {
            IntElement temp = (IntElement) otherElement;
            num = temp.num;
        }
     
        Override
        public DataElement getCopy()
        {
            IntElement temp = new IntElement(num);
            return temp;
        }
     
        Override
        public String toString()
        {
            return String.valueOf(num);
        }
    }

    ArrayList Class

    package lab6;
     
    public abstract class ArrayList
    {
        protected int length;               //to store the length of the list
        protected int maxSize;              //to store the maxium size of the list
        protected DataElement[] list;       //array to hold the list elements
     
        public ArrayList()
        {
            maxSize = 101;
            length = 0;
            list = new DataElement[maxSize];
        }
     
        public ArrayList(int size)
        {
            if(size <= 0)
            {
                System.err.println("The array size must be positive. "
                        + "Creating an array of size 100. ");
                maxSize = 100;
            }
            else
            {
                maxSize = size;
            }
     
            length = 0;
            list = new DataElement[maxSize];
        }
     
        public ArrayList(ArrayList otherList)
        {
            maxSize = otherList.maxSize;
            length = otherList.length;
            list = new DataElement[maxSize];    //create the array
     
            for(int j = 0; j < length; j++)     //copy otherList
                list[j] = otherList.list[j].getCopy();
        }
     
        public boolean isEmpty()
        {
            return (length == 0);
        }
     
        public boolean isFull()
        {
            return (length == maxSize);
        }
     
        public int listSize()
        {
            return length;
        }
     
        public int maxListSize()
        {
            return maxSize;
        }
     
        public void print()
        {
            for(int i = 0; i < length; i++)
                System.out.println(list[i] + " ");
            System.out.println();
        }
     
        public boolean isItemAtEqual(int location, DataElement item)
        {
            return (list[location].equals(item));
        }
     
        public boolean insertAt(int location, DataElement insertItem)
        {
            boolean rc = true;
            if(location < 0 || location >= maxSize)
            {
                System.err.println("The position of the item to "
                        + "be inserted is out of range");
                rc = false;
            }
            else
                if(length >= maxSize)   //list is full
                {
                    System.err.println("Cannot insert in a full list.");
                    rc = false;
                }
                else
                {
                    for(int i = length; i > location; i--)
                        list[i] = list[i - i];      //move the elements down
     
                    list[location] = insertItem.getCopy();  //insert the item at
                                                          //the specified position
     
                    length++;       //increment the length
                }
            return rc;
        }
     
        public boolean insertEnd(DataElement insertItem)
        {
            boolean rc = true;
            if(length >= maxSize)   //the list is full
            {
                System.err.println("Cannot insert in a full list.");
                rc = false;
            }
            else
            {
                list[length] = insertItem.getCopy();    //insert the item
                                                        //at the end
                length++;
            }
            return rc;
        }
     
        public boolean removeAt(int location)
        {
            boolean rc = true;
            if(location < 0 || location >= length)
            {
                System.err.println("The location of the item to "
                        + "be removed is out of range.");
                rc = false;
            }
            else
            {
                for(int i = location; i < length - 1; i++)
                    list[i] = list[i + 1];
     
                list[length - 1] = null;
     
                length--;
            }
            return rc;
        }
     
        public DataElement retrieveAt(int location)
        {
            if(location < 0 || location >= length)
            {
                System.err.println("The location of the item to be "
                        + "retrieved is out of range.");
                return null;
            }
            else
                return list[location].getCopy();
        }
     
        public boolean replaceAt(int location, DataElement repItem)
        {
            boolean rc = true;
            if(location < 0 || location >= length)
            {
                System.err.println("The location of the item to "
                        + "be replaced is out of range");
                rc = false;
            }
            else
                list[location].makeCopy(repItem);
            return rc;
        }
     
        public void clearList()
        {
            for(int i = 0; i < length; i++)
                list[i] = null;
     
            length = 0;
     
            System.gc();
        }
     
        public abstract int seqSearch(DataElement searchItem);
     
        public abstract boolean insert(DataElement insertItem);
     
        public abstract void remove(DataElement removeItem);
     
        public abstract boolean insertDup (DataElement InsertItem);
     
        public abstract int min ();
     
        public abstract int max ();
     
        public void copyList(ArrayList otherList)
        {
            if(this != otherList)       //avoid self-copying
            {
                for(int j = 0; j < length; j++) //destroy this list
                    list[j] = null;
                System.gc();
     
                maxSize = otherList.maxSize;
                length = otherList.length;
                list = new DataElement[maxSize];    //create the array
     
                for(int j = 0; j < length; j++)     //copy otherList
                    list[j] = otherList.list[j].getCopy();
            }
        }
    }

    UnsortedArrayList Class

    package lab6;
     
    public class UnsortedArrayList extends ArrayList
    {
        public UnsortedArrayList()
        {
           super();
        }
     
        public UnsortedArrayList(int size)
        {
            super(size);
            list = new IntElement[size];
    	length = 0;
        }
     
        public UnsortedArrayList (UnsortedArrayList otherList)
        {
            super(otherList);       
        }
     
        Override
        public int seqSearch(DataElement searchItem)
        {
            int loc;
     
            for(loc = 0; loc < length; loc++)
                if(list[loc].equals(searchItem))
                    return loc;   //found
     
            return -1;           
        }
     
        Override
        public boolean insert(DataElement insertItem)
        {
            boolean rc = true;
            int loc;
     
            if(length == 0)                         //list is empty
                list[length++] = insertItem;        //insert the item and
                                                    //increment the length
            else
                if(length == maxSize)
                {
                    System.err.println("Cannot insert in a full list.");
                    rc = false;
                }
                else
                {
                    loc = seqSearch(insertItem);
     
                    if(loc == -1)       //the item to be inserted does not
                                        //exist in the list
                        list[length++] = insertItem.getCopy();
                    else
                    {
                     //   System.err.println("The item to be inserted is "
                     //           + "already in the list. "
                     //           + "No duplicates are allowed.");
                        rc = false;
                    }
                }
            return rc;
        }
     
        Override
        public void remove(DataElement removeItem)
        {
            int loc;
     
            if(length == 0)
                System.err.println("Cannon delete from an empty list");
            else
            {
                loc = seqSearch(removeItem);
     
                if(loc != -1)
                    removeAt(loc);
                else
                    System.out.println("The item to be deleted is "
                            + "not in the list.");
            }
        }
     
        Override
        public boolean removeAt(int location)
        {
            boolean rc = true; 
            if(location < 0 || location >= length)     //out of range
               rc = false;
            else
            {
               for(int i = location; i < length - 1; i++)
                   list[i] = list[i + 1];
               list[length - 1] = null;
               length--;
             }
             return rc;       
        }
     
        // insert duplicates
        Override
        public boolean insertDup(DataElement insertItem)
        {
            boolean rc = true;
            int loc;
            if(length == 0)                  //list is empty
               list[length++] = insertItem;  //insert the item and
                                             //increment the length
            else
               if(length == maxSize)  //list full
                  rc = false;
               else
                  list[length++] = insertItem.getCopy();
     
            return rc;       
        }//end insertDup
     
     
        Override
        public int min()
        {
           //  System.out.println("UnsortedArrayList.min(): list = " + list);
        int lowest = 100;
     
        for(int i = 0; i<100; i++)
        {
     
         //   System.out.println("UnsortedArrayList.min(): list[" +
         //           i + "] = " + list[i] +
         //           ", list[" + (i+1) + "] = " + list[i+1]);
    	if(list[i].compareTo(list[i+1]) == -1)
            {
                    IntElement temp = (IntElement)(list[i].getCopy());
                    if (temp.getNum() < lowest)
    		lowest = temp.getNum();
                    System.out.println (" lowest: " + lowest);
            }
     
        }
        return lowest;
        }
     
        Override
        public int max()
        {
            int highest = 0;
     
        for(int i = 0; i<100; i++)
        {
     
         //   System.out.println("UnsortedArrayList.min(): list[" +
         //           i + "] = " + list[i] +
         //           ", list[" + (i+1) + "] = " + list[i+1]);
    	if(list[i].compareTo(list[i+1]) == 1)
            {
                    IntElement temp = (IntElement)(list[i].getCopy());
                    if (temp.getNum() > highest)
    		highest = temp.getNum();
                    System.out.println (" highest: " + highest);
            }
     
     
        }return highest;
        }
     
       /* public double sum()
        {
     
        }
     
        public double average()
        {
     
        }    */
    }
    And this is what i get once i run the program:

    run:
    1) 92
    2) 54
    3) 91
    4) 79
    5) 23
    6) 46
    7) 37
    8) 62
    9) 45
    10) 42
    11) 15
    12) 42
    13) 8
    14) 50
    15) 45
    16) 0
    17) 70
    18) 71
    19) 15
    20) 91
    21) 22
    22) 71
    23) 84
    24) 61
    25) 44
    26) 21
    27) 55
    28) 16
    29) 70
    30) 94
    31) 100
    32) 34
    33) 50
    34) 64
    35) 61
    Exception in thread "main" java.lang.NullPointerException
    36) 59
    37) 22
    38) 34
    39) 42
    40) 57
    41) 58
    42) 34
    43) 17
    44) 79
    45) 18
    	at lab6.IntElement.compareTo(IntElement.java:55)
    46) 46
    47) 64
    48) 89
    	at lab6.UnsortedArrayList.max(UnsortedArrayList.java:156)
    	at lab6.Lab6.main(Lab6.java:32)
    49) 89
    50) 68
    51) 78
    52) 69
    53) 72
    54) 76
    55) 26
    56) 28
    57) 8
    58) 61
    59) 79
    60) 90
    61) 59
    62) 70
    63) 49
    64) 88
    65) 48
    66) 53
    67) 19
    68) 23
    69) 65
    70) 49
    71) 30
    72) 15
    73) 46
    74) 68
    75) 59
    76) 87
    77) 32
    78) 78
    79) 45
    80) 35
    81) 57
    82) 57
    83) 36
    84) 57
    85) 51
    86) 96
    87) 9
    88) 59
    89) 87
    90) 16
    91) 7
    92) 96
    93) 54
    94) 100
    95) 40
    96) 66
    97) 36
    98) 90
    99) 70
    100) 67
     highest: 92
     highest: 92
     highest: 92
     highest: 92
     highest: 92
     highest: 92
     highest: 92
     highest: 92
     highest: 92
     highest: 92
     highest: 92
     highest: 92
     highest: 92
     highest: 92
     highest: 100
     highest: 100
     highest: 100
     highest: 100
     highest: 100
     highest: 100
     highest: 100
     highest: 100
     highest: 100
     highest: 100
     highest: 100
     highest: 100
     highest: 100
     highest: 100
    Java Result: 1
    BUILD SUCCESSFUL (total time: 0 seconds)


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: unsorted array list (Exception in thread "main" java.lang.NullPointerException)

    And what's the error? Post that.

  3. #3
    Junior Member
    Join Date
    Oct 2013
    Posts
    5
    My Mood
    Goofy
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: unsorted array list (Exception in thread "main" java.lang.NullPointerException)

    If i run the program without invoking log.max(); i dont get any error.
    i dont think my problem is specifically in the max() function but in compareTO or somwhere else. i cant figure it out

    this is the error:
     
    35) 61
    Exception in thread "main" java.lang.NullPointerException
    36) 59
    37) 22
    38) 34
    39) 42
    40) 57
    41) 58
    42) 34
    43) 17
    44) 79
    45) 18
    	at lab6.IntElement.compareTo(IntElement.java:55)
    46) 46
    47) 64
    48) 89
    	at lab6.UnsortedArrayList.max(UnsortedArrayList.java:156)
    	at lab6.Lab6.main(Lab6.java:32)

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: unsorted array list (Exception in thread "main" java.lang.NullPointerException)

    Exception in thread "main" java.lang.NullPointerException
    at lab6.IntElement.compareTo(IntElement.java:55)
    Look at the code on line 55. What variable on that line has a null value? Then backtrack to see why that variable does not have a valid value.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Oct 2013
    Posts
    5
    My Mood
    Goofy
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: unsorted array list (Exception in thread "main" java.lang.NullPointerException)

    I think im going in the right direction but i still dont understand how to fix it. i found a temporary fix but i would like to know why is doing this.

    here is what i did, i added an if statement that if list[i] or list [i+1] == null to output a "NULLLLL" instead of going through the whole proces.
    After doing this i dont get any null pointers ERROR. but why list [i] and list [i+1] are null sometimes?
        public int max()
        {
            int highest = 0;
     
        for(int i = 0; i<100; i++)
        {
     
         //   System.out.println("UnsortedArrayList.min(): list[" +
         //           i + "] = " + list[i] +
         //           ", list[" + (i+1) + "] = " + list[i+1]);
          //  System.out.println ( "List: " + list [i]);
          //  System.out.println ( "List+1: " + list [i+1]);
         //   System.out.println ( "null: " + null);
     
     
            if ((list[i] == null)|| list[i+1] == null)
                System.out.println ( "NUUUUUULLLLL" );
            else
    	if(list[i].compareTo(list[i+1]) == 1)
            {
                    System.out.println ( "Not Null" );
                    IntElement temp = (IntElement)(list[i].getCopy());
                    if (temp.getNum() > highest)
    		highest = temp.getNum();
               //     System.out.println (" highest: " + highest);
            }




    Here is what happen once i run it.

    run:
    1) 95
    2) 89
    3) 81
    4) 61
    5) 45
    6) 91
    7) 88
    8) 7
    9) 0
    10) 6
    11) 76
    12) 9
    13) 78
    14) 95
    15) 14
    16) 88
    17) 44
    18) 82
    19) 40
    20) 93
    21) 59
    22) 56
    23) 62
    24) 55
    25) 41
    26) 35
    27) 84
    28) 11
    29) 62
    30) 87
    31) 35
    32) 48
    33) 26
    34) 5
    35) 85
    36) 4
    37) 30
    38) 17
    39) 83
    40) 93
    41) 52
    42) 6
    43) 40
    44) 62
    45) 44
    46) 77
    47) 22
    48) 58
    49) 30
    50) 6
    51) 3
    52) 38
    53) 100
    54) 17
    55) 5
    56) 42
    57) 44
    58) 15
    59) 65
    60) 17
    61) 87
    62) 33
    63) 20
    64) 66
    65) 69
    66) 69
    67) 67
    68) 17
    69) 66
    70) 20
    71) 82
    72) 51
    73) 72
    74) 35
    75) 35
    76) 20
    77) 100
    78) 72
    79) 65
    80) 24
    81) 99
    82) 2
    83) 63
    84) 83
    85) 64
    86) 16
    87) 84
    88) 60
    89) 47
    90) 7
    91) 10
    92) 57
    93) 54
    94) 43
    95) 15
    96) 64
    97) 96
    98) 14
    99) 68
    100) 27
    Not Null
    Not Null
    Not Null
    Not Null
    Not Null
    Not Null
    Not Null
    Not Null
    Not Null
    Not Null
    Not Null
    Not Null
    Not Null
    Not Null
    Not Null
    Not Null
    Not Null
    Not Null
    Not Null
    Not Null
    Not Null
    Not Null
    Not Null
    Not Null
    Not Null
    Not Null
    Not Null
    Not Null
    Not Null
    Not Null
    Not Null
    Not Null
    Not Null
    Not Null
    Not Null
    Not Null
    Not Null
    Not Null
    Not Null
    NUUUUUULLLLL
    NUUUUUULLLLL
    NUUUUUULLLLL
    NUUUUUULLLLL
    NUUUUUULLLLL
    NUUUUUULLLLL
    NUUUUUULLLLL
    NUUUUUULLLLL
    NUUUUUULLLLL
    NUUUUUULLLLL
    NUUUUUULLLLL
    NUUUUUULLLLL
    NUUUUUULLLLL
    NUUUUUULLLLL
    NUUUUUULLLLL
    NUUUUUULLLLL
    NUUUUUULLLLL
    NUUUUUULLLLL
    NUUUUUULLLLL
    NUUUUUULLLLL
    NUUUUUULLLLL
    NUUUUUULLLLL
    NUUUUUULLLLL
    NUUUUUULLLLL
    NUUUUUULLLLL
    NUUUUUULLLLL
    NUUUUUULLLLL
    NUUUUUULLLLL
    NUUUUUULLLLL
    NUUUUUULLLLL
    NUUUUUULLLLL
    NUUUUUULLLLL
    NUUUUUULLLLL
    NUUUUUULLLLL
    Highest Number: 100
    BUILD SUCCESSFUL (total time: 0 seconds)

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: unsorted array list (Exception in thread "main" java.lang.NullPointerException)

    why list [i] and list [i+1] are null sometimes?
    To see what is in an array, use the Arrays class's toString() method with println():
    System.out.println("an ID "+ java.util.Arrays.toString(theArrayName));

    Print out the contents of the list array occasionally so you can see what the code is putting in it.

    The debug output would be more useful if it included the value of i:
    System.out.println ( "NUUUUUULLLLL at i=" + i);
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Oct 2013
    Posts
    5
    My Mood
    Goofy
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: unsorted array list (Exception in thread "main" java.lang.NullPointerException)

    Alright so i found part of the problem, in the "insert" part my program used Seqserch to find duplicates, if there were duplicates it made that part of the array null.
    i made the "insert" part bypass the "seqsearch" so now my array is full but i have other code error "Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 100"

    the funny thing is that when i create the array if i create it with 101 elements or more for ex: 150, the program runs fine.

     UnsortedArrayList log = new UnsortedArrayList(120); //created a new object which declares an array of 100 DataElement objects
     
            for(int i = 0; i<120; i++)
            {
     
                int randomNum = (int)(Math.random() *101); //generates a random number
                IntElement temp = new IntElement(randomNum); //creates a new IntElement object which sets the number
                System.out.println(i+1 + ") " + temp); //prints out what the random number is
                log.insert(temp); //inserts the IntElement object into the log array
     
     
            }


    output:
    run:
    1) 51
    2) 22
    3) 55
    4) 28
    5) 5
    6) 77
    7) 77
    8) 86
    9) 14
    10) 9
    11) 24
    12) 90
    13) 48
    14) 20
    15) 13
    16) 84
    17) 5
    18) 57
    19) 69
    20) 62
    21) 100
    22) 96
    23) 15
    24) 34
    25) 72
    26) 37
    27) 67
    28) 23
    29) 44
    30) 64
    31) 61
    32) 82
    33) 97
    34) 2
    35) 88
    36) 52
    37) 58
    38) 85
    39) 2
    40) 91
    41) 35
    42) 96
    43) 75
    44) 19
    45) 5
    46) 72
    47) 74
    48) 88
    49) 21
    50) 84
    51) 19
    52) 27
    53) 25
    54) 76
    55) 52
    56) 15
    57) 16
    58) 5
    59) 97
    60) 97
    61) 28
    62) 48
    63) 97
    64) 80
    65) 76
    66) 49
    67) 3
    68) 84
    69) 73
    70) 72
    71) 78
    72) 19
    73) 69
    74) 12
    75) 36
    76) 77
    77) 88
    78) 71
    79) 88
    80) 17
    81) 86
    82) 14
    83) 92
    84) 1
    85) 51
    86) 21
    87) 93
    88) 42
    89) 95
    90) 56
    91) 77
    92) 32
    93) 55
    94) 37
    95) 53
    96) 80
    97) 58
    98) 38
    99) 76
    100) 85
    101) 20
    102) 95
    103) 41
    104) 1
    105) 2
    106) 8
    107) 85
    108) 63
    109) 17
    110) 25
    111) 23
    112) 88
    113) 10
    114) 75
    115) 35
    116) 49
    117) 31
    118) 93
    119) 89
    120) 69
    Highest Number: 100
    BUILD SUCCESSFUL (total time: 0 seconds)

    if i create it with 100 elements or less, it is either out of bounce or null pointer.

    Any tips?

    --- Update ---

    Here is with the error

       public static void main(String[] args) 
        {
            int n;
     
            ArrayList log = new UnsortedArrayList(100); //created a new object which declares an array of 100 DataElement objects
     
            for(int i = 0; i<100; i++)
            {
     
                int randomNum = (int)(Math.random() *101); //generates a random number
                IntElement temp = new IntElement(randomNum); //creates a new IntElement object which sets the number
                System.out.println(i+1 + ") " + temp); //prints out what the random number is
                log.insert(temp); //inserts the IntElement object into the log array
     
     
            }
            n = log.max();
            System.out.println("Highest Number: " + n);
            //    System.out.println("Inserted 100 randomly generated numbers into the log.");
            //    System.out.println("min number is: " + log.min());
       //         System.out.println("min number is: " + log.max());
        }
    }

     
    run:
    1) 5
    2) 33
    3) 72
    4) 57
    5) 40
    6) 36
    7) 40
    8) 32
    9) 28
    10) 1
    11) 85
    12) 9
    13) 85
    14) 35
    15) 10
    16) 49
    17) 71
    18) 36
    19) 0
    20) 77
    21) 67
    22) 1
    23) 58
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 100
    24) 79
    25) 92
    26) 87
    27) 74
    28) 48
    29) 47
    30) 67
    31) 6
    32) 41
    33) 52
    34) 68
    	at lab6.UnsortedArrayList.max(UnsortedArrayList.java:157)
    35) 49
    36) 30
    	at lab6.Lab6.main(Lab6.java:33)
    37) 34
    38) 38
    39) 2
    40) 89
    41) 81
    42) 72
    43) 29
    44) 76
    45) 58
    46) 39
    47) 24
    48) 10
    49) 12
    50) 61
    51) 50
    52) 1
    53) 45
    54) 22
    55) 34
    56) 94
    57) 71
    58) 31
    59) 9
    60) 21
    61) 100
    62) 36
    63) 24
    64) 54
    65) 80
    66) 59
    67) 18
    68) 60
    69) 79
    70) 0
    71) 57
    72) 17
    73) 31
    74) 97
    75) 89
    76) 6
    77) 16
    78) 21
    79) 89
    80) 24
    81) 49
    82) 62
    83) 57
    84) 26
    85) 10
    86) 69
    87) 49
    88) 4
    89) 9
    90) 39
    91) 16
    92) 45
    93) 8
    94) 79
    95) 86
    96) 9
    97) 18
    98) 2
    99) 19
    100) 64
    Java Result: 1
    BUILD SUCCESSFUL (total time: 0 seconds)

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: unsorted array list (Exception in thread "main" java.lang.NullPointerException)

    java.lang.ArrayIndexOutOfBoundsException: 100
    at lab6.UnsortedArrayList.max(UnsortedArrayList.java: 157)
    That error message says the program is trying to use an index that is past the end of the array.
    Remember that array indexes range in value from 0 to the array length-1. The valid indexes for an array of 100 elements would be 0 to 99.

    Look at line 157 to see why the code there uses an index past the end of the array. If you can't tell what the value of the indexes used there are, add a println() statement to print them out.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Oct 2013
    Posts
    5
    My Mood
    Goofy
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: unsorted array list (Exception in thread "main" java.lang.NullPointerException)

    Alright, thanks for all your help!
    i look in forums for very long time before i ask something and i couldnt find much info about this topic, so to make it easier for everybody else in the future, here is my finished program.
    the program was tested and it works great. ill let you know in a couple of weeks what my grade was

    run:
    1) 11
    2) 82
    3) 45
    4) 46
    5) 77
    6) 5
    7) 79
    8) 11
    9) 40
    10) 90
    11) 97
    12) 56
    13) 17
    14) 27
    15) 96
    16) 44
    17) 34
    18) 50
    19) 72
    20) 27
    21) 27
    22) 43
    23) 33
    24) 20
    25) 18
    26) 22
    27) 3
    28) 42
    29) 56
    30) 23
    31) 71
    32) 94
    33) 49
    34) 3
    35) 48
    36) 14
    37) 75
    38) 54
    39) 93
    40) 61
    41) 1
    42) 14
    43) 93
    44) 35
    45) 55
    46) 7
    47) 93
    48) 94
    49) 20
    50) 43
    51) 18
    52) 23
    53) 65
    54) 96
    55) 28
    56) 95
    57) 61
    58) 9
    59) 54
    60) 66
    61) 94
    62) 81
    63) 24
    64) 8
    65) 18
    66) 65
    67) 53
    68) 77
    69) 12
    70) 58
    71) 28
    72) 99
    73) 4
    74) 27
    75) 80
    76) 41
    77) 58
    78) 31
    79) 48
    80) 92
    81) 8
    82) 3
    83) 99
    84) 1
    85) 84
    86) 60
    87) 49
    88) 41
    89) 87
    90) 82
    91) 91
    92) 2
    93) 17
    94) 31
    95) 78
    96) 56
    97) 33
    98) 70
    99) 50
    100) 18
     
    Lowest number: 1
    Highest number: 99
    The sum of all the number is: 4783
    The average of all the number is: 47.0
     
    The Management Team would like to knowhow many data items are within One Standard Deviation.
    45, 46, 44, 50, 43, 42, 49, 48, 43, 53, 41, 48, 49, 41, 50, 
    Numbers within one standard diviation: 15
     
    How does this compare with a Normal Distribution?
    For a normal distribution "68% of the scores" should be within one standard deviation
    Porcentage of scores within one standard diviation %15
    BUILD SUCCESSFUL (total time: 0 seconds)

    package lab6;
     
    public abstract class DataElement 
    {
        public abstract boolean equals(DataElement otherElement);
     
        public abstract int compareTo(DataElement otherElement);
     
        public abstract void makeCopy(DataElement otherElement);
     
        public abstract DataElement getCopy();    
    }

    package lab6;
     
    public class IntElement extends DataElement
    {
        private int num = 0;
     
        public IntElement()
        {
            num = 0;
        }
     
        public IntElement(int x)
        {
            num = x;
        }
     
        public IntElement(IntElement otherElement)
        {
            num = otherElement.num;
        }
     
        public void setNum(int x)
        {
            num = x;
        }
     
        public int getNum()
        {
            return num;
        }
     
        @Override
        public boolean equals(DataElement otherElement)
        {
            IntElement temp = (IntElement) otherElement;
            return(num == temp.num);
        }
     
        @Override
        public int compareTo(DataElement otherElement)
        {
            IntElement temp = (IntElement) otherElement;
            if (num < temp.num) 
                return -1;
            else 
                if (num > temp.num) 
                    return 1;        
                else
                    return 0;
        }
     
        @Override
        public void makeCopy(DataElement otherElement)
        {
            IntElement temp = (IntElement) otherElement;
            num = temp.num;
        }
     
        @Override
        public DataElement getCopy()
        {
            IntElement temp = new IntElement(num);
            return temp;
        }
     
        @Override
        public String toString()
        {
            return String.valueOf(num);
        }
    }
    package lab6;
     
    public abstract class ArrayList extends IntElement
    {
        protected int length;     
        protected int maxSize;
        protected DataElement[] list;
     
        public ArrayList()
        {
            maxSize = 100;
            length = 0;
            list = new DataElement[maxSize];
        }
     
        public ArrayList(int size)
        {
            if(size <= 0)
            {
                System.err.println("The array size must be positive. "
                        + "Creating an array of size 100. ");
                maxSize = 100;
            }
            else
            {
                maxSize = size;
            }
     
            length = 0;
            list = new DataElement[maxSize];
        }
     
        public ArrayList(ArrayList otherList)
        {
            maxSize = otherList.maxSize;
            length = otherList.length;
            list = new DataElement[maxSize];
     
            for(int j = 0; j < length; j++)
                list[j] = otherList.list[j].getCopy();
        }
     
        public boolean isEmpty()
        {
            return (length == 0);
        }
     
        public boolean isFull()
        {
            return (length == maxSize);
        }
     
        public int listSize()
        {
            return length;
        }
     
        public int maxListSize()
        {
            return maxSize;
        }
     
        public void print()
        {
            for(int i = 0; i < length; i++)
                System.out.println(list[i] + " ");
            System.out.println();
        }
     
        public boolean isItemAtEqual(int location, DataElement item)
        {
            return (list[location].equals(item));
        }
     
        public boolean insertAt(int location, DataElement insertItem)
        {
            boolean rc = true;
            if(location < 0 || location >= maxSize)
            {
                System.err.println("The position of the item to "
                        + "be inserted is out of range");
                rc = false;
            }
            else
                if(length >= maxSize)
                {
                    System.err.println("Cannot insert in a full list.");
                    rc = false;
                }
                else
                {
                    for(int i = length; i > location; i--)
                        list[i] = list[i - i];
     
                    list[location] = insertItem.getCopy();               
                    length++;
                }
            return rc;
        }
     
        public boolean insertEnd(DataElement insertItem)
        {
            boolean rc = true;
            if(length >= maxSize)
            {
                System.err.println("Cannot insert in a full list.");
                rc = false;
            }
            else
            {
                list[length] = insertItem.getCopy();
                length++;
            }
            return rc;
        }
     
        public boolean removeAt(int location)
        {
            boolean rc = true;
            if(location < 0 || location >= length)
            {
                System.err.println("The location of the item to "
                        + "be removed is out of range.");
                rc = false;
            }
            else
            {
                for(int i = location; i < length - 1; i++)
                    list[i] = list[i + 1];
     
                list[length - 1] = null;           
                length--;
            }
            return rc;
        }
     
        public DataElement retrieveAt(int location)
        {
            if(location < 0 || location >= length)
            {
                System.err.println("The location of the item to be "
                        + "retrieved is out of range.");
                return null;
            }
            else
                return list[location].getCopy();
        }
     
        public boolean replaceAt(int location, DataElement repItem)
        {
            boolean rc = true;
            if(location < 0 || location >= length)
            {
                System.err.println("The location of the item to "
                        + "be replaced is out of range");
                rc = false;
            }
            else
                list[location].makeCopy(repItem);
            return rc;
        }
     
        public void clearList()
        {
            for(int i = 0; i < length; i++)
                list[i] = null;
     
            length = 0;       
            System.gc();
        }
     
        public void copyList(ArrayList otherList)
        {
            if(this != otherList)       //avoid self-copying
            {
                for(int j = 0; j < length; j++) //destroy this list
                    list[j] = null;
                System.gc();
     
                maxSize = otherList.maxSize;
                length = otherList.length;
                list = new DataElement[maxSize];    //create the array
     
                for(int j = 0; j < length; j++)     //copy otherList
                    list[j] = otherList.list[j].getCopy();
            }
        }
     
        public abstract int seqSearch(DataElement searchItem);
     
        public abstract boolean insert(DataElement insertItem);
     
        public abstract void remove(DataElement removeItem);
     
        public abstract boolean insertDup (DataElement InsertItem);
     
        public abstract int getMin ();
     
        public abstract int getMax ();
     
        public abstract int getSum ();
     
        public abstract double getAvg ();
     
        public abstract double getDiviations ();
     
        public abstract int within1StdDev (double getDiviations,double getAvg);
     
        public abstract int CalcNormDistribution (double getDiviations, double getAvg);
     
    }
    package lab6;
     
    public class UnsortedArrayList extends ArrayList
    {
        public UnsortedArrayList()
        {
           super();
        }
     
        public UnsortedArrayList(int size)
        {
            super(size);
            list = new IntElement[size];
    	length = 0;
        }
     
        public UnsortedArrayList (UnsortedArrayList otherList)
        {
            super(otherList);       
        }
     
        @Override
        public int seqSearch(DataElement searchItem)
        {
            int loc;
     
            for(loc = 0; loc < length; loc++)
                if(list[loc].equals(searchItem))
                    return loc;   //found
     
            return -1;           
        }
     
        @Override
        public boolean insert(DataElement insertItem)
        {
            boolean rc = true;
     
            if(length == 0)
                list[length++] = insertItem.getCopy(); 
            else
                if(length == maxSize)
                {
                    System.err.println("Cannot insert in a full list.");
                    rc = false;
                }
                else
                {
                    rc = true;
                    list[length++] = insertItem.getCopy();
                }
            return rc;
        }
     
        @Override
        public void remove(DataElement removeItem)
        {
            int loc;
     
            if(length == 0)
                System.err.println("Cannon delete from an empty list");
            else
            {
                loc = seqSearch(removeItem);           
                if(loc != -1)
                    removeAt(loc);
                else
                    System.out.println("The item to be deleted is "
                            + "not in the list.");
            }
        }
     
        @Override
        public boolean removeAt(int location)
        {
            boolean rc = true; 
            if(location < 0 || location >= length)
               rc = false;
            else
            {
               for(int i = location; i < length - 1; i++)
                   list[i] = list[i + 1];
     
               list[length - 1] = null;
               length--;
             }
             return rc;       
        }
     
        @Override
        public boolean insertDup(DataElement insertItem)
        {
            boolean rc = true;
     
            if(length == 0)                 
               list[length++] = insertItem;                        
            else
               if(length == maxSize)
                  rc = false;
               else
                  list[length++] = insertItem.getCopy();
     
            return rc;       
        }
     
     
        @Override
        public int getMin()
        {
            int lowest = 100;
     
            for(int i = 0; i<list.length-1; i++)
            {
                if(list[i].compareTo(list[i+1]) == -1)
                {
                        IntElement temp = (IntElement)(list[i].getCopy());
                        if (temp.getNum() < lowest)
                        lowest = temp.getNum();
                }       
        }
        return lowest;
        }
     
        @Override
        public int getMax()
        {
            int highest = 0;
     
            for(int i = 0; i<list.length-1; i++)
            {
                if(list[i].compareTo(list[i+1]) == 1)
                {
                        IntElement temp = (IntElement)(list[i].getCopy());
                        if (temp.getNum() > highest)
                        highest = temp.getNum();
                }
            }return highest;
        }
     
        @Override
        public int getSum()
        {
            int sum = 0;
     
            for(int i = 0; i<list.length; i++)
            {
               IntElement temp = (IntElement)(list[i].getCopy());
                sum += temp.getNum();   
            }
            return sum;       
        }
     
        @Override
        public double getAvg ()
        {
            int avg = 0;
     
            for(int i = 0; i<list.length; i++)
            {
               IntElement temp = (IntElement)(list[i].getCopy());
                avg += temp.getNum();     
            }
            avg /= 100;
            return avg;       
        }    
     
        @Override
        public double getDiviations ()
        {
            double getStdDev = 0;
            int i;
     
            for( i=0; i<list.length; i++)
                {
                   IntElement temp = (IntElement)(list[i].getCopy());
                    getStdDev += temp.getNum();    
                }        
            getStdDev =  Math.sqrt( getStdDev / list.length) ;        
            return getStdDev;   
        }
     
        @Override
           public int within1StdDev (double getDiviations,double getAvg)
        { 
            int i;
            int within1StdDev = 0;
     
            for (i=0; i <list.length ; i++)
            {
                IntElement temp = (IntElement)(list[i].getCopy());            
                if ((temp.getNum() > (getAvg - getDiviations)) && (temp.getNum() < (getAvg + getDiviations)) )
                {
                    System.out.print (temp.getNum() + ", ");
                    within1StdDev++;
                }
            }
            return within1StdDev;     
        }
     
        @Override
        public int CalcNormDistribution (double getDiviations, double getAvg)
        { 
            System.out.println ("For a normal distribution \"68% of the scores\" should be within one standard deviation");
            int calcNormDistribuion= 0;
            int i;
            int within1StdDev =0;
     
            for (i=0; i <list.length; i++)
            {
                IntElement temp = (IntElement)(list[i].getCopy());
                if ((temp.getNum() > (getAvg - getDiviations)) && ( temp.getNum() < (getAvg + getDiviations)) )
                    within1StdDev++;               
                calcNormDistribuion = ((within1StdDev * 100) / list.length);       
            }
            return calcNormDistribuion;
        } 
    }

Similar Threads

  1. I get the error Exception in thread "main" java.lang.NullPointerException?
    By jeremy28 in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: October 3rd, 2013, 11:13 PM
  2. Replies: 1
    Last Post: April 7th, 2013, 03:40 PM
  3. Replies: 5
    Last Post: December 9th, 2012, 02:25 PM
  4. Exception in thread "main" java.lang.NullPointerException problem.......
    By Adam802 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: September 20th, 2012, 02:23 AM
  5. Replies: 3
    Last Post: December 7th, 2011, 02:03 AM