Click here to Skip to main content
15,887,175 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
Here's my code......

package MyProject;

import java.awt.HeadlessException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import java.lang.*;

/**
*
* @author hp
*/
public class User1 extends javax.swing.JFrame {

Connection con = null;
PreparedStatement ps;
ResultSet rs;
/**
* Creates new form User1
*/
public User1() {
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() {

label1 = new javax.swing.JLabel();
Back1 = new javax.swing.JButton();
label2 = new javax.swing.JLabel();
username = new javax.swing.JTextField();
password = new javax.swing.JTextField();
Submit1 = new javax.swing.JButton();
label3 = new javax.swing.JLabel();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

label1.setText("Username:");

Back1.setText("Back");
Back1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
Back1ActionPerformed(evt);
}
});

label2.setText("Password:");

username.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
usernameActionPerformed(evt);
}
});

password.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
passwordActionPerformed(evt);
}
});

Submit1.setText("Submit");
Submit1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
Submit1ActionPerformed(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(15, 15, 15)
.addComponent(Back1))
.addGroup(layout.createSequentialGroup()
.addGap(51, 51, 51)
.addComponent(label1)
.addGap(103, 103, 103)
.addComponent(username, javax.swing.GroupLayout.PREFERRED_SIZE, 121, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addGap(51, 51, 51)
.addComponent(label2)
.addGap(107, 107, 107)
.addComponent(password, javax.swing.GroupLayout.PREFERRED_SIZE, 121, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addGap(145, 145, 145)
.addComponent(Submit1))
.addGroup(layout.createSequentialGroup()
.addGap(121, 121, 121)
.addComponent(label3, javax.swing.GroupLayout.PREFERRED_SIZE, 146, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap(161, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(16, 16, 16)
.addComponent(Back1)
.addGap(34, 34, 34)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(3, 3, 3)
.addComponent(label1))
.addComponent(username, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(47, 47, 47)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(3, 3, 3)
.addComponent(label2))
.addComponent(password, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(31, 31, 31)
.addComponent(Submit1)
.addGap(63, 63, 63)
.addComponent(label3, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(184, Short.MAX_VALUE))
);

pack();
}//

private void Back1ActionPerformed(java.awt.event.ActionEvent evt) {
this.dispose();
}

private void usernameActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}

private void passwordActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}

private void Submit1ActionPerformed(java.awt.event.ActionEvent evt) {




con=MysqlConnection.ConnectionDB();

String Query="select * from users where Username=? AND Password=?";

try{
ps = con.prepareStatement(Query);

ps.setString(1,username.getText());
ps.setString(2,password.getText());

rs = ps.executeQuery(Query);
if(rs.next())
{

JOptionPane.showMessageDialog(null,"invalid","invalid",JOptionPane.ERROR_MESSAGE);

}




else
{


this.dispose();
}

}



catch (SQLException | HeadlessException ex) {
}







}

/**
* @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 | InstantiationException | IllegalAccessException | javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(User1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//

//

/* Create and display the form */
java.awt.EventQueue.invokeLater(() -> {
new User1().setVisible(true);
});
}

// Variables declaration - do not modify
private javax.swing.JButton Back1;
private javax.swing.JButton Submit1;
private javax.swing.JLabel label1;
private javax.swing.JLabel label2;
private javax.swing.JLabel label3;
private javax.swing.JTextField password;
private javax.swing.JTextField username;
// End of variables declaration
}

What I have tried:

here'a MysqlConnection file

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package MyProject;

import java.awt.HeadlessException;

import java.sql.*;
import javax.swing.*;


/**
*
* @author hp
*/
public class MysqlConnection {
Connection con=null;
public static Connection ConnectionDB(){
try{
System.out.println("1");
Class.forName("com.jdbc.mysql.Driver");
System.out.println("1");
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/airlines?autoReconnect=true&useSSL=false", "root","riyard");
JOptionPane.showMessageDialog(null,"Connected to Database");
return con;

}
catch(ClassNotFoundException | SQLException | HeadlessException e){
JOptionPane.showMessageDialog(null,e);
return null;

}


}



}
donno how to fix this.. Please help me...
Posted
Updated 16-Sep-18 21:17pm

1 solution

This is one of the most common problems we get asked, and it's also the one we are least equipped to answer, but you are most equipped to answer yourself.

Let me just explain what the error means: You have tried to use a variable, property, or a method return value but it contains null - which means that there is no instance of a class in the variable.
It's a bit like a pocket: you have a pocket in your shirt, which you use to hold a pen. If you reach into the pocket and find there isn't a pen there, you can't sign your name on a piece of paper - and you will get very funny looks if you try! The empty pocket is giving you a null value (no pen here!) so you can't do anything that you would normally do once you retrieved your pen. Why is it empty? That's the question - it may be that you forgot to pick up your pen when you left the house this morning, or possibly you left the pen in the pocket of yesterdays shirt when you took it off last night.

We can't tell, because we weren't there, and even more importantly, we can't even see your shirt, much less what is in the pocket!

Back to computers, and you have done the same thing, somehow - and we can't see your code, much less run it and find out what contains null when it shouldn't.
But you can - and Visual Studio will help you here. Run your program in the debugger and when it fails, VS will show you the line it found the problem on. You can then start looking at the various parts of it to see what value is null and start looking back through your code to find out why. So put a breakpoint at the beginning of the method containing the error line, and run your program from the start again. This time, VS will stop before the error, and let you examine what is going on by stepping through the code looking at your values.

But we can't do that - we don't have your code, we don't know how to use it if we did have it, we don't have your data. So try it - and see how much information you can find out!
 
Share this answer
 
Comments
Member 13986703 17-Sep-18 13:23pm    
I have sent the code...
OriginalGriff 17-Sep-18 13:57pm    
And what have you done with the debugger to find out where the problem is?
Member 13986703 18-Sep-18 1:27am    
Error solved... You were right I was trying to retrieve the data from a variable which hold the null value... Thank You.
OriginalGriff 18-Sep-18 1:58am    
You're welcome!

Remember the debugger - it is truly your bestest buddy when you are developing code :D
Christian Byabushi 20-Aug-21 8:50am    
Thanks for your your help attached to examples

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900