0

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 + ")");
}
}
2
  • It would be helpful, if you point out where the corresponding line numbers are (DatabaseManager.java:84, etc) Commented May 1, 2018 at 10:23
  • Have you tried to debug the issue? Just go step-by step with debugger in IDE you use. Commented May 1, 2018 at 10:26

1 Answer 1

1

You have null object 'statement' in the 'generateDatabase' method. The 'statement' field is initialized at the 'add' method when 'add' is never runs.

Try to put

statement = connect.createStatement();

after

createConnection();

in 'generateDatabase' method.

Sign up to request clarification or add additional context in comments.

1 Comment

I cant believe I missed this, It's solved now thank you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.