我使用以下代码从MySql数据库中检索数据。 GContnStr
是我调用连接数据库的连接方法。但是,当我使用此代码时,我得到以下异常。我该如何解决这个问题?
package designstudent;
import java.sql.*;
import java.awt.event.*;
import java.beans.EventHandler;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.table.DefaultTableModel;
import java.sql.*;
import java.util.ArrayList;
import java.util.jar.Attributes.Name;
import javax.swing.JOptionPane;
import javax.swing.ListSelectionModel;
import java.util.ArrayList.*;
import java.util.Vector;
import javax.swing.*;
public class combobutton extends javax.swing.JFrame {
Statement TmpStmnt=null;
ResultSet TmpDetlRs=null;
GContnStr GCS=new GContnStr();
String GStrSql=null;
private String PFldname="StudClass";
private String PTName="studentmaster";
enum options{
DBTABLE,
DBQryDef
}
String PoptnStr;
options val = options.valueOf(PoptnStr);
public combobutton() throws SQLException {
combo(PoptnStr, TmpStmnt, TmpDetlRs, GStrSql, PFldname, PTName);
initComponents();
}
public void combo(String PoptnStr,Statement TmpStmnt,ResultSet TmpDetlRs,String StrSql,String PFldName,String PTName) throws SQLException
{
cbx1.removeAllItems();
cbx2.removeAllItems();
cbx3.removeAllItems();
String DBTABLE = null;
try{
switch(val)
{
case DBTABLE:
case DBQryDef:
if(PoptnStr==DBTABLE)
{
GCS.GContnStr();
GStrSql="select '"+ PFldName+"' from '"+PTName+"'";
TmpDetlRs=TmpStmnt.executeQuery(GStrSql);
while(TmpDetlRs.next())
{
String TmpOb1=TmpDetlRs.getString("StudClass");
System.out.println(TmpOb1);
}
}
}
}
catch(Exception e){
System.out.println(e);
}
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
try {
new combobutton().setVisible(true);
} catch (SQLException ex) {
Logger.getLogger(combobutton.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
}
// Variables declaration - do not modify
private javax.swing.JComboBox cbx1;
private javax.swing.JComboBox cbx2;
private javax.swing.JComboBox cbx3;
// End of variables declaration
}
ERROR
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException: Name is null
at java.lang.Enum.valueOf(Enum.java:236)
at designstudent.combobutton$options.valueOf(combobutton.java:30)
at designstudent.combobutton.<init>(combobutton.java:37)
at designstudent.combobutton$1.run(combobutton.java:122)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:660)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:211)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:128)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:117)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:113)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
BUILD SUCCESSFUL (total time: 1 second)
答案 0 :(得分:2)
此问题与数据库无关。你的问题就在这里:
String PoptnStr;
options val = options.valueOf(PoptnStr);
您创建的String
已初始化为null
,然后立即使用,因此您已撰写options.valueOf(null);
此外,按照惯例,类名应以大写字母开头(即options
应为Options
),实例变量名称应为驼峰(即PoptnStr
应为{{1} }})。
答案 1 :(得分:1)
您的字段的初始化
options val = options.valueOf(PoptnStr);
导致NPE。在执行此代码时,没有为PoptnStr
分配值。将初始化移动到构造函数并确保已设置PoptnStr
将修复此
答案 2 :(得分:1)
你可以尝试初始化PoptnStr,如下所示
String PoptnStr = options.DBQryDef.name();
String PoptnStr = options.DBTABLE.name();
并且不明白为什么要为DBTABLE和DBQryDef启用val并且还要与DBTABLE null字符串进行比较:
//assigning the val variable
options val = options.valueOf(PoptnStr);
//..... your other code
//And comparing it as below:
String DBTABLE = null;
try{
switch(val)
{
case DBTABLE:
case DBQryDef:
if(PoptnStr==DBTABLE)
{
你能详细说明一下吗?