Tuesday, September 17, 2013

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException in Netbeans

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException in
Netbeans

there is a program for gui
/* * To change this template, choose Tools | Templates * and open the
templaenter code herete in the editor. */ package loginpage;
import javax.swing.JOptionPane;
/** * * @author Parth */ public class NewJFrame extends javax.swing.JFrame {
/**
* Creates new form NewJFrame
*/
regidatabase1 db;
public NewJFrame() {
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jLabel2 = new javax.swing.JLabel();
button1 = new javax.swing.JButton();
jLabel3 = new javax.swing.JLabel();
uname1 = new javax.swing.JTextField();
ppass1 = new javax.swing.JPasswordField();
jLabel4 = new javax.swing.JLabel();
jLabel5 = new javax.swing.JLabel();
jLabel6 = new javax.swing.JLabel();
jLabel7 = new javax.swing.JLabel();
fname = new javax.swing.JTextField();
lname = new javax.swing.JTextField();
uname = new javax.swing.JTextField();
ppass = new javax.swing.JPasswordField();
button = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel2.setText("UserName");
button1.setText("Login");
button1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
button1ActionPerformed(evt);
}
});
jLabel3.setText("PassWord");
jLabel4.setText("FirstName");
jLabel5.setText("LastName");
jLabel6.setText("PassWord");
jLabel7.setText("Username");
button.setText("Registration");
button.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
buttonActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new
javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(85, 85, 85)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING,
false)
.addComponent(jLabel3,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
Short.MAX_VALUE)
.addComponent(jLabel2,
javax.swing.GroupLayout.DEFAULT_SIZE, 58,
Short.MAX_VALUE)
.addComponent(jLabel4,
javax.swing.GroupLayout.Alignment.TRAILING,
javax.swing.GroupLayout.DEFAULT_SIZE, 58,
Short.MAX_VALUE)
.addComponent(jLabel5,
javax.swing.GroupLayout.DEFAULT_SIZE, 58,
Short.MAX_VALUE)
.addComponent(jLabel7,
javax.swing.GroupLayout.DEFAULT_SIZE, 58,
Short.MAX_VALUE)
.addComponent(jLabel6,
javax.swing.GroupLayout.DEFAULT_SIZE, 58,
Short.MAX_VALUE))
.addGap(74, 74, 74)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING,
false)
.addComponent(uname1,
javax.swing.GroupLayout.DEFAULT_SIZE, 100,
Short.MAX_VALUE)
.addComponent(ppass1)
.addComponent(fname,
javax.swing.GroupLayout.Alignment.TRAILING,
javax.swing.GroupLayout.DEFAULT_SIZE, 100,
Short.MAX_VALUE)
.addComponent(lname,
javax.swing.GroupLayout.Alignment.TRAILING,
javax.swing.GroupLayout.DEFAULT_SIZE, 100,
Short.MAX_VALUE)
.addComponent(uname,
javax.swing.GroupLayout.DEFAULT_SIZE, 100,
Short.MAX_VALUE)
.addComponent(ppass,
javax.swing.GroupLayout.DEFAULT_SIZE, 100,
Short.MAX_VALUE)))
.addGroup(layout.createSequentialGroup()
.addGap(186, 186, 186)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(button)
.addComponent(button1))))
.addContainerGap(682, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(89, 89, 89)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2,
javax.swing.GroupLayout.PREFERRED_SIZE, 32,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(uname1,
javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel3,
javax.swing.GroupLayout.PREFERRED_SIZE, 32,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(ppass1,
javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addComponent(button1)
.addGap(20, 20, 20)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel4,
javax.swing.GroupLayout.PREFERRED_SIZE, 32,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(fname,
javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(26, 26, 26)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel5,
javax.swing.GroupLayout.PREFERRED_SIZE, 32,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(lname,
javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel7,
javax.swing.GroupLayout.PREFERRED_SIZE, 32,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(uname,
javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(38, 38, 38)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel6,
javax.swing.GroupLayout.PREFERRED_SIZE, 32,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(ppass,
javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(33, 33, 33)
.addComponent(button)
.addContainerGap(330, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void button1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
if(evt.getSource()==button1)
{
char[] temp_pwd1=ppass1.getPassword();
String pwd1;
pwd1=String.copyValueOf(temp_pwd1);
System.out.println("Username,Pwd:"+uname1.getText()+","+pwd1);
if(db.checkLogin(uname1.getText(),pwd1))
{
JOptionPane.showMessageDialog(null, "You have logged in
successfully","Success",
JOptionPane.INFORMATION_MESSAGE);
}
else
{
JOptionPane.showMessageDialog(null, "Login failed!","Failed!!",
JOptionPane.ERROR_MESSAGE);
}
}
}
private void buttonActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
/* if(evt.getSource()==button)
{
char[] temp_pwd=ppass.getPassword();
String pwd=null;
pwd=String.copyValueOf(temp_pwd);
System.out.println("fname,lname,uname,ppass:"+fname.getText()+","+lname.getText()+","+uname.getText()+","+pwd);
if(db.Registration(fname.getText(),
lname.getText(),uname.getText(),pwd))
{
JOptionPane.showMessageDialog(null, "Registratione
Successfully ","Success",
JOptionPane.INFORMATION_MESSAGE);
}
else
{
// int result = JOptionPane.showMessageDialog(null,
"Username exists","Failed!!",
// + JOptionPane.ERROR_MESSAGE);
int result = JOptionPane.showConfirmDialog(null, "Username
exists","Error", JOptionPane.OK_CANCEL_OPTION);
if (result == JOptionPane.OK_OPTION)
{
uname.setText("");
uname.requestFocus();
}
else if(result == JOptionPane.CANCEL_OPTION)
{
System.exit(0);
}
// JOptionPane.showMessageDialog(null, "Registration
Fail","Failed!!",
// + JOptionPane.ERROR_MESSAGE);
}
}*/
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting
code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the
default look and feel.
* For details see
http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info :
javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE,
null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE,
null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE,
null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE,
null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new NewJFrame().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton button;
private javax.swing.JButton button1;
private javax.swing.JTextField fname;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JLabel jLabel6;
private javax.swing.JLabel jLabel7;
private javax.swing.JTextField lname;
private javax.swing.JPasswordField ppass;
private javax.swing.JPasswordField ppass1;
private javax.swing.JTextField uname;
private javax.swing.JTextField uname1;
// End of variables declaration
}
and this is database program
/*
To change this template, choose Tools | Templates
and open the template in the editor.
*/
package loginpage;
import java.sql.; /* * * @author Parth */ public class regidatabase1 {
/**
* @param args the command line arguments
*/
Connection con;
PreparedStatement pst,pst1,pst2;
ResultSet rs;
regidatabase1()
{
try{
Class.forName("com.mysql.jdbc.Driver");
con=DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/ajax","root","parth");
pst=con.prepareStatement("select * from registrtion where uname=?
and pwd=password(?)");
pst1=con.prepareStatement("INSERT INTO registrtion"+
"(firstname,lastname,uname,pwd) VALUES"+ "(?,?,?,password(?))");
// pst1=con.prepareStatement("INSERT INTO registrtion"+
"(firstname,lastname,uname,pwd) VALUES"+ "(?,?,?,?)");
}
catch (Exception e)
{
System.out.println(e);
}
}
public Boolean checkLogin(String uname,String pwd)
{
try {
pst.setString(1, uname);
pst.setString(2, pwd);
rs=pst.executeQuery();
if(rs.next())
{
return true;
}
else
{
return false;
}
} catch (Exception e) {
System.out.println("error while validating"+e);
return false;
}
}
/*public Boolean Registration(String firstname,String lastname,String
uname,String pwd) { try {
pst1.setString(1, firstname);
pst1.setString(2, lastname);
pst1.setString(3, uname);
pst1.setString(4, pwd);
pst1.executeUpdate();
return true;
}
catch (Exception e) {
System.out.println("error while validating"+e);
return false;
}
}*/ }
plz help me this is gives me Runtime NullPointer exception.

No comments:

Post a Comment