NullPointerException podczas tworzenia bazy

0

Hej, nie mogę dojść dlaczego wyskakuje mi błąd w metodzie getDao(). Macie pomysł jak to naprawić?
Poniżej kod błędu.
CreateConnectionSource
InitCreateConnectionSource
getDao
2021-12-10 2257,639 [DEBUG] DaoManager created dao for class class pl.praca.inzynierska.database.model.Company with reflection
GetDaoError
java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:464)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:363)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1051)
Caused by: java.lang.NullPointerException
at praca.inzynierska/pl.praca.inzynierska.database.dao.CommonDao.getDao(CommonDao.java:104)
at praca.inzynierska/pl.praca.inzynierska.database.dao.CommonDao.findById(CommonDao.java:75)
at praca.inzynierska/pl.praca.inzynierska.app.App.main(App.java:25)
... 11 more
Exception running application pl.praca.inzynierska.app.App

Ustawiłem w metodach komunikaty aby mieć pewność co i kiedy się wywołuje.

Wysyłam również metody z których korzystam.

// Klasa DbManager
public static void initDatabase(){
createConnectionSource();
System.out.println("InitCreateConnectionSource");
closeConnectionSource();
System.out.println("InitCloseConnectionSource");
}

// Klasa DbManager
private static void createConnectionSource(){
try {
connectionSource = new JdbcConnectionSource(DB_URL, DBUser.ADMIN_LOGIN, DBUser.ADMIN_PASSWORD);
System.out.println("CreateConnectionSource");
} catch (SQLException e) {
System.out.println("Nie utworzono połaczenia");
LOGGER.warn(e.getMessage());
}
}

// Klasa DbManager
public static void closeConnectionSource(){
if(connectionSource!=null){
try {
connectionSource.close();
} catch (IOException e) {
LOGGER.warn(e.getMessage());
}
}
}

// Klasa CommonDao
public <T extends BaseModel, I> Dao<T, I> getDao(Class<T> cls) throws ApplicationException {
try {
System.out.println("getDao");
return DaoManager.createDao(connectionSource, cls);
} catch (SQLException e) {
System.out.println("GetDaoError");
LOGGER.warn(e.getCause().getMessage());
throw new ApplicationException("Error");
} finally {
this.closeDbConnection();
}
}

// Klasa Main
public static void main(String[] args) throws SQLException, IOException, ApplicationException {
launch(args);
CommonDao commonDao = new CommonDao();
Industry byId = commonDao.findById(Industry.class, 1);
System.out.println(byId);

}

@Override
public void start(Stage stage) throws Exception {

    Pane mainPane = PaneUtils.fxmlLoader(MAIN_PANE_FXML);
    Scene scene = new Scene(mainPane);
    stage.setScene(scene);
    stage.setTitle("Praca dyplomowa");
    stage.show();

    DbManager.initDatabase();
}

Z góry dzięki za pomoc.

0

Ty tak poważnie? Weź jak człowiek postaw breakpoint tak gdzie ci leci to NPE i zobacz czemu. Albo pokaż CAŁY KOD tego CommonDao, bo teraz nie widać tam ani findById ani co jest w linii 104 gdzie leci błąd.

Macie pomysł jak to naprawić?

Zamiast jakichś dupa printów może warto zacząć używać debuggera? Szczególnie po 3 latach studiów...

Swoją drogą konstrukcja gdzie robisz jakieś:

try{
    return DaoManager.createDao(connectionSource, cls);
//
} finally {
this.closeDbConnection();
}

Nie ma za bardzo sensu, bo zwróciłeś komuś DAO, tylko że połączenie do bazy juz zamknięte ;D Czy ty rozumiesz jaka jest kolejność wykonywania operacji return i finally? Mam spore wątpliwosci.

2
public static void initDatabase(){
createConnectionSource();
System.out.println("InitCreateConnectionSource");
closeConnectionSource();
System.out.println("InitCloseConnectionSource");
}

Eeee tworzysz połączenie a potem zamykasz?

0

@Shalom: Dzięki za uwagi
Poniżej wklejam kod klasy CommonDao.

public class CommonDao {
    private static final Logger LOGGER = LoggerFactory.getLogger(CommonDao.class);
    protected final JdbcConnectionSource connectionSource;

    public CommonDao() {
        this.connectionSource = DbManager.getConnectionSource();
    }

    public <T extends BaseModel, I> void refresh(BaseModel baseModel) throws ApplicationException {
        try {
            Dao<T, I> dao = getDao((Class<T>) baseModel.getClass());
            dao.refresh((T) baseModel);
        } catch (SQLException e) {
            LOGGER.warn(e.getCause().getMessage());
        } finally {
            this.closeDbConnection();
        }
    }


    public <T extends BaseModel, I> T findById(Class<T> cls, Integer id) throws ApplicationException {
        try {
            Dao<T, I> dao = getDao(cls);
            return dao.queryForId((I) id);
        } catch (SQLException e) {
            LOGGER.warn(e.getCause().getMessage());
            throw new ApplicationException("Error");
        } finally {
            this.closeDbConnection();
        }
    }

  
    public <T extends BaseModel, I> Dao<T, I> getDao(Class<T> cls) throws ApplicationException {
        try {
            return DaoManager.createDao(connectionSource, cls);
        } catch (SQLException e) {
            LOGGER.warn(e.getCause().getMessage()); // W tej linijce leci bład
            throw new ApplicationException("Error");
        } finally {
            this.closeDbConnection();
        }
    }


    private void closeDbConnection() throws ApplicationException {
        try {
            this.connectionSource.close();
        } catch (IOException e) {
            throw new ApplicationException("Error");
        }
    }
}

Jak mogę to przerobić żeby działało? xd

1

Kłamiesz. Error mówi at praca.inzynierska/pl.praca.inzynierska.database.dao.CommonDao.getDao(CommonDao.java:104) a kod który pokazałes ma 50 linijek.

        LOGGER.warn(e.getCause().getMessage()); // W tej linijce leci bład

A skąd wiesz ze exception który dostałeś ma jakieś cause ustawione w ogóle? Pewnie nie ma, stąd NPE. Niemniej cały ten kod nie ma zadnego sensu.

1 użytkowników online, w tym zalogowanych: 0, gości: 1