Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用java注释生成Postgresql表_Java_Postgresql_Nullpointerexception_Dynamically Generated - Fatal编程技术网

使用java注释生成Postgresql表

使用java注释生成Postgresql表,java,postgresql,nullpointerexception,dynamically-generated,Java,Postgresql,Nullpointerexception,Dynamically Generated,我正在研究注释以及hibernate是如何工作的,以便更好地理解它 我知道我的代码可能不是最好的质量,当我可以解决我的异常时,我会修复它,我不断得到一个NullPointerException,但我不明白为什么,我做错了什么 这个类中的main只是为了找到问题的原因,我有另一个类将使用这个类,从我的另一个类中,我可以将数据添加到我的表中,但是我需要能够使用注释生成一个表,这就是我得到我的NullPointerException我无法生成我的表的地方 这是我的StackTrace Exceptio

我正在研究注释以及hibernate是如何工作的,以便更好地理解它

我知道我的代码可能不是最好的质量,当我可以解决我的异常时,我会修复它,我不断得到一个
NullPointerException
,但我不明白为什么,我做错了什么

这个类中的main只是为了找到问题的原因,我有另一个类将使用这个类,从我的另一个类中,我可以将数据添加到我的表中,但是我需要能够使用注释生成一个表,这就是我得到我的
NullPointerException
我无法生成我的表的地方

这是我的
StackTrace

Exception in thread "main" java.lang.RuntimeException: java.lang.NullPointerException
at psybergate.grad2018.javafnds.javabasics.annotations.hw3.vendor.DatabaseManager.generateDatabase(DatabaseManager.java:84)
at psybergate.grad2018.javafnds.javabasics.annotations.hw3.vendor.DatabaseManager.main(DatabaseManager.java:40)
Caused by: java.lang.NullPointerException
at psybergate.grad2018.javafnds.javabasics.annotations.hw3.vendor.DatabaseManager.generateDatabase(DatabaseManager.java:82)
... 1 more
这是我的密码

package psybergate.grad2018.javafnds.javabasics.annotations.hw3.vendor;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

import psybergate.grad2018.javafnds.javabasics.annotations.hw3.annotations.DomainClass;
import psybergate.grad2018.javafnds.javabasics.annotations.hw3.annotations.DomainProperty;
import psybergate.grad2018.javafnds.javabasics.annotations.hw3.developer.Customer;

/**
* @since 26 Apr 2018
* @author christiaan.dotze
*/
public class DatabaseManager {

private String addField = "";

private Connection connect = null;

private Statement statement = null;

private String primaryKey = "";

private String tableName = "";

public static void main(String[] args) {
    DatabaseManager manager = new DatabaseManager();
    try {
        Customer cust = new Customer();
        cust.setCustomerNum("11");
        cust.setFirstName("John");
        cust.setLastName("Doe");
        cust.setAge(27);
        cust.setDateOfBirth(1991);

        manager.generateDatabase(cust);
    } catch (ClassNotFoundException | NoSuchFieldException | SecurityException | SQLException ex) {
        throw new RuntimeException("An error occured", ex);
    }
}

public String getTableName(Object obj) {
    Annotation[] domainClassAnnotation = obj.getClass().getDeclaredAnnotations();
    for (Annotation annotation : domainClassAnnotation) {
        if (annotation instanceof DomainClass) {
            tableName = ((DomainClass) annotation).name();
        }
    }
    return tableName;
}

private String getDatabaseFields(Object obj) throws NoSuchFieldException, SecurityException {
    Field[] decladedFields = obj.getClass().getDeclaredFields();
    for (Field field : decladedFields) {
        Annotation[] annotations = field.getDeclaredAnnotations();
        for (Annotation annotation : annotations) {
            if (annotation instanceof DomainProperty) {
                DomainProperty myAnnotation = (DomainProperty) annotation;
                if (myAnnotation.primaryKey() == true) primaryKey = myAnnotation.name();
                if (myAnnotation.unique() == false) {
                    addField += myAnnotation.name() + " " + myAnnotation.dataType() + ",\n";
                } else {
                    addField += myAnnotation.name() + " " + myAnnotation.dataType() + " " + "NOT NULL" + ",\n";
                }
            }
        }
    }
    addField += "PRIMARY KEY(" + primaryKey + ")\n";
    return addField;
}

public void generateDatabase(Object obj)
        throws SQLException, ClassNotFoundException, NoSuchFieldException, SecurityException {
    createConnection();
    getTableName(obj);
    getDatabaseFields(obj);
    try {
        statement.executeUpdate("CREATE TABLE " + getTableName(obj) + "(\n" + getDatabaseFields(obj) + ")");
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    }
}

private void createConnection() throws ClassNotFoundException, SQLException {
    Class.forName("org.postgresql.Driver");
    connect = DriverManager.getConnection("jdbc:postgresql://localhost:5432/", "postgres", "1234");
}

public int add(Object obj, String data)
        throws NoSuchFieldException, SecurityException, SQLException, ClassNotFoundException {
    createConnection();
    statement = connect.createStatement();
    return statement.executeUpdate("INSERT INTO " + getTableName(obj) + " VALUES (" + data + ")");
}
}

“generateDatabase”方法中有空对象“statement”。“statement”字段在从未运行“add”时在“add”方法处初始化

试着把

statement = connect.createStatement();
之后

createConnection();

在“generateDatabase”方法中。

如果您指出相应行号的位置(DatabaseManager.java:84等),您是否尝试过调试该问题,这将非常有用?只要在IDE中使用调试器就可以了。我不敢相信我错过了这个,现在已经解决了,谢谢