0

I don't understand why I'm getting this error:

java.lang.NullPointerException: Cannot invoke "String.toLowerCase(java.util.Locale)" because "columnName" is null

This is my product table in my database:

enter image description here

And this is the Product model:

@Entity
@Table(name = "product", schema = "auction_app_schema")
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Product {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(name = "product_name", nullable = false)
    private String productName;

    @Column(name = "description", nullable = false)
    private String description;

    @Column(name = "start_price", nullable = false)
    private Float startPrice;

    @ElementCollection
    @CollectionTable(name = "product_images", joinColumns = @JoinColumn(name = "product_id"))
    @Column(name = "image")
    private List<String> images;

    @Column(name = "start_date", nullable = false)
    private LocalDateTime startDate;

    @Column(name = "end_date", nullable = false)
    private LocalDateTime endDate;

    @Formula("(SELECT COUNT(*) FROM auction_app_schema.bid b INNER JOIN auction_app_schema.product p ON p.id = b.product_id WHERE b.product_id = p.id)")
    private BigInteger numberOfBids;

    @Formula("(SELECT b.price FROM auction_app_schema.product p INNER JOIN auction_app_schema.bid b ON p.id =b.product_id ORDER BY b.price DESC LIMIT 1)")
    private BigDecimal highestBid;

    @ManyToOne
    @JoinColumn(name = "category_id", nullable = false)
    private Category category;
}

Here is the repository function:

@Query(value = "SELECT p.id, p.description, p.product_name, p.start_price, p.start_date, p.end_date, p.category_id, p.is_highlighted, pi.image\n" +
        "FROM auction_app_schema.product p\n" +
        "INNER JOIN auction_app_schema.category c ON p.category_id = c.id\n" +
        "INNER JOIN auction_app_schema.product_images pi ON p.id = pi.product_id\n" +
        "WHERE c.id = ? LIMIT ? OFFSET ?", nativeQuery = true)
List<Product> findProductsByCategoryId(Long categoryId, int limit, int offset);

Context: This same query executed directly in database works, soon as I send request to server I get the NullPointerException even when I have all the columns in my database. I didn't run into this problem with PostgreSQL until now. Hibernate is just not picking up my schema, I think.

This is almost the whole stack trace:

