Results 1 to 4 of 4

Thread: projt

  1. #1
    smjsarsji is offline Member
    Join Date
    Oct 2010
    Posts
    2
    Rep Power
    0

    Default projt

    Please help me soon!

    I am getting the following error in the program I have provided below. "Exception in thread "main" java.lang.NullPointerException." Can anyone help me?

    Exception in thread "main" java.lang.NullPointerException
    at ProductInfo.<init>(ProductInfo.java:37)
    at ProductInfo.main(ProductInfo.java:172)


    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.sql.*;
    public class ProductInfo extends JFrame implements ActionListener{
    JLabel heading,id,desc,rate,quantity,unit_of_msr,empty_la bel,error;
    JTextField idField,descField,rateField,quantityField,unit_of_ msrField;
    JButton insert,update,delete,clear,exit;
    GridBagLayout gbl;
    GridBagConstraints gbc;
    JComboBox idCombo;
    Connection con;
    PreparedStatement stat;
    Statement stmt;
    ResultSet rs;
    Font f;
    JPanel jpl;
    public ProductInfo()
    {
    try
    {
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    con=DriverManager.getConnection("jdbc:odbc:MyDataS ource","scott"," ");
    stmt=con.createStatement();
    rs=stmt.executeQuery("SELECT p_id FROM product");
    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:odbc:MyDataS ource","scott"," ");
    stat=con.prepareStatement("DELETE from product WHERE p_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:odbc:MyDataS ource","scott"," ");
    stmt=con.createStatement();
    rs=stmt.executeQuery("SELECT p_id FROM product");
    idCombo.removeAllItems();
    while(rs.next())
    idCombo.addItem(Integer.toString(rs.getInt(1)));
    con.close();
    idCombo.addActionListener(this);
    idField.setText("");
    descField.setText("");
    rateField.setText("");
    quantityField.setText("");
    unit_of_msrField.setText("");
    error.setText("row deleted");
    }
    catch(Exception e)
    {
    System.out.println("error" +e);
    error.setText("row cannot be deleted");
    }
    }
    if(ae.getActionCommand()=="Insert")
    {
    try
    {
    con=DriverManager.getConnection("jdbc:odbc:MyDataS ource","scott"," ");
    stat=con.prepareStatement("INSERT INTO product VALUES(?,?,?,?,?)");
    String id=idField.getText();
    String description=descField.getText();
    String rate=rateField.getText();
    String quantity=quantityField.getText();
    String unit_of_msr=unit_of_msrField.getText();
    stat.setInt(1,Integer.parseInt(id));
    stat.setString(2,description);
    stat.setDouble(3,Double.parseDouble(rate));
    stat.setInt(4,Integer.parseInt(quantity));
    stat.setString(5,unit_of_msr);
    stat.executeUpdate();
    con.close();
    idCombo.removeActionListener(this);
    con=DriverManager.getConnection("jdbc:odbc:MyDataS ource","scott"," ");
    stmt=con.createStatement();
    rs=stmt.executeQuery("SELECT p_id FROM product");
    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("error" +e);
    error.setText("row cannot be inserted");
    }
    }
    if(ae.getSource() == idCombo)
    {
    try
    {
    con=DriverManager.getConnection("jdbc:odbc:MyDataS ource","scott"," ");
    String selected_id=idCombo.getSelectedItem().toString();
    int id=Integer.parseInt(selected_id);
    stmt=con.createStatement();
    rs=stmt.executeQuery("SELECT p_desc,p_rate,p_qty,unit_of_msr FROM product WHERE p_id=" + id);
    rs.next();
    idField.setText(selected_id);
    descField.setText(rs.getString(1));
    rateField.setText(Double.toString(rs.getDouble(2)) );
    quantityField.setText(Integer.toString(rs.getInt(3 )));
    unit_of_msrField.setText(rs.getString(4));
    con.close();
    }
    catch(Exception e)
    {
    System.out.println("error" +e);
    }
    }
    if(ae.getActionCommand()=="Update")
    {
    try
    {
    con=DriverManager.getConnection("jdbc:odbc:MyDataS ource","scott"," ");
    stat=con.prepareStatement("UPDATE product SET p_desc=?,p_rate=?,p_qty=?,unit_of_msr=? where p_id=?" );
    String description=descField.getText();
    String rate=rateField.getText();
    String quantity=quantityField.getText();
    String unit_of_msr=unit_of_msrField.getText();
    String id_selected=idField.getText();
    int id=Integer.parseInt(id_selected);

    stat.setString(1,description);
    stat.setDouble(2,Double.parseDouble(rate));
    stat.setInt(3,Integer.parseInt(quantity));
    stat.setString(4,unit_of_msr);
    stat.setInt(5,id);
    stat.executeUpdate();
    con.close();
    error.setText("row updated");
    }
    catch(Exception e)
    {
    System.out.println("error" +e);
    error.setText("row cannot be updated");
    }
    }
    if(ae.getActionCommand()=="Clear")
    {
    idField.setText("");
    descField.setText("");
    rateField.setText("");
    quantityField.setText("");
    unit_of_msrField.setText("");
    }
    }
    public static void main(String args[])
    {
    ProductInfo p=new ProductInfo();
    p.show();
    }
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,266
    Rep Power
    25

    Default

    java.lang.NullPointerException
    at ProductInfo.<init>(ProductInfo.java:37)
    What code is at line 37? What variable on that line is null?

  3. #3
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    5

    Default

    idCombo.addItem
    idCombo.addActionListener(this); --> idCombo is null! create an instance first!

    btw:
    strings are objects, use equals instead of == ! (even if it works here because of the pool...)
    show() is deprecated, use setVisible(true) !
    Last edited by eRaaaa; 10-05-2010 at 03:56 PM.

  4. #4
    smjsarsji is offline Member
    Join Date
    Oct 2010
    Posts
    2
    Rep Power
    0

    Default prjt

    sorry 4 forgetting the line number

    i provide the code along with line number which was shown as error


    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.sql.*;
    public class ProductInfo extends JFrame implements ActionListener{
    JLabel heading,id,desc,rate,quantity,unit_of_msr,empty_la bel,error;
    JTextField idField,descField,rateField,quantityField,unit_of_ msrField;
    JButton insert,update,delete,clear,exit;
    GridBagLayout gbl;
    GridBagConstraints gbc;
    JComboBox idCombo;
    Connection con;
    PreparedStatement stat;
    Statement stmt;
    ResultSet rs;
    Font f;
    JPanel jpl;
    public ProductInfo()
    {
    try
    {
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    con=DriverManager.getConnection("jdbc:odbc:MyDataS ource","scott"," ");
    stmt=con.createStatement();
    rs=stmt.executeQuery("SELECT p_id FROM product");
    while(rs.next())
    {
    idCombo.addItem(Integer.toString(rs.getInt(1)));
    }
    con.close();
    }
    catch(Exception e)
    {
    System.out.println("error: "+e);
    }
    line 37 idCombo.addActionListener(this);
    }
    public void actionPerformed(ActionEvent ae)
    {
    if(ae.getActionCommand()=="Exit")
    System.exit(0);
    if(ae.getActionCommand()=="Delete")
    {
    try
    {
    con=DriverManager.getConnection("jdbc:odbc:MyDataS ource","scott"," ");
    stat=con.prepareStatement("DELETE from product WHERE p_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:odbc:MyDataS ource","scott"," ");
    stmt=con.createStatement();
    rs=stmt.executeQuery("SELECT p_id FROM product");
    idCombo.removeAllItems();
    while(rs.next())
    idCombo.addItem(Integer.toString(rs.getInt(1)));
    con.close();
    idCombo.addActionListener(this);
    idField.setText("");
    descField.setText("");
    rateField.setText("");
    quantityField.setText("");
    unit_of_msrField.setText("");
    error.setText("row deleted");
    }
    catch(Exception e)
    {
    System.out.println("error" +e);
    error.setText("row cannot be deleted");
    }
    }
    if(ae.getActionCommand()=="Insert")
    {
    try
    {
    con=DriverManager.getConnection("jdbc:odbc:MyDataS ource","scott"," ");
    stat=con.prepareStatement("INSERT INTO product VALUES(?,?,?,?,?)");
    String id=idField.getText();
    String description=descField.getText();
    String rate=rateField.getText();
    String quantity=quantityField.getText();
    String unit_of_msr=unit_of_msrField.getText();
    stat.setInt(1,Integer.parseInt(id));
    stat.setString(2,description);
    stat.setDouble(3,Double.parseDouble(rate));
    stat.setInt(4,Integer.parseInt(quantity));
    stat.setString(5,unit_of_msr);
    stat.executeUpdate();
    con.close();
    idCombo.removeActionListener(this);
    con=DriverManager.getConnection("jdbc:odbc:MyDataS ource","scott"," ");
    stmt=con.createStatement();
    rs=stmt.executeQuery("SELECT p_id FROM product");
    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("error" +e);
    error.setText("row cannot be inserted");
    }
    }
    if(ae.getSource() == idCombo)
    {
    try
    {
    con=DriverManager.getConnection("jdbc:odbc:MyDataS ource","scott"," ");
    String selected_id=idCombo.getSelectedItem().toString();
    int id=Integer.parseInt(selected_id);
    stmt=con.createStatement();
    rs=stmt.executeQuery("SELECT p_desc,p_rate,p_qty,unit_of_msr FROM product WHERE p_id=" + id);
    rs.next();
    idField.setText(selected_id);
    descField.setText(rs.getString(1));
    rateField.setText(Double.toString(rs.getDouble(2)) );
    quantityField.setText(Integer.toString(rs.getInt(3 )));
    unit_of_msrField.setText(rs.getString(4));
    con.close();
    }
    catch(Exception e)
    {
    System.out.println("error" +e);
    }
    }
    if(ae.getActionCommand()=="Update")
    {
    try
    {
    con=DriverManager.getConnection("jdbc:odbc:MyDataS ource","scott"," ");
    stat=con.prepareStatement("UPDATE product SET p_desc=?,p_rate=?,p_qty=?,unit_of_msr=? where p_id=?" );
    String description=descField.getText();
    String rate=rateField.getText();
    String quantity=quantityField.getText();
    String unit_of_msr=unit_of_msrField.getText();
    String id_selected=idField.getText();
    int id=Integer.parseInt(id_selected);

    stat.setString(1,description);
    stat.setDouble(2,Double.parseDouble(rate));
    stat.setInt(3,Integer.parseInt(quantity));
    stat.setString(4,unit_of_msr);
    stat.setInt(5,id);
    stat.executeUpdate();
    con.close();
    error.setText("row updated");
    }
    catch(Exception e)
    {
    System.out.println("error" +e);
    error.setText("row cannot be updated");
    }
    }
    if(ae.getActionCommand()=="Clear")
    {
    idField.setText("");
    descField.setText("");
    rateField.setText("");
    quantityField.setText("");
    unit_of_msrField.setText("");
    }
    }
    public static void main(String args[])
    {
    line 171 ProductInfo p=new ProductInfo();
    p.show();
    }
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •