package com.mycompany.myapp;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

public class ClientCode {

    public static void main(String[] args) throws Exception {

//        Transaction tx = null;
        
        try/*( Session session = HibernateUtil.getSessionFactory().openSession() )*/ {
//            tx = session.beginTransaction();

            // FOR APPLE
            Food apple = new Apple(8.9);
            FoodDao.getInstance().save(apple);
            
//            // FOR BREAD LOAF
            ShippingForm shippingForm = new ShippingForm(123.45, 45.123, LocalDateTime.now(), false);
            BreadSlice breadSlice1 = new BreadSlice(0.2, 420.0, LocalDateTime.now().plusMinutes(1), false);
            BreadSlice breadSlice2 = new BreadSlice(0.8, 1680.0, LocalDateTime.now().plusMinutes(2), false);
            List<BreadSlice> breadSlices = new ArrayList<>(2);
            breadSlices.add(breadSlice1);
            breadSlices.add(breadSlice2);
//            
            Food breadLoaf = new BreadLoaf("Bready bread", shippingForm, breadSlices);
            FoodDao.getInstance().save(breadLoaf);
            
//            // DISPLAY ALL FOOD
            List<Food> foods = FoodDao.getInstance().loadAllData(Food.class);
            System.out.println("All of the ITransactionSets: " + foods);

//            tx.commit();
        } catch (Exception e) {
//            tx.rollback();
            e.printStackTrace();
        }
    }
}


package com.mycompany.myapp;

import jakarta.persistence.Basic;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.UUID;

@Entity
@Table(name = "apples")
public class Apple implements Food, Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.UUID)
    private final UUID uuid;
    
    private final double price;
    
    @Basic
    private LocalDateTime timestamp;
    
    public Apple() {
        this(0.0, LocalDateTime.now());
    }
    
    public Apple(double transactionAmount) {
        this(transactionAmount, LocalDateTime.now());
    }   

    public Apple(LocalDateTime timestamp) {
        this(0.0, timestamp);
    }
    
    public Apple(double transactionAmount, LocalDateTime timestamp) {
        this.uuid = UUID.randomUUID();
        this.price = transactionAmount;
        this.timestamp = timestamp;
    }
    
    public double getPrice() {
        return this.price;
    }
    
    @Override
    public UUID getUuid() {
        return this.uuid;
    }

    public String getTimestamp() {
        return timestamp.toString();
    }

    public void setTimestamp(LocalDateTime timestamp) {
        this.timestamp = timestamp;
    }

    public void setTimestamp(String timestamp) {
        this.timestamp = LocalDateTime.parse(timestamp);
    }
}

/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 */
package com.mycompany.myapp;

import jakarta.persistence.criteria.CriteriaBuilder;
import jakarta.persistence.criteria.CriteriaQuery;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.Transaction;

public class FoodDao {
    private static FoodDao transactionSetDao = null;
    
    private FoodDao() {}
    
    public static FoodDao getInstance() {
        if(transactionSetDao == null) {
            transactionSetDao = new FoodDao();
        }
        
        return transactionSetDao;
    }
    
    public void save(Food food) {
        Transaction tx = null;
        
        try( Session session = HibernateUtil.getSessionFactory().openSession() ) {
            tx = session.beginTransaction();
            
            session.merge(food);
            
            tx.commit();
        }
    }
    
    public <T> List<T> loadAllData(Class<T> type) {
        Transaction tx = null;
        
        try( Session session = HibernateUtil.getSessionFactory().openSession() ) {
            tx = session.beginTransaction();
            
            CriteriaBuilder builder = session.getCriteriaBuilder();
            CriteriaQuery<T> criteria = builder.createQuery(type);
            criteria.from(type);

            List<T> data = session.createQuery(criteria).getResultList();

            tx.commit();
            
            return data;
        }
    }
}

FoodDao.getInstance().save(apple); seems to be causing the NullPointerException, but why?

What must I change for the problem to be resolved? Be elaborate for the syntactic parts of your answer, but concise for the explanations.