Dates

Danica
Danica used Ask the Experts™
on
Here another easy one that iam sure u all should know

i have have a jtextfield, when the program load up it automatical puts in 2days date. now its seems that if i do a search that gets anthor date from the db n i trie it over ride it it gives me a null pointer error.. now i have tried every thing it display in an empty text box etc but no in the one with the date

Danica
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Hi,
i can't understand exactly what is the problam, can u post some code (especially the part that handle your jtextfield)

-gkern

Author

Commented:
here is some off the code i have taken out the stuff that isnt need


public Order(String CustomerID) {
       
JPanel p2= new JPanel();
        p2.setLayout(new FlowLayout(FlowLayout.RIGHT));
        JLabel lbDate= new JLabel("Date",JLabel.RIGHT);
        JTextField tfDate= new JTextField(8);
       DateFormat shortDate=DateFormat.getDateInstance(DateFormat.SHORT);
        //java.util.Date date=new java.util.Date();
        //SimpleDateFormat fmt=new SimpleDateFormat("dd/MM/yyyy");
        //String dtstv=fmt.format(date);
       today=new java.util.Date();
       tfDate.setText(shortDate.format(today));
        p2.add(lbDate);
        p2.add(tfDate);
       
        JPanel p8= new JPanel();
        p8.setLayout(new FlowLayout(FlowLayout.CENTER));
        lbTitle=new JLabel("Customer Orders");
        lbTitle.setFont(new Font("Arial",Font.BOLD,18));
        lbTitle.setForeground(Color.blue);
        p8.add(lbTitle);
       
        JPanel p3= new JPanel();
        p3.setLayout(new GridLayout(4,6));
        lbOrderNo=new JLabel("   Order Number");
        JLabel lb2=new JLabel("");
        JLabel lb3=new JLabel("");
        JLabel lb4=new JLabel("");
        JLabel lb5=new JLabel("");
        JLabel  lb6=new JLabel("");
        JLabel lb7=new JLabel("");
        lbCustNo=new JLabel("  Customer Number");
        tfCustNo=new JTextField(10);
        tfOrderNo=new JTextField(10);
        lbContactName=new JLabel("  Contact Name");
        lbDeliveryAddress=new JLabel("   Delivery Address");
        tfContactName=new JTextField(10);
        tfDeliveryAddress=new JTextField(10);
        lbProductName=new JLabel("  Product Name");
        lbItemCost=new JLabel("   Cost");
        chProduct=new Choice();
        product();
        //    tfProductName.setBorder(new javax.swing.border.EtchedBorder());
        tfItemCost=new JTextField(10);
        lbQty=new JLabel("  Qty");
        tfQty=new JTextField(10);
        tfCustNo.setText(CustID);
        btAddItem=new JButton("Add Item");
        p3.add(lbCustNo);
        p3.add(tfCustNo);
        p3.add(lbOrderNo);
        p3.add(tfOrderNo);
        p3.add(lbContactName);
        p3.add(tfContactName);
        p3.add(lbDeliveryAddress);
        p3.add(tfDeliveryAddress);
        p3.add(lbProductName);
        p3.add(chProduct);
       
        //        p3.add(lstProduct);
        p3.add(lbItemCost);
        p3.add(tfItemCost);
        p3.add(lbQty);
        p3.add(tfQty);
        p3.add(lb6);
        p3.add(lb6);
       
        JPanel p4= new JPanel();
        p4.setLayout(new FlowLayout(FlowLayout.LEFT));
        lbDiscount= new JLabel();
        lbDiscount.setText("Discount");
        p4.add(lbDiscount);
        cbg =new CheckboxGroup();
        jb1=new Checkbox("Yes",cbg,true);
        p4.add(jb1);
        jb2=new Checkbox("No",cbg,false);
        p4.add(jb2);
       
       
       
        p3.add(p4);
       
       
        JPanel p7=new JPanel();
        p7.setLayout(new FlowLayout(FlowLayout.CENTER));
        btAdd= new JButton("Add");
        btDelete= new JButton("Delete");
        btUpdate= new JButton("Update");
        btClear=new JButton("Clear");
        btSearch=new JButton("Search");
        p7.add(btAddItem);
        btAddItem.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                additem();
               
            }
        });
       
        p7.add(btAdd);
        btAdd.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                insert();
            }
        });
       
        p7.add(btDelete);
       
        btDelete.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                delete();
            }
        });
       
        p7.add(btUpdate);
        btUpdate.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
               update();
            }
        });
       
       
        p7.add(btSearch);
        btSearch.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                search();
            }
        });
       
        p7.add(btClear);
        btClear.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                clear();
            }
        });
       
       
        JPanel p6= new JPanel();
        p6.setLayout(new GridLayout(2,1));
        JLabel lb1=new JLabel();
        p6.add(p2);
        p6.add(p8);
   
       
        Container c=getContentPane();
        c.setLayout(new GridLayout(5,1,5,5));
        c.add(p6);
        c.add(p3);
        c.add(p4);
        c.add(p1);
        c.add(p7);
     
        chProduct.addItemListener(new ItemListener() {
            public void itemStateChanged(ItemEvent event) {
                price();
            }
           
        });
       
    }
   
    public void search() {
        try {
     
            sql ="select DeliveryAddress,CustContactName,ItemQuantityOrdered,ItemCost,ordervalue,OrderDate,PurchaseItem.DiscountAllowed,PurchaseOrder.CustNO from PurchaseOrder, Customer,PurchaseItem where PurchaseOrder.OrderNo=? and PurchaseItem.OrderNo=? and PurchaseOrder.Custno=Customer.CustNo";
            pstmt=conn.prepareStatement(sql);
            pstmt.setString(1,tfOrderNo.getText());
            pstmt.setString(2,tfOrderNo.getText());
            ResultSet result=pstmt.executeQuery();
            result.next();
            tfCustNo.setText(result.getString(8));
            tfDeliveryAddress.setText(result.getString(1));
            tfContactName.setText(result.getString(2));
            tfQty.setText(result.getString(3));
            tfItemCost.setText(result.getString(4));
            searchtotalcost=(result.getDouble(5));
       
            d=(result.getDate(6));
        //  SimpleDateFormat fmt=new SimpleDateFormat("dd/MM/yyyy");
        String g =d.toString();
       
       tfDate.setText(g);
     
           
          System.out.println(g);
 //          String t=(result.getString(7));
           
            if (t.equals("No")){
                jb2.setState(true);
               
            }
            else {
                jb1.setState(true);
            }
        }
        catch(SQLException se) {
            //tfContactName.setText(" ");
            JOptionPane.showMessageDialog(null,"SQLException: " + se.getMessage() + " --->Record Not Found.");
            tfCustNo.requestFocus();
        }
        catch(Exception ex) {
            JOptionPane.showMessageDialog(null,ex.toString());
        }
    }
   

