Vydělávej až 160.000 Kč měsíčně! Akreditované rekvalifikační kurzy s garancí práce od 0 Kč. Více informací.
Hledáme nové posily do ITnetwork týmu. Podívej se na volné pozice a přidej se do nejagilnější firmy na trhu - Více informací.

Diskuze: JAVAFX POMOC

Aktivity
Avatar
mimacala
Člen
Avatar
mimacala:16.3.2018 20:44

Error, prosím o pomoc už mám nervy a nevím co s tím :D .....
Jedná se jednoduchou Login aplikaci s pomocí SQLite, myslím,že se to ani nepřipojí k DB. ...
Předem děkuji moc všem za ochotu, protože prolouskat se tím zabere docela čas a námahu, .. :)

java.lang.NullPointerException
        at mim.LoginModel.isLogin(LoginModel.java:49)
        at mim.loginController.Kontrola(loginController.java:38)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        at sun.reflect.misc.Trampoline.invoke(Unknown Source)
        at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        at sun.reflect.misc.MethodUtil.invoke(Unknown Source)
        at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1769)
        at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1657)
        at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
        at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
        at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
        at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
        at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
        at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
        at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
        at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
        at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
        at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
        at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
        at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
        at javafx.event.Event.fireEvent(Event.java:198)
        at javafx.scene.Node.fireEvent(Node.java:8413)
        at javafx.scene.control.Button.fire(Button.java:185)
        at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:182)
        at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:96)
        at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:89)
        at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
        at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
        at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
        at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
        at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
        at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
        at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
        at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
        at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
        at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
        at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
        at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
        at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
        at javafx.event.Event.fireEvent(Event.java:198)
        at javafx.scene.Scene$MouseHandler.process(Scene.java:3757)
        at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485)
        at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
        at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
        at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:381)
        at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295)
        at java.security.AccessController.doPrivileged(Native Method)
        at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$353(GlassViewEventHandler.java:417)
        at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
        at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:416)
        at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
        at com.sun.glass.ui.View.notifyMouse(View.java:937)
        at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
        at com.sun.glass.ui.win.WinApplication.lambda$null$147(WinApplication.java:177)
        at java.lang.Thread.run(Unknown Source)

Připojení

package mim;
import java.sql.*;

public class SqliteConnection {

        public static Connection Connector() {
                try {
                Class.forName("org.sqlite.JDBC");
                Connection conn = DriverManager.getConnection("jdbc:sqlite:db_min.sqlite");
                return conn;

                } catch (Exception e) {
                        System.out.println(e);
                        return null;

                }


        }
}

Login Model

package mim;
import java.sql.*;

public class LoginModel {
        Connection connection;

        public LoginModel() {
                connection = SqliteConnection.Connector();
                if (connection == null) {
                        System.out.println("Není připojeno");
                        System.exit(1);
                }
        }

        public boolean isDBConnected() {
                try {
                        return !connection.isClosed();
                } catch (SQLException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                        return false;
                }

        }

        public boolean isLogin(String JMN, String HSL) throws SQLException {
                PreparedStatement preparedStatement = null;
                ResultSet resultSet = null;
                String query = "select * from uzivatele where nick = ? and heslo = ?";


                try {
                        preparedStatement = connection.prepareStatement(query);
                        preparedStatement.setString(1, JMN);
                        preparedStatement.setString(2, HSL);

                        resultSet = preparedStatement.executeQuery();
                        if(resultSet.next()) {
                                return true;
                        } else {
                                return false;
                        }
                } catch (Exception e) {
                        return false;



                } finally {
                        preparedStatement.close();
                        resultSet.close();
                }
        }

}

a Controller

package mim;

import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;

import javafx.scene.control.TextField;

import java.net.URL;
import java.util.ResourceBundle;

import javafx.event.ActionEvent;

import javafx.scene.control.Label;

import javafx.scene.control.PasswordField;

public class loginController implements Initializable{
        public LoginModel loginModel = new LoginModel();


        @FXML
        private Label popis;
        @FXML
        private Label status;
        @FXML
        private PasswordField heslo;
        @FXML
        private TextField jmeno;
        @FXML
        private Button prihlasit;



        @FXML
        public void Kontrola(ActionEvent event) {
                try {
                        if(loginModel.isLogin(jmeno.getText(), heslo.getText())) {
                                popis.setText("Údaje jsou v pořádku");
                        } else {
                         popis.setText("Neplatné přihlašovací údaje!1");
                        }
                } catch (Exception e) {
                        popis.setText("Neplatné přihlašovací údaje2!");
                        e.printStackTrace();
                }
        }