FROM auction_app_schema.product p
INNER JOIN auction_app_schema.category c ON p.category_id = c.id
INNER JOIN auction_app_schema.product_images pi ON p.id = pi.product_id
WHERE c.id = ? LIMIT ? OFFSET ?
2023-04-01 22:56:55.147 ERROR 18404 --- [nio-8080-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.NullPointerException: Cannot invoke "String.toLowerCase(java.util.Locale)" because "columnName" is null] with root cause

    java.lang.NullPointerException: Cannot invoke "String.toLowerCase(java.util.Locale)" because "columnName" is null
        at org.postgresql.jdbc.PgResultSet.findColumnIndex(PgResultSet.java:2791) ~[postgresql-42.2.23.jar:42.2.23]
        at org.postgresql.jdbc.PgResultSet.findColumn(PgResultSet.java:2750) ~[postgresql-42.2.23.jar:42.2.23]
        at org.postgresql.jdbc.PgResultSet.getBigDecimal(PgResultSet.java:410) ~[postgresql-42.2.23.jar:42.2.23]
        at com.zaxxer.hikari.pool.HikariProxyResultSet.getBigDecimal(HikariProxyResultSet.java) ~[HikariCP-4.0.3.jar:na]
        at org.hibernate.type.descriptor.sql.DecimalTypeDescriptor$2.doExtract(DecimalTypeDescriptor.java:63) ~[hibernate-core-5.4.32.Final.jar:5.4.32.Final]
        at org.hibernate.type.descriptor.sql.BasicExtractor.extract(BasicExtractor.java:47) ~[hibernate-core-5.4.32.Final.jar:5.4.32.Final]
        at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:257) ~[hibernate-core-5.4.32.Final.jar:5.4.32.Final]
        at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:253) ~[hibernate-core-5.4.32.Final.jar:5.4.32.Final]
        at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:243) ~[hibernate-core-5.4.32.Final.jar:5.4.32.Final]
        at org.hibernate.type.AbstractStandardBasicType.hydrate(AbstractStandardBasicType.java:329) ~[hibernate-core-5.4.32.Final.jar:5.4.32.Final]
        at org.hibernate.persister.entity.AbstractEntityPersister.hydrate(AbstractEntityPersister.java:3131) ~[hibernate-core-5.4.32.Final.jar:5.4.32.Final]
        at org.hibernate.loader.Loader.loadFromResultSet(Loader.java:1863) ~[hibernate-core-5.4.32.Final.jar:5.4.32.Final]
        at org.hibernate.loader.Loader.hydrateEntityState(Loader.java:1791) ~[hibernate-core-5.4.32.Final.jar:5.4.32.Final]
        at org.hibernate.loader.Loader.instanceNotYetLoaded(Loader.java:1764) ~[hibernate-core-5.4.32.Final.jar:5.4.32.Final]
        at org.hibernate.loader.Loader.getRow(Loader.java:1616) ~[hibernate-core-5.4.32.Final.jar:5.4.32.Final]
        at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:740) ~[hibernate-core-5.4.32.Final.jar:5.4.32.Final]
        at org.hibernate.loader.Loader.getRowsFromResultSet(Loader.java:1039) ~[hibernate-core-5.4.32.Final.jar:5.4.32.Final]
        at org.hibernate.loader.Loader.processResultSet(Loader.java:990) ~[hibernate-core-5.4.32.Final.jar:5.4.32.Final]
        at org.hibernate.loader.Loader.doQuery(Loader.java:959) ~[hibernate-core-5.4.32.Final.jar:5.4.32.Final]
        at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:349) ~[hibernate-core-5.4.32.Final.jar:5.4.32.Final]
        at org.hibernate.loader.Loader.doList(Loader.java:2843) ~[hibernate-core-5.4.32.Final.jar:5.4.32.Final]
        at org.hibernate.loader.Loader.doList(Loader.java:2825) ~[hibernate-core-5.4.32.Final.jar:5.4.32.Final]
        at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2657) ~[hibernate-core-5.4.32.Final.jar:5.4.32.Final]
        at org.hibernate.loader.Loader.list(Loader.java:2652) ~[hibernate-core-5.4.32.Final.jar:5.4.32.Final]
        at org.hibernate.loader.custom.CustomLoader.list(CustomLoader.java:338) ~[hibernate-core-5.4.32.Final.jar:5.4.32.Final]
        at org.hibernate.internal.SessionImpl.listCustomQuery(SessionImpl.java:2141) ~[hibernate-core-5.4.32.Final.jar:5.4.32.Final]
        at org.hibernate.internal.AbstractSharedSessionContract.list(AbstractSharedSessionContract.java:1169) ~[hibernate-core-5.4.32.Final.jar:5.4.32.Final]
        at org.hibernate.query.internal.NativeQueryImpl.doList(NativeQueryImpl.java:176) ~[hibernate-core-5.4.32.Final.jar:5.4.32.Final]
        at org.hibernate.query.internal.AbstractProducedQuery.list(AbstractProducedQuery.java:1604) ~[hibernate-core-5.4.32.Final.jar:5.4.32.Final]
        at org.hibernate.query.Query.getResultList(Query.java:165) ~[hibernate-core-5.4.32.Final.jar:5.4.32.Final]
        at org.springframework.data.jpa.repository.query.JpaQueryExecution$CollectionExecution.doExecute(JpaQueryExecution.java:126) ~[spring-data-jpa-2.5.5.jar:2.5.5]
        at org.springframework.data.jpa.repository.query.JpaQueryExecution.execute(JpaQueryExecution.java:88) ~[spring-data-jpa-2.5.5.jar:2.5.5]
        at org.springframework.data.jpa.repository.query.AbstractJpaQuery.doExecute(AbstractJpaQuery.java:155) ~[spring-data-jpa-2.5.5.jar:2.5.5]
        at org.springframework.data.jpa.repository.query.AbstractJpaQuery.execute(AbstractJpaQuery.java:143) ~[spring-data-jpa-2.5.5.jar:2.5.5]
Nemanja
  • 113
  • 2
  • 9

1 Answers1

0

Looks like this says that you are trying to call the "toLowerCase()" method on null.

Please ensure that columnName is not null before calling the toLowerCase() method.

you can first check columnName is not null and if it is not, it assigns the lowercase version.

  • Indeed it looks like `id` is not named. – Joop Eggen Apr 01 '23 at 21:28
  • @JoopEggen Elaborate please. – Nemanja Apr 01 '23 at 21:34
  • @Nemanja missing `@Column name` – Joop Eggen Apr 02 '23 at 15:24
  • Wrong answer. The error was because the colleague ran hibernate drop-create on database. Previous version of the database had a column that wasn't created by Hibernate mapping and that's why on runtime when accessing it I was getting this stack trace. As for column name, I was suprised to see what are you pointing at since you have been on SO for 11 years, and don't know that we use @Column for giving the additional information about the field. It is not mandatory to define it and would not cause the NullPointerException. All the best to you. – Nemanja Apr 23 '23 at 06:53