Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / September 2006

Tip: Looking for answers? Try searching our database.

null pointer exception while using arrays

Thread view: 
gaff - 11 Sep 2006 19:01 GMT
Not sure why this code doesn't work, keep getting null pointer
exception problem.

Array_test is a class that has variables of type array, boolean,
String. The idea is to have an array of Array_test objects from which I
can add and delete objects but where all the objects are stored at the
very start with no ull spaces.

Am I wrong in assuming that having null spaces in an array with a class
of type that you have implemented is okay?

Anywho here's the code, and at the end are some tests and results from
the main.

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

// For the app only one instance of Trackeris created, it keeps track
of all segs that are created

public class Tracker {

//             Tracker has just one variable that's an array of objects
of type Array_test class which is predefined
    private Array_test segholder[];

//              construct.
    public Tracker(int size_of_array){
        segholder = new Array_test[size_of_array];
    }

//    retrive from array at i position
    public Array_test getElementFromArray(int element_location) {
        return segholder[element_location];
    }

//    retrive from array at i position
    public String getElementFromArrayName(int element_location) {

        String its_name;
        its_name = segholder[element_location].getIdentifier();

        return its_name;
    }

//    adds a object type array_test to the first null position in the
array segholder
    public void addToArray(Array_test new_seg) {

        int i = 0;

        while ( i < (segholder.length - 1)  ) {

            if (segholder[i] == null){
                segholder[i] = new_seg;
                i = segholder.length;
            }

        i++;
        }
    }

//    delete from an array
    public void deleteFromArray(String old_seg) {

        int i = 0;

        while (i < segholder.length) {
            if (segholder[i].getIdentifier() == old_seg) {
                segholder[i] = null;
                i = segholder.length;
            }
            i++;
        }

        // now remove the null space

        String[] remove = {null};
        List templist = new ArrayList(Arrays.asList(segholder));
        templist.removeAll(new ArrayList(Arrays.asList(remove)));

        segholder = (Array_test[]) templist.toArray(new Array_test[1000 -
templist.size()]);

    }

}

**********MAIN********
       Array_test o1 = new arrray_test("o1", 50);
       Array_test o2 = new arrray_test("o2", 50);
       Array_test o3 = new arrray_test("o3", 50);
       Array_test o4 = new arrray_test("o4", 50);
       Array_test o5 = new arrray_test("o5", 50);
       Array_test o6 = new arrray_test("o6", 50);

       track.addToArray(o1);
       track.addToArray(o2);
       track.addToArray(o3);
       track.addToArray(o4);
       track.addToArray(o5);
       track.addToArray(o6);

       System.out.println(track.getElementFromArrayName(0));
       System.out.println(track.getElementFromArrayName(1));
       System.out.println(track.getElementFromArrayName(2));
       System.out.println(track.getElementFromArrayName(3));
       System.out.println(track.getElementFromArrayName(4));
       System.out.println(track.getElementFromArrayName(5));

Result, works as expected:
o1
o2
o3
o4
o5
o6

add this next line and get null pointer exception
System.out.println(track.getElementFromArrayName(6));

The delete function works fine... until I put it into a GUI listener,
then I get the same exception

Could SOMEONE please explain to me what is happening and why.

Cheers,

Gaff
Oliver Wong - 11 Sep 2006 19:22 GMT
> Not sure why this code doesn't work, keep getting null pointer
> exception problem.

[snip most of the code]

   Note that you didn't post the code for Array_test, which sounds like
it's important here.

> **********MAIN********
>        Array_test o1 = new arrray_test("o1", 50);
[quoted text clipped - 3 lines]
>        Array_test o5 = new arrray_test("o5", 50);
>        Array_test o6 = new arrray_test("o6", 50);

   Here you've created 6 elements: 1, 2, 3, 4, 5, 6.

