Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: why is this error?

  1. #1
    Junior Member
    Join Date
    Sep 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default why is this error?

    This is my code to connect to mysql

    but i got this error when executing the code...help me on this

    run:
    Exception in thread "main" java.lang.NullPointerException
    at StaffRecord.<init>(StaffRecord.java:48)
    at StaffRecord.main(StaffRecord.java:188)
    Java Result: 1
    BUILD SUCCESSFUL (total time: 1 second
    My code

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.sql.*;
    public class StaffRecord extends JFrame implements ActionListener {

    JLabel heading,id,Name,Dept,Year,Handlingsubject,empty_la bel,error;
    JTextField idField, NameField, DeptField,YearField, HandlingsubjectField;
    JButton insert,update,delete,clear,exit;
    GridBagLayout gb1;
    GridBagConstraints gbc;
    JComboBox idCombo;
    Connection con;
    PreparedStatement stat;
    Statement stmt;
    ResultSet rs;
    Font f;
    JPanel jp1;
    public StaffRecord() throws ClassNotFoundException
    {
    try
    {
    Class.forName("com.mysql.jdbc.Driver");
    con=DriverManager.getConnection("jdbc:mysql://localhost:3306/mystaffrecord","root","admin");
    stmt=con.createStatement();
    rs=stmt.executeQuery("SELECT s_id FROM Records");
    while (rs.next())
    {
    idCombo.addItem(Integer.toString(rs.getInt(1)));
    }
    con.close();
    }
    catch(Exception e)
    {
    System.out.println("Error : " +e);
    }

    idCombo.addActionListener(this);
    }
    public void actionPerformed(ActionEvent ae)
    {
    if(ae.getActionCommand()=="Exit");
    System.exit(0);
    if(ae.getActionCommand()=="Delete");
    {
    try
    {
    con = DriverManager.getConnection("jdbc:mysql://localhost:3306/mystaffrecord","root","admin");
    stat=con.prepareStatement("Delete from Records WHERE s_id =?");
    String selected_id =idCombo.getSelectedItem().toString();
    int id = Integer.parseInt(selected_id);
    stat.setInt(1, id);
    stat.executeUpdate();
    con.close();
    idCombo.removeActionListener(this);;
    con=DriverManager.getConnection("jdbc:mysql://localhost:3306/mystaffrecord","root","admin");
    stmt=con.createStatement();
    rs=stmt.executeQuery("SELECT s_id FROM Records");
    idCombo.removeAllItems();
    while(rs.next())
    idCombo.addItem(Integer.toString(rs.getInt(1)));
    con.close();
    idCombo.addActionListener(this);
    idField.setText("");
    NameField.setText("");
    DeptField.setText("");
    YearField.setText("");
    HandlingsubjectField.setText("");
    error.setText("Row Deleted");
    }
    catch(Exception e)
    {
    System.out.println("Error"+e);
    error.setText("Deletion Failed --Retry");
    }
    }

    if(ae.getActionCommand()=="Insert")
    {
    try
    {
    con=DriverManager.getConnection("jdbc:mysql://localhost:3306/mystaffrecord","root","admin");
    stat = con.prepareStatement("INSERT INTO Records VALUES(?,?,?,?,?");
    String id=idField.getText();
    String Name=NameField.getText();
    String Dept=DeptField.getText();
    String Year=YearField.getText();
    String Handlingsubject=HandlingsubjectField.getText();
    stat.setInt(1,Integer.parseInt(id));
    stat.setString(2,Name);
    stat.setString(3,Dept);
    stat.setInt(4,Integer.parseInt(Year));
    stat.setString(5, Handlingsubject);
    stat.executeUpdate();
    con.close();
    idCombo.removeActionListener(this);
    con=DriverManager.getConnection("jdbc:mysql://localhost:3306/mystaffrecord","root","admin");
    stmt =con.createStatement();
    rs=stmt.executeQuery("SELECT s_id FROM Records");
    idCombo.removeAllItems();
    while(rs.next())
    idCombo.addItem(Integer.toString(rs.getInt(1)));
    con.close();
    idCombo.addActionListener(this);
    error.setText("Row Inserted");
    }
    catch(Exception e)
    {
    System.out.println("Error1 :"+e);
    error.setText("Insertion Failed --Retry");
    }
    }
    if(ae.getSource()==idCombo)
    {
    try
    {
    con=DriverManager.getConnection("jdbc:mysql://localhost:3306/mystaffrecord","root","admin");
    String selected_id=idCombo.getSelectedItem().toString();
    int id=Integer.parseInt(selected_id);
    stmt=con.createStatement();
    rs=stmt.executeQuery("SELECT s_Name,s_Dept,s_Year,s_Handlingsubject");
    rs.next();
    idField.setText(selected_id);
    NameField.setText(rs.getString(1));
    DeptField.setText(rs.getString(2));
    YearField.setText(Integer.toString(rs.getInt(3)));
    HandlingsubjectField.setText(rs.getString(4));
    con.close();
    }
    catch(Exception e)
    {
    System.out.println("Error :"+e);
    }
    }
    if(ae.getActionCommand()=="Update");
    {
    try
    {
    con=DriverManager.getConnection("jdbc:mysql://localhost:3306/mystaffrecord","root","admin");
    stat=con.prepareStatement("UPDATE Records SET s_Name =?,s_Dept=?,s_Year=?,s_Handlingsubject=? where s_id=?");
    String id=idField.getText();
    String Name=NameField.getText();
    String Dept=DeptField.getText();
    String Year=YearField.getText();
    String Handlingsubject=HandlingsubjectField.getText();
    stat.setInt(1,Integer.parseInt(id));
    stat.setString(2,Name);
    stat.setString(3,Dept);
    stat.setInt(4,Integer.parseInt(Year));
    stat.setString(5, Handlingsubject);
    stat.executeUpdate();
    con.close();
    error.setText("Row Updated");
    }
    catch(Exception e)
    {
    System.out.println("Error :"+e);
    error.setText("Row updation Failure");
    }
    }
    if(ae.getActionCommand()=="Clear")
    {
    idField.setText("");
    NameField.setText("");
    DeptField.setText("");
    YearField.setText("");
    HandlingsubjectField.setText("");

    }
    }
    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) throws ClassNotFoundException

    {

    StaffRecord s= new StaffRecord();

    s.setVisible(true);

    }
    }
    Last edited by vjemman; September 16th, 2011 at 11:19 PM.


  2. #2
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: why is this error?

    Exception in thread "main" java.lang.NullPointerException
    at StaffRecord.<init>(StaffRecord.java:48)
    Use code tags for your code. Look at line 48 in your editor and try to work out which reference is null. Why is it null? If you can't see why the reference is null, post back and tell us which line is line 48.