Re: null pointer exception while using arrays
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));
}