I'm looking into annotations and how hibernate works, to get a better understanding of it.
I know my code might not be the best quality, and I will be fixing it up when I can resolve my exception, I keep getting a NullPointerException, but I cannot see why, what did I do wrong?
The main in this class is only there to find the cause of the problem, I have another class that will use this one, from my other class I am able to add data to my table, but I need to be able to generate a table using annotations, and this is where I my get my NullPointerException I cannot generate my table
This is my 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
This is my code
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 + ")");
}
}