我正在练习一个Java Netbeans程序,我在MySql数据库中调用值,但它给了我这个Null Pointer的例外。我是新手,所以请帮我解决这个问题。
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)
这是我的代码:
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));
}
答案 0 :(得分:0)
我可以看到DriverManager.getConnection
返回null
的两个可能原因。
JDBC URL无效。请检查您的MySQL服务器是否在默认端口(3306)上的计算机上运行。如果没有,请在URL中明确包含该端口。然后,coolingSystem
应该是服务器上现有数据库的名称(不是表或其他内容)。最后,请仔细检查您的凭据。
驱动程序未包含在运行时类路径中。 Class.forName
是否会抛出任何异常/错误?请为此目的检查日志。
答案 1 :(得分:0)
你的类路径中有mysql-connector吗?似乎Connection为null。 测试Connection是否为空并将其打印出来,只是为了检查。