>        track.addToArray(o1);
>        track.addToArray(o2);
>        track.addToArray(o3);
>        track.addToArray(o4);
>        track.addToArray(o5);
>        track.addToArray(o6);

   Here, you're adding 6 elements: 1, 2, 3, 4, 5, 6.

>        System.out.println(track.getElementFromArrayName(0));
>        System.out.println(track.getElementFromArrayName(1));
>        System.out.println(track.getElementFromArrayName(2));
>        System.out.println(track.getElementFromArrayName(3));
>        System.out.println(track.getElementFromArrayName(4));
>        System.out.println(track.getElementFromArrayName(5));

   Here, you're accessing 6 elements: 0, 1, 2, 3, 4, 5 (count them, there
are 6 elements there).

> Result, works as expected:
> o1
[quoted text clipped - 6 lines]
> add this next line and get null pointer exception
> System.out.println(track.getElementFromArrayName(6));

   You're trying to access a 7th element, but you only have 6 elements.
That's probably why you're getting the NPE.

   - Oliver
gaff - 11 Sep 2006 21:50 GMT
When the array is created (sorry didn't include this line of code):
final Tracker track = new Tracker(1000);

the object track is created  and the 1000 indicated the size of the
array that it has as it's variable.

therefore shouldn't it be an array of 1000 null elements?

when I look for the 7th element, after occuping the first six, it
should return 1-6 of the objects and then null?
Oliver Wong - 11 Sep 2006 22:53 GMT
> When the array is created (sorry didn't include this line of code):
> final Tracker track = new Tracker(1000);
[quoted text clipped - 6 lines]
> when I look for the 7th element, after occuping the first six, it
> should return 1-6 of the objects and then null?

   If you post an SSCCE, it'll probably a lot easier for me to tell you
exactly why you're getting a NullPointerException:
http://mindprod.com/jgloss/sscce.html

   - Oliver
gaff - 12 Sep 2006 11:49 GMT
Compliable code uses two two classes and one main, you'll notice the
classes are similar, the first having an array of Strings as variable
and the second having an array of the first class as objects. They have
similar functions but never got any grief from the first class, does
this problem have something to do with nulls combined with Strings in
an array working fine and objects created with a class that I define
not working with nulls in an array, does that happen? :

//********Class one********// relaise this should be Array_test, typo I
never fixed, but code compiles

import java.util.*;

public class arrray_test {

    private String outro_text[];
    private String identifier;

    public arrray_test(String new_name, int array_size) {

        outro_text = new String[array_size];
        identifier = new_name;
    }

    // Assume that the array always has the entires at the start with the
first null to be the end.
    // Code should keep account of this

    // retrieve name of object
    public String getIdentifier() {
        return identifier;
    }

    // adds a string to the first null position in the array
    public void addToArray(String new_string) {

        int i = 0;

        while ( i < (outro_text.length - 1)  ) {

            if (outro_text[i] == null){
                outro_text[i] = new_string;
                i = outro_text.length;
            }

        i++;
        }
    }

    //method to retrieve single element from array in this object
    public String getElementFromArray(int element_location) {
        return outro_text[element_location];
    }

    // delete from an array only first occurence
    public void deleteFromArray(String old_string) {

        int i = 0;

        while (i < outro_text.length) {
            if (outro_text[i] == old_string) {
                outro_text[i] = null;
                i = outro_text.length;
            }
            i++;
        }

        // now remove the null space

        String[] remove = {null};
        List templist = new ArrayList(Arrays.asList(outro_text));
        templist.removeAll(new ArrayList(Arrays.asList(remove)));

        outro_text = (String[]) templist.toArray(new String[50 -
templist.size()]);

    }

    //remove all nulls form a string array... then add them back in at the
end so more strings can be added if necessarey
    public void removeNulls() {
        String[] remove = {null};
        List templist = new ArrayList(Arrays.asList(outro_text));
        templist.removeAll(new ArrayList(Arrays.asList(remove)));

        outro_text = (String[]) templist.toArray(new String[50 -
templist.size()]);

    }

    //list all strings in the array in the object as a string
    public String listAll() {

        int i = 0;
        String all_strings = null;

        while ((i < outro_text.length) &&(outro_text[i] != null)) {

                all_strings = all_strings + " " + outro_text[i];

            i++;
        }

        return all_strings;
    }

    // return the size of the array, i.e. the places that are not null
    public int sizeOfArray() {
        int i = 0;
        int array_size = 0;

        while (i < outro_text.length) {
            if (outro_text[i] != null) {
                array_size = array_size + 1;
            }
            else i = outro_text.length;
            i++;
        }

        return array_size;
    }

}

    //public void addString(String new_String) {
    //    new_String = outro_text[0];
    //}

    /*
    private Vector v;

    public arrray_test(String new_name, int vector_size) {
        v = new Vector(vector_size);
        namey = new_name;
    }

    public void addToVector(String new_string) {

        v.add(new_string);
    }

    public String getFirst() {
        String firsty;

        Iterator iter = v.iterator();
        //iter.hasNext();

        firsty =  (String)iter.next();

        return firsty;
    }*/

//********Class two********//
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

// For the app only one instance of Trackeris created, it keeps track
of all segs that are created

public class Tracker {

    // Tracker has just one variable that's an array of objects of type
Array_test class which is predefined
    private arrray_test segholder[];

    // construct.
    public Tracker(int size_of_array){
        segholder = new arrray_test[size_of_array];
    }

//    retrive from array at i position
    public arrray_test getElementFromArray(int element_location) {
        return segholder[element_location];
    }

//    retrive from array at i position
    public String getElementFromArrayName(int element_location) {

        String its_name;
        its_name = segholder[element_location].getIdentifier();

        return its_name;
    }

//    adds a object type array_test to the first null position in the
array segholder
    public void addToArray(arrray_test new_seg) {

        int i = 0;

        while ( i < segholder.length  ) {

            if (segholder[i] == null){
                segholder[i] = new_seg;
                i = segholder.length;
            }

        i++;
        }
    }

//    delete from an array only first occurence but impossible to have
duplicate objects
    public void deleteFromArray(String old_seg) {

        int i = 0;

        while (i < segholder.length) {
            if (segholder[i].getIdentifier() == old_seg) {
                segholder[i] = null;
                i = segholder.length;
            }
            i++;
        }

        // now remove the null space

        String[] remove = {null};
        List templist = new ArrayList(Arrays.asList(segholder));
        templist.removeAll(new ArrayList(Arrays.asList(remove)));

        segholder = (arrray_test[]) templist.toArray(new arrray_test[1000 -
templist.size()]);

    }

}

//********MAIN-1********//
public static void main(String[] args){
       //ButtonExample ss = new ButtonExample();
       final Tracker track = new Tracker(1000);

       arrray_test o1 = new arrray_test("o1", 50);
       arrray_test o2 = new arrray_test("o2", 50);
       arrray_test o3 = new arrray_test("o3", 50);
       arrray_test o4 = new arrray_test("o4", 50);
       arrray_test o5 = new arrray_test("o5", 50);
       arrray_test o6 = new arrray_test("o6", 50);

       track.addToArray(o1);
       track.addToArray(o2);
       track.addToArray(o3);
       track.addToArray(o4);
       track.addToArray(o5);
       track.addToArray(o6);

       System.out.println(track.getElementFromArrayName(0));
       System.out.println(track.getElementFromArrayName(1));
       System.out.println(track.getElementFromArrayName(2));
       System.out.println(track.getElementFromArrayName(3));
       System.out.println(track.getElementFromArrayName(4));
       System.out.println(track.getElementFromArrayName(5));
       System.out.println(track.getElementFromArrayName(6));
       //System.out.println("Break");

       track.deleteFromArray("o1");
       System.out.println(track.getElementFromArrayName(0));
       System.out.println(track.getElementFromArrayName(1));
       System.out.println(track.getElementFromArrayName(2));
       System.out.println(track.getElementFromArrayName(3));
       System.out.println(track.getElementFromArrayName(4));
   }
Oliver Wong - 12 Sep 2006 15:09 GMT
> Compliable code uses two two classes and one main, you'll notice the
> classes are similar, the first having an array of Strings as variable
[quoted text clipped - 3 lines]
> an array working fine and objects created with a class that I define
> not working with nulls in an array, does that happen? :

[code snipped]

   The code is too long for me to take a close look at it. Again, I suspect
the NPE is due to you add 6 objects, and trying to extract 7 objects. If you
want me to take another look, try to get your SSCCE down to 50 lines or
less. I don't think the array_test class is relevant to the problem. You
could just use Tracker, and add instances of Strings instead of instances of
array_test.

   - Oliver
gaff - 12 Sep 2006 11:49 GMT
Compliable code uses two two classes and one main, you'll notice the
classes are similar, the first having an array of Strings as variable
and the second having an array of the first class as objects. They have
similar functions but never got any grief from the first class, does
this problem have something to do with nulls combined with Strings in
an array working fine and objects created with a class that I define
not working with nulls in an array, does that happen? :

//********Class one********// relaise this should be Array_test, typo I
never fixed, but code compiles

import java.util.*;

public class arrray_test {

    private String outro_text[];
    private String identifier;

    public arrray_test(String new_name, int array_size) {

        outro_text = new String[array_size];
        identifier = new_name;
    }

    // Assume that the array always has the entires at the start with the
first null to be the end.
    // Code should keep account of this

    // retrieve name of object
    public String getIdentifier() {
        return identifier;
    }

    // adds a string to the first null position in the array
    public void addToArray(String new_string) {

        int i = 0;

        while ( i < (outro_text.length - 1)  ) {

            if (outro_text[i] == null){
                outro_text[i] = new_string;
                i = outro_text.length;
            }

        i++;
        }
    }

    //method to retrieve single element from array in this object
    public String getElementFromArray(int element_location) {
        return outro_text[element_location];
    }

    // delete from an array only first occurence
    public void deleteFromArray(String old_string) {

        int i = 0;

        while (i < outro_text.length) {
            if (outro_text[i] == old_string) {
                outro_text[i] = null;
                i = outro_text.length;
            }
            i++;
        }

        // now remove the null space

        String[] remove = {null};
        List templist = new ArrayList(Arrays.asList(outro_text));
        templist.removeAll(new ArrayList(Arrays.asList(remove)));

        outro_text = (String[]) templist.toArray(new String[50 -
templist.size()]);

    }

    //remove all nulls form a string array... then add them back in at the
end so more strings can be added if necessarey
    public void removeNulls() {
        String[] remove = {null};
        List templist = new ArrayList(Arrays.asList(outro_text));
        templist.removeAll(new ArrayList(Arrays.asList(remove)));

        outro_text = (String[]) templist.toArray(new String[50 -
templist.size()]);

    }

    //list all strings in the array in the object as a string
    public String listAll() {

        int i = 0;
        String all_strings = null;

        while ((i < outro_text.length) &&(outro_text[i] != null)) {

                all_strings = all_strings + " " + outro_text[i];

            i++;
        }

        return all_strings;
    }

    // return the size of the array, i.e. the places that are not null
    public int sizeOfArray() {
        int i = 0;
        int array_size = 0;

        while (i < outro_text.length) {
            if (outro_text[i] != null) {
                array_size = array_size + 1;
            }
            else i = outro_text.length;
            i++;
        }

        return array_size;
    }

}

    //public void addString(String new_String) {
    //    new_String = outro_text[0];
    //}

    /*
    private Vector v;

    public arrray_test(String new_name, int vector_size) {
        v = new Vector(vector_size);
        namey = new_name;
    }

    public void addToVector(String new_string) {

        v.add(new_string);
    }

    public String getFirst() {
        String firsty;

        Iterator iter = v.iterator();
        //iter.hasNext();

        firsty =  (String)iter.next();

        return firsty;
    }*/

//********Class two********//
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

// For the app only one instance of Trackeris created, it keeps track
of all segs that are created

public class Tracker {

    // Tracker has just one variable that's an array of objects of type
Array_test class which is predefined
    private arrray_test segholder[];

    // construct.
    public Tracker(int size_of_array){
        segholder = new arrray_test[size_of_array];
    }

//    retrive from array at i position
    public arrray_test getElementFromArray(int element_location) {
        return segholder[element_location];
    }

//    retrive from array at i position
    public String getElementFromArrayName(int element_location) {

        String its_name;
        its_name = segholder[element_location].getIdentifier();

        return its_name;
    }

//    adds a object type array_test to the first null position in the
array segholder
    public void addToArray(arrray_test new_seg) {

        int i = 0;

        while ( i < segholder.length  ) {

            if (segholder[i] == null){
                segholder[i] = new_seg;
                i = segholder.length;
            }

        i++;
        }
    }

//    delete from an array only first occurence but impossible to have
duplicate objects
    public void deleteFromArray(String old_seg) {

        int i = 0;

        while (i < segholder.length) {
            if (segholder[i].getIdentifier() == old_seg) {
                segholder[i] = null;
                i = segholder.length;
            }
            i++;
        }

        // now remove the null space

        String[] remove = {null};
        List templist = new ArrayList(Arrays.asList(segholder));
        templist.removeAll(new ArrayList(Arrays.asList(remove)));

        segholder = (arrray_test[]) templist.toArray(new arrray_test[1000 -
templist.size()]);

    }

}

//********MAIN-1********//
public static void main(String[] args){
       //ButtonExample ss = new ButtonExample();
       final Tracker track = new Tracker(1000);

       arrray_test o1 = new arrray_test("o1", 50);
       arrray_test o2 = new arrray_test("o2", 50);
       arrray_test o3 = new arrray_test("o3", 50);
       arrray_test o4 = new arrray_test("o4", 50);
       arrray_test o5 = new arrray_test("o5", 50);
       arrray_test o6 = new arrray_test("o6", 50);

       track.addToArray(o1);
       track.addToArray(o2);
       track.addToArray(o3);
       track.addToArray(o4);
       track.addToArray(o5);
       track.addToArray(o6);

       System.out.println(track.getElementFromArrayName(0));
       System.out.println(track.getElementFromArrayName(1));
       System.out.println(track.getElementFromArrayName(2));
       System.out.println(track.getElementFromArrayName(3));
       System.out.println(track.getElementFromArrayName(4));
       System.out.println(track.getElementFromArrayName(5));
       System.out.println(track.getElementFromArrayName(6));
       //System.out.println("Break");

       track.deleteFromArray("o1");
       System.out.println(track.getElementFromArrayName(0));
       System.out.println(track.getElementFromArrayName(1));
       System.out.println(track.getElementFromArrayName(2));
       System.out.println(track.getElementFromArrayName(3));
       System.out.println(track.getElementFromArrayName(4));
   }
Patricia Shanahan - 11 Sep 2006 23:52 GMT
> //    adds a object type array_test to the first null position in the
> array segholder
[quoted text clipped - 12 lines]
>         }
>     }

As far as I can tell, given the loop structure, there is a bug in
addToArray. If the first length-1 entries are non-null, the reference
will not be added to the array.

I have a web page on an approach to debugging at
http://home.earthlink.net/~patricia_shanahan/debug/

Patricia
gaff - 12 Sep 2006 09:36 GMT
The addToArray method seems to be working fine, it finds the first null
space in the array and adds the object in this space.

Haven't tried to fill the array yet but see what you mean with the -1
part.
gaff - 12 Sep 2006 09:36 GMT
The addToArray method seems to be working fine, it finds the first null
space in the array and adds the object in this space.

Haven't tried to fill the array yet but see what you mean with the -1
part.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.