Author

Commented:
here is some off the code i have taken out the stuff that isnt need


public Order(String CustomerID) {
       
JPanel p2= new JPanel();
        p2.setLayout(new FlowLayout(FlowLayout.RIGHT));
        JLabel lbDate= new JLabel("Date",JLabel.RIGHT);
        JTextField tfDate= new JTextField(8);
       DateFormat shortDate=DateFormat.getDateInstance(DateFormat.SHORT);
        //java.util.Date date=new java.util.Date();
        //SimpleDateFormat fmt=new SimpleDateFormat("dd/MM/yyyy");
        //String dtstv=fmt.format(date);
       today=new java.util.Date();
       tfDate.setText(shortDate.format(today));
        p2.add(lbDate);
        p2.add(tfDate);
       
        JPanel p8= new JPanel();
        p8.setLayout(new FlowLayout(FlowLayout.CENTER));
        lbTitle=new JLabel("Customer Orders");
        lbTitle.setFont(new Font("Arial",Font.BOLD,18));
        lbTitle.setForeground(Color.blue);
        p8.add(lbTitle);
       
        JPanel p3= new JPanel();
        p3.setLayout(new GridLayout(4,6));
        lbOrderNo=new JLabel("   Order Number");
        JLabel lb2=new JLabel("");
        JLabel lb3=new JLabel("");
        JLabel lb4=new JLabel("");
        JLabel lb5=new JLabel("");
        JLabel  lb6=new JLabel("");
        JLabel lb7=new JLabel("");
        lbCustNo=new JLabel("  Customer Number");
        tfCustNo=new JTextField(10);
        tfOrderNo=new JTextField(10);
        lbContactName=new JLabel("  Contact Name");
        lbDeliveryAddress=new JLabel("   Delivery Address");
        tfContactName=new JTextField(10);
        tfDeliveryAddress=new JTextField(10);
        lbProductName=new JLabel("  Product Name");
        lbItemCost=new JLabel("   Cost");
        chProduct=new Choice();
        product();
        //    tfProductName.setBorder(new javax.swing.border.EtchedBorder());
        tfItemCost=new JTextField(10);
        lbQty=new JLabel("  Qty");
        tfQty=new JTextField(10);
        tfCustNo.setText(CustID);
        btAddItem=new JButton("Add Item");
        p3.add(lbCustNo);
        p3.add(tfCustNo);
        p3.add(lbOrderNo);
        p3.add(tfOrderNo);
        p3.add(lbContactName);
        p3.add(tfContactName);
        p3.add(lbDeliveryAddress);
        p3.add(tfDeliveryAddress);
        p3.add(lbProductName);
        p3.add(chProduct);
       
        //        p3.add(lstProduct);
        p3.add(lbItemCost);
        p3.add(tfItemCost);
        p3.add(lbQty);
        p3.add(tfQty);
        p3.add(lb6);
        p3.add(lb6);
       
        JPanel p4= new JPanel();
        p4.setLayout(new FlowLayout(FlowLayout.LEFT));
        lbDiscount= new JLabel();
        lbDiscount.setText("Discount");
        p4.add(lbDiscount);
        cbg =new CheckboxGroup();
        jb1=new Checkbox("Yes",cbg,true);
        p4.add(jb1);
        jb2=new Checkbox("No",cbg,false);
        p4.add(jb2);
       
       
       
        p3.add(p4);
       
       
        JPanel p7=new JPanel();
        p7.setLayout(new FlowLayout(FlowLayout.CENTER));
        btAdd= new JButton("Add");
        btDelete= new JButton("Delete");
        btUpdate= new JButton("Update");
        btClear=new JButton("Clear");
        btSearch=new JButton("Search");
        p7.add(btAddItem);
        btAddItem.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                additem();
               
            }
        });
       
        p7.add(btAdd);
        btAdd.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                insert();
            }
        });
       
        p7.add(btDelete);
       
        btDelete.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                delete();
            }
        });
       
        p7.add(btUpdate);
        btUpdate.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
               update();
            }
        });
       
       
        p7.add(btSearch);
        btSearch.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                search();
            }
        });
       
        p7.add(btClear);
        btClear.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                clear();
            }
        });
       
       
        JPanel p6= new JPanel();
        p6.setLayout(new GridLayout(2,1));
        JLabel lb1=new JLabel();
        p6.add(p2);
        p6.add(p8);
   
       
        Container c=getContentPane();
        c.setLayout(new GridLayout(5,1,5,5));
        c.add(p6);
        c.add(p3);
        c.add(p4);
        c.add(p1);
        c.add(p7);
     
        chProduct.addItemListener(new ItemListener() {
            public void itemStateChanged(ItemEvent event) {
                price();
            }
           
        });
       
    }
   
    public void search() {
        try {
     
            sql ="select DeliveryAddress,CustContactName,ItemQuantityOrdered,ItemCost,ordervalue,OrderDate,PurchaseItem.DiscountAllowed,PurchaseOrder.CustNO from PurchaseOrder, Customer,PurchaseItem where PurchaseOrder.OrderNo=? and PurchaseItem.OrderNo=? and PurchaseOrder.Custno=Customer.CustNo";
            pstmt=conn.prepareStatement(sql);
            pstmt.setString(1,tfOrderNo.getText());
            pstmt.setString(2,tfOrderNo.getText());
            ResultSet result=pstmt.executeQuery();
            result.next();
            tfCustNo.setText(result.getString(8));
            tfDeliveryAddress.setText(result.getString(1));
            tfContactName.setText(result.getString(2));
            tfQty.setText(result.getString(3));
            tfItemCost.setText(result.getString(4));
            searchtotalcost=(result.getDouble(5));
       
            d=(result.getDate(6));
        //  SimpleDateFormat fmt=new SimpleDateFormat("dd/MM/yyyy");
        String g =d.toString();
       
       tfDate.setText(g);
     
           
          System.out.println(g);
 //          String t=(result.getString(7));
           
            if (t.equals("No")){
                jb2.setState(true);
               
            }
            else {
                jb1.setState(true);
            }
        }
        catch(SQLException se) {
            //tfContactName.setText(" ");
            JOptionPane.showMessageDialog(null,"SQLException: " + se.getMessage() + " --->Record Not Found.");
            tfCustNo.requestFocus();
        }
        catch(Exception ex) {
            JOptionPane.showMessageDialog(null,ex.toString());
        }
    }
   
Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

good,
now please post the exception stack track (ie your complete error message)

-gkern
Commented:
you are getting a null pointer expection because of the possible reason..

you may have declared somethng like this..

public class Order{

...

JTextField tfDate;

..

public Order(String CusomterID){
..
JTextField tfDate = new JTextField(8);

..

by doing so, what is happeneing is you are initializing another tfDate whose scope is only within the constructor.. to access it from outside, you should not be reinitializing it again in the constructor. ie in the constructor, say
tfDate = new JTextField(8);

if this is still confusing, see what u have done for tfContactName, tfDeliveryAddress in the constructor

hth
vemul
in your code:
...
d=(result.getDate(6));
String g =d.toString();
tfDate.setText(g); NullPointerException
...

instead of last line, try:

tfDate.setText( (g==null) ? "" : g );

-gkern
oops, i haven't seen vemul answer, i supose this is the answer to your NullPointerException.
Danica:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.

Commented:
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

- Points for vemul

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Venabili
EE Cleanup Volunteer

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial