Page 1 of 3 123 LastLast
Results 1 to 20 of 58
  1. #1
    Revoke is offline Member
    Join Date
    Aug 2011
    Posts
    25
    Rep Power
    0

    Default [MySQL][Jar] Problems outside the IDE

    My code works very well on NetBeans but I can't run the .jar file in command line, I received these errors:

    Exception in thread "main" java.lang.NullPointerException
    at cadastro.dao.DadosDao.findDados(DadosDao.java:44)
    at cadastro.controller.DadosController.listaDados(Dad osController.java:68)
    at cadastro.Principal.<init>(Principal.java:33)
    at cadastro.Cadastro.main(Cadastro.java:12)

    These are the codes near the errors

    DadosDao (findDados around line 44)

    Java Code:
    public List<Dados> findDados() throws SQLException 
        {
            List<Dados> dadoss = new ArrayList<Dados>();
    
            String select = "SELECT * FROM dados";
    
            PreparedStatement stmt = getConnection().prepareStatement(select); //Line 44
            ResultSet rs = stmt.executeQuery();
    DadosController( listaDados, around line 68)
    Java Code:
    public List<Dados> listaDados() 
        {
            DadosDao dao = new DadosDao();
            try 
            {
                return dao.findDados(); //Line 68
    	} 
            catch (SQLException e) 
            {
                
                JOptionPane.showMessageDialog(null, "Problemas ao localizar contato\n" + e.getLocalizedMessage());
    
            }
            
            return null;
    	    
        }
    Principal (around line 33):
    Java Code:
    private JFileChooser flImagem;
        private JButton btnSalvar, btnAlterar, btnExcluir, btnClear, btnLocalizar;
        private JButton btnPrimeiro, btnProximo, btnAnterior, btnUltimo, btnAbrir;
        private JButton btnCarregar;
        private List<Dados> contatoList = new DadosController().listaDados(); //Line 33
        private int registroAtual = 0;

  2. #2
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    356
    Rep Power
    5

    Default

    How are you running the jar from the command line, what are you typing in?

    If its runnning ok in the IDE, could be a classpath issue. Check the manifest file and see what the path is to the class that has your main method. Probably something like packagename.className. Then try to run it using "java -cp packagename.classname MyJarFile.jar

  3. #3
    Revoke is offline Member
    Join Date
    Aug 2011
    Posts
    25
    Rep Power
    0

    Default

    I'm doing this
    java -jar "/home/lsa/NetBeansProjects/Cadastro/dist/Cadastro.jar" (suggested by the IDE)

    I tried the method you just said and got theses errors

    Exception in thread "main" java.lang.NoClassDefFoundError: Cadastro.jar
    at gnu.java.lang.MainThread.run(libgcj.so.11)
    Caused by: java.lang.ClassNotFoundException: Cadastro.jar not found in gnu.gcj.runtime.SystemClassLoader{urls=[], parent=gnu.gcj.runtime.ExtensionClassLoader{urls=[], parent=null}}
    at java.net.URLClassLoader.findClass(libgcj.so.11)
    at gnu.gcj.runtime.SystemClassLoader.findClass(libgcj .so.11)
    at java.lang.ClassLoader.loadClass(libgcj.so.11)
    at java.lang.ClassLoader.loadClass(libgcj.so.11)
    at gnu.java.lang.MainThread.run(libgcj.so.11)

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,458
    Rep Power
    25

    Default

    The error you get on line 44 could be caused by getConnection() returning a null value.
    Break line 44 into separate steps and print out the value returned by getConnection() to see if it is null.

    The instructions in post#2 are not going to help. You are executing the code ok, but are having a problem there.

    The instructions from post#2 should say something like this:
    java -cp MyJarFile.jar;. cadastro.Cadastro

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,016
    Rep Power
    20

    Default

    What Norm said.
    I would lay good odds that your getConnection() is actually eating an exception and returning a null Connection.
    That exception is quite likely to be a ClassNotFoundException (the MySQL connector jar isn't where it should be), or a SQLException (failed to connect).

  6. #6
    Revoke is offline Member
    Join Date
    Aug 2011
    Posts
    25
    Rep Power
    0

    Default Re: [MySQL][Jar] Problems outside the IDE

    the value of getConnection is right, at least on the IDE, it returns the value
    com.mysql.jdbc.JDBC4Connection@19fcc69

    I really don't know what else to do...

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,016
    Rep Power
    20

    Default Re: [MySQL][Jar] Problems outside the IDE

    Then the above code is not the source of that errror.
    Line 44 only has one thing on it that can be the cause of your NPE and that is the return value of getConnection() has to be null.

    Since you haven't shown us your getConnection() code we can't say why.

    Rewrite the top method as:
    Java Code:
    public List<Dados> findDados() throws SQLException 
        {
            List<Dados> dadoss = new ArrayList<Dados>();
    
            String select = "SELECT * FROM dados";
    
            Connection connection = getConnection();
            System.out.println(connection);
            PreparedStatement stmt = connection.prepareStatement(select);
            ResultSet rs = stmt.executeQuery();
    and check what the value of connection is.

  8. #8
    Revoke is offline Member
    Join Date
    Aug 2011
    Posts
    25
    Rep Power
    0

    Default Re: [MySQL][Jar] Problems outside the IDE

    The value is "com.mysql.jdbc.JDBC4Connection@253498"

    The code for getConnection is here

    Java Code:
    public static Connection getConnection() 
    {
            System.out.println("Conectando ao Banco de Dados");
            
            try 
            {
                //Carrega o Driver do Banco
                Class.forName(DRIVER_CLASS);
                Connection conn = DriverManager.getConnection(URL_MYSQL, USER, PASS);
                if (conn != null) {
    
                    System.out.println("STATUS--->Conectado com sucesso!");
                    System.out.println("conn =" +conn);
    
                } else {
    
                    System.out.println("STATUS--->Não foi possivel realizar conexão");
    
                }
                return conn;
                
            } 
            
            catch (ClassNotFoundException e) 
            {
                System.out.println("O driver expecificado nao foi encontrado.");
                //e.printStackTrace();
            } 
            catch (SQLException e) 
            {
                System.out.println("Nao foi possivel conectar ao Banco de Dados.");
                //throw new RuntimeException(e);
                return null;
            }
            return null;
        }
    }

  9. #9
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,458
    Rep Power
    25

    Default Re: [MySQL][Jar] Problems outside the IDE

    What is the text of the error messaged now?

  10. #10
    Revoke is offline Member
    Join Date
    Aug 2011
    Posts
    25
    Rep Power
    0

    Default Re: [MySQL][Jar] Problems outside the IDE

    The same message as before

  11. #11
    Revoke is offline Member
    Join Date
    Aug 2011
    Posts
    25
    Rep Power
    0

    Default Re: [MySQL][Jar] Problems outside the IDE

    But outside the IDE it gets the value NULL instead of the value that is used to be....

  12. #12
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,540
    Blog Entries
    7
    Rep Power
    20

    Default Re: [MySQL][Jar] Problems outside the IDE

    Quote Originally Posted by Revoke View Post
    But outside the IDE it gets the value NULL instead of the value that is used to be....
    Is the .jar file that contains your JDBC driver on your classpath?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  13. #13
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,458
    Rep Power
    25

    Default Re: [MySQL][Jar] Problems outside the IDE

    Please post the full text of the error message.

    What variable is null? Why is that variable null? Add lots of printlns to show the contents of the variable to see where it is being set to null.

  14. #14
    Revoke is offline Member
    Join Date
    Aug 2011
    Posts
    25
    Rep Power
    0

    Default Re: [MySQL][Jar] Problems outside the IDE

    Quote Originally Posted by JosAH View Post
    Is the .jar file that contains your JDBC driver on your classpath?

    kind regards,

    Jos
    No it doesn't, how can I fix this? I really new to Java and .jar files, on my .jar iot only contains the Classes

    Norm
    the return variable of getConnectiojn is Null, but it happens only outside the IDE

  15. #15
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,458
    Rep Power
    25

    Default Re: [MySQL][Jar] Problems outside the IDE

    That would be because of the missing class definitions that are in the missing jar file.
    You need to add the jar file to the classpath for the java program.
    How do you execute your program outside of the IDE?

  16. #16
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,016
    Rep Power
    20

    Default Re: [MySQL][Jar] Problems outside the IDE

    Java Code:
            catch (ClassNotFoundException e) 
            {
                System.out.println("O driver expecificado nao foi encontrado.");
                //e.printStackTrace();
            } 
            catch (SQLException e) 
            {
                System.out.println("Nao foi possivel conectar ao Banco de Dados.");
                //throw new RuntimeException(e);
                return null;
            }
            return null;
        }
    This is your bug (as I said earlier).
    You are eating your exceptions.
    You are also treating the failure to actually get a connection (which is the purpose of this method) as a perfectly reasonable event. It isn't. It is a failure in your system and should be throwing an exception, not returning null.

  17. #17
    Revoke is offline Member
    Join Date
    Aug 2011
    Posts
    25
    Rep Power
    0

    Default Re: [MySQL][Jar] Problems outside the IDE

    Norm
    I'm using this to run the .jar file

    java -jar "/home/lsa/NetBeansProjects/Cadastro/dist/Cadastro.jar"

    How do I add the jar file to the classpath?

    Tolls

    With those exception I can see many errors, like :

    Exception in thread "main" java.lang.RuntimeException: com.mysql.jdbc.exceptions.MySQLSyntaxErrorExceptio n: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '????????????????' at line 1
    at cadastro.dao.ConnectionDataBase.getConnection(Conn ectionDataBase.java:49)
    at cadastro.dao.GenericDao.<init>(GenericDao.java:19)
    at cadastro.dao.DadosDao.<init>(DadosDao.java:16)
    at cadastro.controller.DadosController.listaDados(Dad osController.java:65)
    at cadastro.Principal.<init>(Principal.java:33)
    at cadastro.Cadastro.main(Cadastro.java:12)
    Caused by: com.mysql.jdbc.exceptions.MySQLSyntaxErrorExceptio n: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '????????????????' at line 1
    at com.mysql.jdbc.SQLError.createSQLException(SQLErro r.java:1049)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.ja va:3597)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.ja va:3529)
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:19 90)
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java :2151)
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionIm pl.java:2619)
    at com.mysql.jdbc.ConnectionImpl.configureClientChara cterSet(ConnectionImpl.java:1881)
    at com.mysql.jdbc.ConnectionImpl.initializePropsFromS erver(ConnectionImpl.java:3496)
    at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(Co nnectionImpl.java:2385)
    at com.mysql.jdbc.ConnectionImpl.createNewIO(Connecti onImpl.java:2154)
    at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImp l.java:792)
    at com.mysql.jdbc.ConnectionImpl.getInstance(Connecti onImpl.java:377)
    at com.mysql.jdbc.NonRegisteringDriver.connect(NonReg isteringDriver.java:305)
    at java.sql.DriverManager.getConnection(libgcj.so.11)
    at java.sql.DriverManager.getConnection(libgcj.so.11)
    at cadastro.dao.ConnectionDataBase.getConnection(Conn ectionDataBase.java:26)
    ...5 more


    But I don't know how to solve any of them, the same errors as before...just with the databases errors now

  18. #18
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,458
    Rep Power
    25

    Default Re: [MySQL][Jar] Problems outside the IDE

    You can add a Class-path: entry to the manifest file in your jar file.

  19. #19
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,016
    Rep Power
    20

    Default Re: [MySQL][Jar] Problems outside the IDE

    What versions of things do you have?
    Java, JDBC driver, MySQL, that sort of thing.
    Looks like you have a mismatch.

    Or something's up with your username and password, or connection string possibly?

    You also need to tell us where you are getting these exceptions, what system, and whether this is via the IDE.

    And what the getConnection() code now looks like.

  20. #20
    Revoke is offline Member
    Join Date
    Aug 2011
    Posts
    25
    Rep Power
    0

    Default Re: [MySQL][Jar] Problems outside the IDE

    The code of getConnection()
    Java Code:
    public class ConnectionDataBase {
    	 
        private static final String URL_MYSQL = "jdbc:mysql://localhost:3306/hospital";
        private static final String DRIVER_CLASS = "com.mysql.jdbc.Driver";
        private static final String USER = "root";
        private static final String PASS = "lsa1234";
    
    public static Connection getConnection() 
    {
            System.out.println("Conectando ao Banco de Dados");
            
            try 
            {
                //Carrega o Driver do Banco
                Class.forName(DRIVER_CLASS);
                Connection conn = DriverManager.getConnection(URL_MYSQL, USER, PASS);
                if (conn != null) {
    
                    System.out.println("STATUS--->Conectado com sucesso!");
    
                } else {
    
                    System.out.println("STATUS--->Não foi possivel realizar conexão");
    
                }
                return conn;
                
            } 
            
            catch (ClassNotFoundException e) 
            {
                System.out.println("O driver expecificado nao foi encontrado.");
                e.printStackTrace();
            } 
            catch (SQLException e) 
            {
                System.out.println("Nao foi possivel conectar ao Banco de Dados.");
                throw new RuntimeException(e);
    
            }
            return null;
        }
    }
    JDBC driver: 5.1.17
    MYSQL: 5.1.54
    Java: JDK 6 Update 27 with NetBeans 7.0.1
    I'm using Linux Ubuntu 11.04

    The password and username are ok!

Page 1 of 3 123 LastLast

Similar Threads

  1. Problems using MySQL database
    By Antrim in forum Eclipse
    Replies: 1
    Last Post: 02-16-2011, 04:57 AM
  2. Problems with MySQL Driver
    By islan in forum JDBC
    Replies: 7
    Last Post: 08-06-2009, 04:47 PM
  3. MySQL/JDBC Mysql query output
    By thelinuxguy in forum Advanced Java
    Replies: 4
    Last Post: 02-13-2009, 01:57 AM
  4. hello the community & mysql connecting problems
    By scchia in forum New To Java
    Replies: 6
    Last Post: 07-16-2008, 08:49 AM
  5. Replies: 0
    Last Post: 04-22-2008, 11:57 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •