Skip to content
Advertisement

How do I print a returning Array of objects ? I could only print the first 1

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:

JavaScript

//I made a full contractor:

JavaScript

//and a setter getter for each :

JavaScript

//I created a main class and also and random array of objects:

JavaScript

Created the function I asked about in the beginning :

JavaScript

//created two Arrays of objects, 1 with random objects that suppose to enter the function and a //receiving //Array of objects to print:

JavaScript

//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)

Advertisement

Answer

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.

Advertisement