I use the following code to retrieve the data from MySql database. GContnStr
is a connection method I call to connect to the database.But,when I use this code, I got the following exceptions. How can I solve this problem?
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)
This problem has nothing to do with the database. Your problem is right here:
String PoptnStr;
options val = options.valueOf(PoptnStr);
You create your String
which is initialized to null
, then immediately use it, so you've written options.valueOf(null);
Also, by convention, class names should start with capital letters (ie options
should be Options
) and instance variable names should be camel case (ie PoptnStr
should be poptnStr
).
The initialization of your field
options val = options.valueOf(PoptnStr);
causes the NPE. At the point this code is executed there is no value assigned to PoptnStr
. Move the initialization to the constructor and make sure PoptnStr
is already set will fix this
can you try after initialize the PoptnStr either as below
String PoptnStr = options.DBQryDef.name();
String PoptnStr = options.DBTABLE.name();
And didnt understand why you are switching on val for DBTABLE and DBQryDef and also comparing with DBTABLE null string:
//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)
{
Can you elaborate it a bit more?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.