0

I am implementing a Java Project, and in the data access layer, I am using the function below:

public List<Error> readList(Predicate<Error> predicate) throws SQLException

It reads some Error list from the database according to criteria defined at the parameter predicate. Code of the Error is given below:

public class Error {

  private int primaryKey;

  private String definition;

  private String placeTaken;

  private boolean isSolved;

  private Status status;

  private String code;

  private boolean canBeSeenByEndUser;   

  private int applicationKey;  

  public Error() {
    // Assignments
  } 

  // other constructors and functions

  public String getPlaceTaken()
  {
    return this.placeTaken;
  }

}

I have written some junit test for testing the readList function. The readList_xxx function tests the readList function according to different predicates. All predicates that are the subject of non-string attributes, such as primaryKey, isSolved and status are working well. However, when I used the predicates that are the subject of string attributes, such as placeTaken and definition I am getting NullPointerException at the initializeReadList_006. Here is the details:

private boolean initializeReadList_006() {

    // the stacktrace points the error point here
    Predicate<Error> currentPredicate = p-> p.getPlaceTaken().equals("PLACETAKEN3");

    return (currentPredicate != null); 


}

@Test
public void readList_006() {    

    try { 

        assertEquals(true, initializeReadList_006());

        errorList = errorDataAccessLayer.readList(currentPredicate);    

        isEmpty = ListAffairs.isEmpty(errorList);
        isZero = ListAffairs.isNumberofElementsZero(errorList);

        assertEquals(false,isEmpty);            
        assertEquals(false,isZero);         

        int size = errorList.size();

        assertEquals(1, size); 


    } catch (Exception exception) {

        fail(exception.getMessage());

    }  catch (SQLException sqlException) {
        fail(sqlException.getMessage());
    }

}

When I debug the function readList, I am getting exception at the specified point. This function connects to database, reads the error list at the database and filters the errors according to predicate.

@Override
public List<Error> readList(Predicate<Error> predicate) throws SQLException {
    try 
    {
        List<Error> errorList = new ArrayList<Error>();
        Creator creator = this.getPreparedStatementCreator();           
        Connection connection = null; 
        List<Error> databaseList = new ArrayList<Error>();

        if(predicate == null)
            throw new ArgumentNullException(MessageAffairs.<Predicate>getEmptyInstanceMessage(Predicate.class));

        if(StringAffairs.isEmpty(this.schemaName)) 
            throw new RuntimeException(MessageAffairs.<String>getEmptyInstanceMessage(String.class));

        if(StringAffairs.isEmpty(this.tableName))
            throw new RuntimeException(MessageAffairs.<String>getEmptyInstanceMessage(String.class));

        this.databaseColumnList = this.createDatabaseColumnList();

        if(ArrayAffairs.isNumberofElementsZero(this.databaseColumnList))
            throw new RuntimeException();

        try
        {

            connection = this.getDatabaseConnection();

        } catch (ClassNotFoundException classNotFoundException) {           

            throw this.creator.createRuntimeException(classNotFoundException);              
        }       

        if(connection == null) {
            throw new RuntimeException();
        }           

        PreparedStatement statement = null;

        try 
        {
            statement = creator.createSelectPredicate(connection, this.schemaName, this.tableName);

        }catch(ArgumentNullException argumentNullException) {

            return databaseList;

        }
        catch (FailedActionError failedActionError) {


            return databaseList;
        }

        if(statement == null)
        { 
            return databaseList;
        }

         ResultSet result = statement.executeQuery();

         if(result == null)                 
            return databaseList;

         databaseList = creator.createErrorList(result,this.databaseColumnList);             

        connection.close(); 

        if(ListAffairs.isEmpty(databaseList))
             throw new RuntimeException();      

        if(ListAffairs.isNumberofElementsZero(databaseList))
            return databaseList;        


        //the point that I am getting error
        errorList = databaseList.stream()
        .filter( predicate )
        .collect(Collectors.<Error>toList());

        return errorList;            


    } catch (RuntimeException runtimeException) {

        throw runtimeException;

    }       
    catch (SQLException sqlException) {         

        throw sqlException;
    }

}

What I am doing wrong? Thanks in advance.

EDIT 1: This question is different from the classical NullPointerException. This question covers a technical affair related to Predicates.

EDIT 2: I think sigur's answer below is the right answer:

If you're getting a NullPointer inside your predicate, it means inside your list there's at least one null value. Something like: arrayList.add(null)

tahasozgen
  • 469
  • 5
  • 27
  • 2
    Does this answer your question? [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – jhamon Apr 21 '20 at 08:02
  • If you're getting a NullPointer inside your predicate, it means inside your list there's at least one null value. Something like: arrayList.add(null) – sigur Apr 21 '20 at 08:11
  • No, because the null control of predicate parameter has been done in the *readList* function. This function does not receive NullPointerException with the predicates that subject of non-string attributes. When I use them with a string attribute, I am getting NullPointerException even though I made every effort that I can make. – tahasozgen Apr 21 '20 at 08:11
  • @sigur thank you sigur. I do not know that issue. – tahasozgen Apr 21 '20 at 08:12
  • It would be easier to answer your question if you posted the stack trace from your the exception you are getting. – LeffeBrune Apr 21 '20 at 08:22
  • @LeffeBrune Actually, the stacktrace of NullPointerException is empty. The junit points the error point and I share it. – tahasozgen Apr 21 '20 at 08:37

0 Answers0