        @Override
        public void initialize(URL location, ResourceBundle resources) {
                if(loginModel.isDBConnected()) {
                        status.setText("DB připojena");
                } else {
                        status.setText("DB nepřipojena");
                }

        }





}

kdyby náhodou, tak i main

package mim;

import java.io.IOException;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.scene.Parent;
import javafx.scene.Scene;


public class Main extends Application {



        @Override
        public void start(Stage stage) {
                try {
                        Parent root = FXMLLoader.load(getClass().getResource("login.fxml"));
                        Scene scene = new Scene(root);

                        stage.setScene(scene);
                        stage.show();

                } catch (IOException e) {

                        e.printStackTrace();
                }

        }







        public static void main(String[] args) {
                launch(args);
        }
}
 
Odpovědět
16.3.2018 20:44
Avatar
Odpovídá na mimacala
Patrik Valkovič:16.3.2018 21:41

Tipl bych si, že preparedStatement ve finally bloku LoginModel.isLogin je null, tedy preparedStatement = connection.prepareStatement(query); vyhodí výjimku. Tu pomocí debugeru zachyť a zjisti, proč tam je.

Editováno 16.3.2018 21:42
Nahoru Odpovědět
16.3.2018 21:41
Nikdy neumíme dost na to, abychom se nemohli něco nového naučit.
Avatar
Robert Michalovič:17.3.2018 6:30
  1. https://www.itnetwork.cz/…otaz-ve-foru -> název POMOC fakt tupé
  2. nauč se streamy a objekty pořádně

padá to ve finaly a to proto že zavíráš dřív preparedStatement než resultSet. Existuje pravidlo, že streamy(objekty) navazující na sebe se likvidují přesně opačném pořadí. Takže pokud první ukončíš resultSet a po něm preparedStatement vše bude v pořádku.

} finally {
                        preparedStatement.close();
                        resultSet.close();
}

změn na toto

} finally {
                        resultSet.close();
                        preparedStatement.close();
}
 
Nahoru Odpovědět
17.3.2018 6:30
Avatar
mimacala
Člen
Avatar
mimacala:17.3.2018 8:49

Přehozeno, bohužel stejná chyba :/ a ano zkušenosti nad zlato, proto jsem také vděčný za každou radu :)

 
Nahoru Odpovědět
17.3.2018 8:49
Avatar
Atrament
Tvůrce
Avatar
Odpovídá na mimacala
Atrament:17.3.2018 10:43

Zřejmě zadáváš špatné přihlašovací údaje a preparedStatement tak zůstane null. Měl bys před pokusem ho uzavřit nejprve otestovat zda není null:

} finaly {
   if (preparedStatement != null) {
      preparedStatement.close();
}

a ještě lépe rovnou požívat novější try-with-resources konstrukci:

try (PreparedStatement preparedStatement = connection.prepareStatement(query)) {
    //práce s preparedStatement...
} catch (SQLException e) {
   //reakce na odchycenou výjimku, btw měl bys správně odchytávat SQLException místo obyčejné Exception...
}
//větev finally s preparedStatement.close() není potřeba, o správné uzavření je postaráno za tebe...

více o try-with-resources: https://docs.oracle.com/…ceClose.html

taky

if(resultSet.next()) {
   return true;
} else {
   return false;
}

se dá nahradit jednoduchým

return resultSet.next();
Akceptované řešení
+20 Zkušeností
+2,50 Kč
Řešení problému
 
Nahoru Odpovědět
17.3.2018 10:43
Avatar
mimacala
Člen
Avatar
mimacala:17.3.2018 11:23

Všem moc děkuji za pomoc a ochotu :) již vše funguje v pořádku.
Nakonec jsem slepej .. , ale kód jsem upravil podle rad, jsem velmi vděčný a ještě jednou moc děkuji :)

public class SqliteConnection {

        public static Connection Connector() {
                try {
                Class.forName("org.sqlite.JDBC");
                Connection conn = DriverManager.getConnection("jdbc:sqlite:db_min.sqlite");  //zde má být mim.sqlite
                return conn;

                } catch (Exception e) {
                        System.out.println(e);
                        return null;

                }


        }
 
Nahoru Odpovědět
17.3.2018 11:23
Děláme co je v našich silách, aby byly zdejší diskuze co nejkvalitnější. Proto do nich také mohou přispívat pouze registrovaní členové. Pro zapojení do diskuze se přihlas. Pokud ještě nemáš účet, zaregistruj se, je to zdarma.

Zobrazeno 6 zpráv z 6.