0

I'm trying to connect java with a mysql database but I am getting a null pointer exception.

Note: I used this method before and it worked well

this is the code :

    public void OpenConnection () throws SQLException{
   try {

        try {
            Class.forName("com.mysql.jdbc.Driver");
            try {
       con = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/hema?"
          + "user=root&password=hima27890");

            } catch (SQLException ex) {
                System.out.print("Connection Not Found");
            }

        } catch (ClassNotFoundException ex) {
            System.out.print("Not found Class");
        }
    } finally {
        System.out.close();
    }




}

and this is the method to return some data in database :

     public String[] GetData(String website) throws ClassNotFoundException, SQLException{
//        System.out.println("\nGet data method");
//        System.out.println("connection will start now");
       OpenConnection();
         System.out.println("connection works");

        String queryget="SELECT * FROM hema.passwords WHERE website='"+website+"'";

        Statement stm=(Statement) con.createStatement();

        ResultSet rs;
        rs = stm.executeQuery(queryget);

        if(rs.next()){
        System.out.println("if works");
             arr[0]=rs.getString("email");
        arr[1]=rs.getString("username");
        arr[2]=rs.getString("password");

        }
        else{

        System.out.println("The website doesn't exist");
        }
        System.out.println("method done");
        return arr;
    }



}

I put some System.out.print to know where is the error and all of them forked but not tje last one "method done".

and this is the code in the JForm that call the last method :

 private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         

    try {
        String Website = jTextField1.getText();
        System.out.println("Website is "+Website);
        Model model=new Model();
        String arr2[]=model.GetData(Website);
        System.out.println("App");
        EmailText.setText(arr2[0]);
        UserText.setText(arr2[1]);
        PasswordText.setText(arr2[2]);

    } catch (ClassNotFoundException ex) {
        Logger.getLogger(Passwords.class.getName()).log(Level.SEVERE, null, ex);
    } catch (SQLException ex) {
        Logger.getLogger(Passwords.class.getName()).log(Level.SEVERE, null, ex);
    }





}       

the stack trace:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Model.Model.GetData(Model.java:68)
at View.Passwords.jButton1ActionPerformed(Passwords.java:161)
at View.Passwords.access$200(Passwords.java:15)
at View.Passwords$3.actionPerformed(Passwords.java:64)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2346)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6527)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6292)
at java.awt.Container.processEvent(Container.java:2234)
at java.awt.Component.dispatchEventImpl(Component.java:4883)
at java.awt.Container.dispatchEventImpl(Container.java:2292)
at java.awt.Component.dispatchEvent(Component.java:4705)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4898)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4533)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4462)
at java.awt.Container.dispatchEventImpl(Container.java:2278)
at java.awt.Window.dispatchEventImpl(Window.java:2739)
at java.awt.Component.dispatchEvent(Component.java:4705)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:746)
at java.awt.EventQueue.access$400(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:697)
at java.awt.EventQueue$3.run(EventQueue.java:691)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.awt.EventQueue$4.run(EventQueue.java:719)
at java.awt.EventQueue$4.run(EventQueue.java:717)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:716)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

BUILD SUCCESSFUL (total time: 21 seconds)

1 Answers1

0

ok I knew where is the problem it was in declaring array of strings in the beginning of the code so it didn't change when i used the if statement so I used array ArrayList<String>