Вопрос

I'm practicing a Java Netbeans program where I'm calling values from MySql Database but it's giving me this Exception of Null Pointer. I'm new to this so please help me solve this problem.

java.lang.NullPointerException
at cooling.system.pkg1.pkg0.Calculation1.calculate(Calculation1.java:371)
at cooling.system.pkg1.pkg0.Calculation1.btn_setActionPerformed(Calculation1.java:1081)
at cooling.system.pkg1.pkg0.Calculation1.access$1000(Calculation1.java:22)
at cooling.system.pkg1.pkg0.Calculation1$11.actionPerformed(Calculation1.java:862)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
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:6505)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3320)
at java.awt.Component.processEvent(Component.java:6270)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4861)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2719)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:735)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:694)
at java.awt.EventQueue$3.run(EventQueue.java:692)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:708)
at java.awt.EventQueue$4.run(EventQueue.java:706)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:705)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)

and this is my code:

public void calculate() throws ClassNotFoundException
{
    if(!(chckbx_current_value.isSelected()))
    {
        JOptionPane.showMessageDialog(this, "Select Current value");
    }
    if(chckbx_big_close_value.isSelected() && chckbx_small_close_value.isSelected())
    {
        JOptionPane.showMessageDialog(this, "Select Only one closest value");
        chckbx_big_close_value.setSelected(false);
        chckbx_small_close_value.setSelected(false);
    }
    else if(!(chckbx_big_close_value.isSelected()) && !(chckbx_small_close_value.isSelected()))
    {
        JOptionPane.showMessageDialog(this, "Select Atleast one closest value");
    }
    Connection con=null;
    try 
    {
                Class.forName("com.mysql.jdbc.Driver");
                con = DriverManager.getConnection("jdbc:mysql://localhost/coolingSystem","root","password");
                String small = "select * from dryAir_properties where T = "+closest_small+"";
                PreparedStatement ps_small = con.prepareStatement(small);
                ResultSet rs_small = ps_small.executeQuery();


                String big = "select * from dryAir_properties where T = "+closest_big+"";
                PreparedStatement ps_big = con.prepareStatement(big);
                ResultSet rs_big = ps_big.executeQuery();
                while(rs_small.next() && rs_big.next())
                {
                    Cp_big = Float.valueOf(rs_big.getString(3));
                    k_big = Float.valueOf(rs_big.getString(4));
                    viscosity_big = Float.valueOf(rs_big.getString(5));
                    density_big = Float.valueOf(rs_big.getString(6));

                    Cp_small = Float.valueOf(rs_small.getString(3));
                    k_small = Float.valueOf(rs_small.getString(4));
                    viscosity_small = Float.valueOf(rs_small.getString(5));
                    density_small = Float.valueOf(rs_small.getString(6));
                } 
    }
    catch (NumberFormatException | SQLException  e) 
    {
        System.out.println("Error...... "+e);
    }

    float Cp = (((tma - closest_small)*(Cp_big - Cp_small))/(closest_big - closest_small)) + Cp_big;
    float k = (((tma - closest_small)*(k_big - k_small))/(closest_big - closest_small)) + k_big;
    float viscosity = (((tma - closest_small)*(viscosity_big - viscosity_small))/(closest_big - closest_small)) + viscosity_big;
    float density = (((tma - closest_small)*(density_big - density_small))/(closest_big - closest_small)) + density_big;
    txt_Cp_a.setText(String.valueOf(Cp));
    txt_Conductivity_a.setText(String.valueOf(k));
    txt_Viscosity_a.setText(String.valueOf(viscosity));
    txt_Density_a.setText(String.valueOf(density));
}
Это было полезно?

Решение

I can see 2 possible causes why DriverManager.getConnection returns null.

  1. The JDBC URL is invalid. Please check if your MySQL server runs on your machine on the default port (3306). If not, please include the port explicitly in the URL. Then, coolingSystem should be the name of an existing database on your server (not a table or something else). Last, please double check your credentials.

  2. The driver is not included in the runtime classpath. Does Class.forName throw any exception/error? Please check the logs for this purpose.

Другие советы

Do you have mysql-connector in your classpath? It seems like Connection is null. Test if Connection is null and print it out, just to check.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top