I created a function that gets an array of objects and the type(String) of the abject and returning an array of objects with a specific type and after that, I tried to print it but it only print the first 1, not sure what went wrong. The objects are Tables:
public class q2Table {
private String color;
private int length;
private int width;
private int height;
private String type;
//I made a full contractor:
public q2Table(String color, int length, int width, int height, String type) {
this.color = color;
this.length = length;
this.width = width;
this.height = height;
this.type = type;
}
//and a setter getter for each :
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public int getLength() {
return length;
}
public void setLength(int length) {
this.length = length;
}
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
//I created a main class and also and random array of objects:
q2Table[] myTable = new q2Table[5];
myTable[0] = new q2Table("Yellow" , 20 , 20 , 20 , "Round");
myTable[1] = new q2Table("White" , 22 , 22 , 22 , "Rectangle");
myTable[2] = new q2Table("Purple" , 21 , 21 , 21 , "Round");
myTable[3] = new q2Table("Blue" , 21 , 21 , 21 , "Traingle");
myTable[4] = new q2Table("Green" , 21 , 21 , 21 , "Round");
Created the function I asked about in the beginning :
public static q2Table[] tableTypeArr(q2Table[] tables,String t) {
// Getting the index i need for the returning array
String type = t;
int i , x=0 , j;
for(i=0;i<tables.length;i ) {
if(tables[i].getType()==type) {
x ;
}
}
//creating the returning array
q2Table[] types = new q2Table[x];
for(j=0;j<types.length;j ) {
if(tables[j].getType()==type) {
types[j]=tables[j];
}
}
return types;
}
//created two Arrays of objects, 1 with random objects that suppose to enter the function and a //receiving //Array of objects to print:
q2Table[] testTable = new q2Table[3];
testTable = tableTypeArr(myTable , "Round");
int i;
for(i=0;i<testTable.length;i ) {
System.out.println(testTable[i].getColor() " " testTable[i].getHeight() " " testTable[i].getLength() " " testTable[i].getWidth() " " testTable[i].getType() );
}
//I successfully printed the first object and after that got an error in the terminal:
Yellow 20 20 20 Round Exception in thread "main" java.lang.NullPointerException: Cannot invoke "lab4.q2Table.getColor()" because "testTable[i]" is null at lab4.q2Main.main(q2Main.java:70)
CodePudding user response:
The NullPointerException occurs because in the second for loop in the function, you're just going through the first x
number of tables. You need to loop through all tables and select the matching typed tables into the new array.
public static q2Table[] tableTypeArr(q2Table[] tables,String t) {
// Getting the index i need for the returning array
String type = t;
int i , x=0 , j;
for(i=0;i<tables.length;i ) {
if(tables[i].getType()==type) {
x ;
}
}
//creating the returning array
q2Table[] types = new q2Table[x];
i=0; // Use i to maintain index for the new array
for(j=0;j<tables.length;j ) { // Loop all tables
if(tables[j].getType()==type) {
types[i ]=tables[j]; // Set the ith index value to the matched table and increment i.
}
}
return types;
}
CodePudding user response:
Your String comparison is wrong: you do: tables[j].getType()==type
you should do: tables[j].getType().equals(type)
. So when you fill your array in your tableTypeArr
you only pick one record and not 3 as you thought. So, when you try to iterate through your testTable, it only holds one record and 2 null values. So you print the first and on the second element you get your NPE (NUllPointerException). You can verify all this in debug.