【问题标题】:Exception Description: A NullPointerException异常描述:NullPointerException
【发布时间】:2019-04-12 17:51:42
【问题描述】:

我正在使用 postgresql 数据库在 java 中开发一个项目,当我尝试插入数据或提交时遇到问题。 看起来它来自 ID (game_id) 的问题,但我不知道是什么。

这是我的实体的代码:

@Entity
@Cacheable(true)
@Table(name = "game")
@Multitenant(MultitenantType.TABLE_PER_TENANT)
@TenantTableDiscriminator(type = TenantTableDiscriminatorType.SCHEMA, contextProperty = PersistenceUnitProperties.MULTITENANT_PROPERTY_DEFAULT)


public class Game implements Serializable{
    private static final long serialVersionUID = 1L;

    @Id
    @Column(name = "game_id", unique = true, nullable = false)
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private Integer gameId;

    @Column(name = "game_title", length = 256)
    private String gameTitle;

    @Temporal(TemporalType.DATE)
    @Column(name = "game_released")
    private Date gameReleased;

    @Column(name = "game_img")
    private Byte[] gameImg;

    @Column(name = "game_desc", length = 3072)
    private String gameDesc;

这是我尝试插入数据的方式:

EntityManagerFactory emf = Persistence.createEntityManagerFactory("projet_nintendo");
        EntityManager em = emf.createEntityManager();   
        EntityTransaction transac = em.getTransaction();
        transac.begin();



        for(int ii = 0; ii < array.length(); ii++) {
            Game g = new Game();
            g.setGameId(Conversion.stringEnInt(array.getJSONObject(ii).getString("fs_id")));
            g.setGameTitle(array.getJSONObject(ii).getString("title"));

            JSONArray test = array.getJSONObject(ii).getJSONArray("dates_released_dts");
            try {
                SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
                Date parsedDate = dateFormat.parse(test.getString(0));
                g.setGameReleased(parsedDate);
            } catch(Exception e) { 
            }
            em.persist(g);


            System.err.println(array.getJSONObject(ii).getString("pretty_date_s"));
        }

        transac.commit();
        em.close();
        emf.close();

我有这个错误:

Exception in thread "main" javax.persistence.RollbackException: Exception [EclipseLink-69] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.DescriptorException
Exception Description: A NullPointerException was thrown while extracting a value from the instance variable [gameId] in the object [database.orm.Game].
Internal Exception: java.lang.NullPointerException
Mapping: org.eclipse.persistence.mappings.DirectToFieldMapping[gameId-->game.game_id]
Descriptor: RelationalDescriptor(database.orm.Game --> [DatabaseTable(game)])
    at org.eclipse.persistence.internal.jpa.transaction.EntityTransactionImpl.commit(EntityTransactionImpl.java:157)
    at test.main(test.java:79)
Caused by: Exception [EclipseLink-69] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd)

你能告诉我我做错了什么吗?

【问题讨论】:

  • @sergei sirik :不,我知道什么是 NullPointerException。 ;)
  • 你能确定这不是空的吗? Conversion.stringEnInt(array.getJSONObject(ii).getString("fs_id"))
  • 那么,试着调试你的代码,看看你到底在哪一行得到了异常。我想它在这里g.setGameId(Conversion.stringEnInt(array.getJSONObject(ii).getString("fs_id")));。你在array 有什么?它来自哪里。检查这个minimal reproducible example
  • @SergeiSirik 我阅读了我的对象“游戏”的每个属性,没有什么是空的,一切都很好。错误出现在第 79 行,即 transac.commit ();

标签: java postgresql jpa eclipselink


【解决方案1】:
try(EntityManager em = emf.createEntityManager();   
    EntityTransaction transac = em.getTransaction()){

    ...
}
// don't need to close here em.. emf..

【讨论】:

  • 欢迎来到 StackOverflow!谢谢你的回答。我对其进行了一些重新设计,因此代码看起来更好。我使用带有“{}”图标的“代码示例”按钮。请使用“编辑”按钮为您的代码 sn-p 提供一些解释。谢谢!
【解决方案2】:

尝试删除您的@GeneratedValue。目前您正在使用

GenerationType.IDENTITY
This GenerationType indicates that the persistence provider must assign primary keys for the entity using a database identity column. IDENTITY column is typically used in SQL Server. This special type column is populated internally by the table itself without using a separate sequence. If underlying database doesn't support IDENTITY column or some similar variant then the persistence provider can choose an alternative appropriate strategy. In this examples we are using H2 database which doesn't support IDENTITY column.

这会导致数据库自动为您的 id 字段分配一个新值,然后 EclipseLink 必须检索它。

【讨论】:

    猜你喜欢
    • 2012-02-10
    • 2014-02-04
    • 2014-09-09
    • 2020-02-15
    • 1970-01-01
    • 1970-01-01
    • 2015-09-30
    • 2013-03-20
    • 1970-01-01
    相关资源
    最近更新 更多