I am getting a Null Pointer Exception while executing the insert query. Everything seems to be fine but the problem still exists.
Code used for Database Connection.
public class DBConnect
{
static Connection c ;
static Statement st ;
{
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
c=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","ashuthesmartest","ashutosha");
st=c.createStatement();
}
catch (Exception ex)
{
JOptionPane.showMessageDialog(null, "Database error");
}
}
}
Action Performed on Button Click
private void b3ActionPerformed(java.awt.event.ActionEvent evt) {
try
{
char[] arr = pa1.getPassword() ;
String s2 = Arrays.toString(arr) ;
String s1 = t3.getText() ;
DBConnect.st.executeUpdate("insert into LOGIN values('"+s1+"','"+s2+"')"); **//EXCEPTION IN THIS LINE**
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
The initialization block in which you create the connection and the statement is not a static initialization block.
Therefore, it will only be executed when you create an instance of the class DBConnect
.
Since you seem to be using DBConnect
only statically, that never happens. Your initialization block should be made static. A static initialization block has the keyword static
preceding the left brace:
static {
// try etc.
}