G
gaff
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 == null){
segholder = 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.getIdentifier() == old_seg) {
segholder = 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
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 == null){
segholder = 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.getIdentifier() == old_seg) {
segholder = 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