0

What is causing the null exception here? I am simply unable to see it.. please second set of eyes would help this beginner java student..This application basically captures 5 pieces of info about a game team and insert into mySQL database.

The error is as below: Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at InsertTeamInfo.insertRecord(InsertTeamInfo.java:130) at InsertTeamInfo.access$100(InsertTeamInfo.java:11)

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

public class InsertTeamInfo extends JFrame{
  private String sql;
  private Connection con;
  private Statement stat;
  private JButton btnSave;

  private JTextField txtTeam;  
  private JTextField txtCity;
  private JTextField txtYear;
  private JTextField txtLoserTeam;
  private JTextField txtLoserCity;

  private JLabel lblTeam;
  private JLabel lblCity;
  private JLabel lblYear;
  private JLabel lblLoserTeam;
  private JLabel lblLoserCity;    

  public InsertTeamInfo(){
    super("Favorite Team");

    btnSave = new JButton("Save");

    txtTeam = new JTextField("");
    txtCity = new JTextField("");
    txtYear = new JTextField("");
    txtLoserTeam = new JTextField("");
    txtLoserCity = new JTextField("");

    lblTeam = new JLabel("Team");
    lblCity = new JLabel("City");
    lblYear = new JLabel("Year Played");
    lblLoserTeam = new JLabel("Loser Team");
    lblLoserCity = new JLabel("Loser City");

    txtYear.setEditable(true);
    txtLoserTeam.setEditable(true);
    txtLoserCity.setEditable(true);
    txtTeam.setEditable(true);
    txtCity.setEditable(true);  

  }

  public void launchJFrame(){
    //width - height
    setSize(500, 300);
    getContentPane().setLayout(null);
    getContentPane().setBackground(Color.white);

    setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
    getContentPane().add(btnSave);

    getContentPane().add(txtTeam);
    getContentPane().add(txtCity);
    getContentPane().add(txtYear);
    getContentPane().add(txtLoserTeam);
    getContentPane().add(txtLoserCity);

    getContentPane().add(lblTeam);
    getContentPane().add(lblCity);    
    getContentPane().add(lblYear);
    getContentPane().add(lblLoserTeam);
    getContentPane().add(lblLoserCity);

    lblTeam.setBounds(new Rectangle(65,20,100,30));
    lblCity.setBounds(new Rectangle(65,55,100,30));
    lblYear.setBounds(new Rectangle(65, 85, 100, 30));
    lblLoserTeam.setBounds(new Rectangle(65, 115, 100, 30));
    lblLoserCity.setBounds(new Rectangle(65, 145, 100, 30));

    txtTeam.setBounds(new Rectangle(210, 20, 150, 30));
    txtCity.setBounds(new Rectangle(210, 54, 150, 30));
    txtYear.setBounds(new Rectangle(210, 86, 150, 30));
    txtLoserTeam.setBounds(new Rectangle(210, 119, 150, 30));
    txtLoserCity.setBounds(new Rectangle(210, 150, 150, 30));

    btnSave.setBounds(new Rectangle(65, 190, 90, 30));
    setVisible(true);

    addWindowListener(new java.awt.event.WindowAdapter() {
      public void windowClosing(java.awt.event.WindowEvent evt) {
        shutDown();
      }
    });

    btnSave.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){ //Handle Save button click event
            insertRecord();
          }
     });
 }

 private void insertRecord(){
    try {
        DriverManager.registerDriver(new com.mysql.jdbc.Driver());
        con = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/week5", "root","Saras231");
    }
    catch(Exception ex){
      JOptionPane.showMessageDialog( null, "Error connection to database.");         
      System.exit(0);
    }

    try {
       sql = "INSERT INTO Wins_S001 (Team, City, Year_T, LoserTeam, LoserCity) VALUES (?, ?, ?, ?, ?)";
        PreparedStatement preparedStatement = con.prepareStatement(sql);
        preparedStatement.setString(1, txtTeam.getText());
        preparedStatement.setString(2, txtCity.getText());
        preparedStatement.setString(3, txtYear.getText());
        preparedStatement.setString(4, txtLoserTeam.getText());
        preparedStatement.setString(5, txtLoserCity.getText());
        preparedStatement.executeUpdate(); 
       JOptionPane.showMessageDialog( null, "Data Inserted Successfully");          
    }
    catch(SQLException ex){
      ex.printStackTrace();
      JOptionPane.showMessageDialog( null, "Data Insert failed.");          
    }

    try{
      stat.close();
      con.close();
    }
    catch(SQLException ex){
      JOptionPane.showMessageDialog( null, "All active connection closed to the database.");          
    }

 }

  private void shutDown(){
    int returnVal = JOptionPane.showConfirmDialog(this, "Are you sure you want to quit?");

    if(returnVal == JOptionPane.YES_OPTION){

      System.exit(0);
    }
  }

  public static void main(String[] args){
    InsertTeamInfo gui = new InsertTeamInfo();
    gui.launchJFrame();
  }
}
1
  • 6
    Post the stack trace, including where it indicates which line of code is causing problems. Then let us know which line of your code corresponds with that line number. Commented Jul 7, 2014 at 20:50

1 Answer 1

1

Remove the stat instance variable. Remove the con instance variable. Remove the sql instance variable. They should all be local variables (and that's probably the cause of your exception: you're closing stat instead of closing preparedStatement. The line number in the stack trace should confirm it).

Also, make sure these are closed in a finally block, to be absolutely sure that they're closed. And beware: if closing the prepared statement throws an exception, the connection won't be closed.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you so much! I followed all of your suggestions and that took care of it.
Next time, read the exception stack trace carefully: the line number tells you where the exception is thrown, which makes finding the problem a tricial task. Also, when posting the stack trace, tell us what line number it refers to.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.