Showing NullPointerException when tried to retrieve data to JTable fro

Showing NullPointerException when tried to retrieve data to JTable fro

Page 1 of 1

8 Replies - 1385 Views - Last Post: 17 July 2009 - 09:36 PM Rate Topic: -----

#1 tinoymalayil  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 23-June 08

Showing NullPointerException when tried to retrieve data to JTable fro

Posted 15 July 2009 - 10:12 PM

//This is my Class with main method

import java.sql.Connection;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.sql.ResultSet;
import java.util.Vector;
import java.util.Date;
public class Examp extends javax.swing.JFrame {


DBCon DB=new DBCon();
ValidationCheck vc=new ValidationCheck();
Vector tmp=new Vector();
Vector data=new Vector();
Vector heading=new Vector();
Connection con=null;
Statement st=null;
String Branch;
String TransactionType;
String PC;
String Cust;
public Examp() throws Exception {
heading.addElement("Branch");
heading.addElement("Transaction Type");
heading.addElement("Principal Company");
heading.addElement("Customer");
heading.addElement("Date");
initComponents();
SimpleDateFormat dateformat=new SimpleDateFormat("dd/MM/yyyy");
Date date=new Date();
DateText.setText(dateformat.format(date));
clearData();
}
public void insertIntoDB() throws Exception
{
con=DB.dbConnection();
st=con.createStatement();
st.executeUpdate("Insert into Transactions values('"+BranchText.getText()+ "','"+TransactionTypeCombo.getSelectedItem()+"','"+PCCombo.getSelectedItem()+"','"+CustCombo.getSelectedItem()+"','"+DateText.getText()+"')");
}

public boolean validateData()
{
Branch=BranchText.getText();
TransactionType=(String)TransactionTypeCombo.getSelectedItem();
PC=(String)PCCombo.getSelectedItem();
Cust=(String)CustCombo.getSelectedItem();
if(vc.checkIfNull(Branch)==false)
{
ErrorLabel.setText("Please Enter Branch");
return false;
}
else if(vc.checkFullName(Branch)==false)
{
ErrorLabel.setText("Branch Name is Incorrect");
return false;
}
else if(vc.checkComboNull(TransactionType)==false)
{
ErrorLabel.setText("Please Select Transaction Type");
return false;
}
else if(vc.checkComboNull(PC)==false)
{
ErrorLabel.setText("Please Select Principal Company");
return false;
}
else if(vc.checkComboNull(Cust)==false)
{
ErrorLabel.setText("Please Select Customer");
return false;
}
else
{
ErrorLabel.setText("");
SuccessLabel.setText("Entered Successfully");
SuccessLabel.setVisible(true);
return true;
}
}

public void clearData()
{
BranchText.setText("");
TransactionTypeCombo.setSelectedItem("Select");
PCCombo.setSelectedItem("Select");
CustCombo.setSelectedItem("Select");
ErrorLabel.setText("");
SuccessLabel.setText("");
}

@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {

TransactionPanel = new javax.swing.JPanel();
BranchLabel = new javax.swing.JLabel();
BranchText = new javax.swing.JTextField();
TransactionTypeLabel = new javax.swing.JLabel();
PCLabel = new javax.swing.JLabel();
PCCombo = new javax.swing.JComboBox();
CustLabel = new javax.swing.JLabel();
TransactionTypeCombo = new javax.swing.JComboBox();
CustCombo = new javax.swing.JComboBox();
DateLabel = new javax.swing.JLabel();
DateText = new javax.swing.JTextField();
SubmitButton = new javax.swing.JButton();
ClearButton = new javax.swing.JButton();
ErrorLabel = new javax.swing.JLabel();
SuccessLabel = new javax.swing.JLabel();
ViewButton = new javax.swing.JButton();
jPanel1 = new javax.swing.JPanel();
jScrollPane1 = new javax.swing.JScrollPane();
TransactionTable = new javax.swing.JTable();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Transaction");

TransactionPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Transaction"));

BranchLabel.setText("Branch");

TransactionTypeLabel.setText("Transaction Type");

PCLabel.setText("Principal Company");

PCCombo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Select", "Zuari", "Birla" }));

CustLabel.setText("Customer");

TransactionTypeCombo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Select", "GoDown", "Direct Delivery" }));

CustCombo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Select", "M/S Asociates", "ABC Merchandale" }));

DateLabel.setText("Date");

SubmitButton.setText("Submit");
SubmitButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
SubmitButtonActionPerformed(evt);
}
});

ClearButton.setText("Clear");
ClearButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
ClearButtonActionPerformed(evt);
}
});

ErrorLabel.setForeground(new java.awt.Color(255, 0, 0));
ErrorLabel.setText("Error:");

SuccessLabel.setForeground(new java.awt.Color(0, 153, 0));
SuccessLabel.setText("Success");

javax.swing.GroupLayout TransactionPanelLayout = new javax.swing.GroupLayout(TransactionPanel);
TransactionPanel.setLayout(TransactionPanelLayout);
TransactionPanelLayout.setHorizontalGroup(
TransactionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(TransactionPanelLayout.createSequentialGroup()
.addGroup(TransactionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(TransactionPanelLayout.createSequentialGroup()
.addContainerGap()
.addGroup(TransactionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(TransactionPanelLayout.createSequentialGroup()
.addGroup(TransactionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(BranchLabel)
.addComponent(TransactionTypeLabel)
.addGroup(TransactionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(SubmitButton)
.addComponent(PCLabel))
.addComponent(CustLabel))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 54, Short.MAX_VALUE)
.addGroup(TransactionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(BranchText, javax.swing.GroupLayout.DEFAULT_SIZE, 130, Short.MAX_VALUE)
.addComponent(TransactionTypeCombo, 0, 130, Short.MAX_VALUE)
.addComponent(PCCombo, 0, 130, Short.MAX_VALUE)
.addGroup(TransactionPanelLayout.createSequentialGroup()
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(TransactionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(ClearButton)
.addComponent(DateText, javax.swing.GroupLayout.DEFAULT_SIZE, 130, Short.MAX_VALUE)))
.addGroup(TransactionPanelLayout.createSequentialGroup()
.addGap(3, 3, 3)
.addComponent(CustCombo, 0, 127, Short.MAX_VALUE))))
.addComponent(DateLabel)))
.addGroup(TransactionPanelLayout.createSequentialGroup()
.addGap(33, 33, 33)
.addComponent(ErrorLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 141, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, TransactionPanelLayout.createSequentialGroup()
.addContainerGap()
.addComponent(SuccessLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 142, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap())
);
TransactionPanelLayout.setVerticalGroup(
TransactionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(TransactionPanelLayout.createSequentialGroup()
.addGap(9, 9, 9)
.addComponent(ErrorLabel)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(TransactionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(BranchLabel)
.addComponent(BranchText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(TransactionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(TransactionTypeCombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(TransactionTypeLabel))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(TransactionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(PCCombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(PCLabel))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(TransactionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(CustLabel)
.addComponent(CustCombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(TransactionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(DateLabel)
.addComponent(DateText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(TransactionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(SubmitButton)
.addComponent(ClearButton))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(SuccessLabel)
.addContainerGap(13, Short.MAX_VALUE))
);

ViewButton.setText("View");
ViewButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
ViewButtonActionPerformed(evt);
}
});

jPanel1.setBackground(new java.awt.Color(255, 255, 255));
jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Transaction View"));

TransactionTable.setModel(new javax.swing.table.DefaultTableModel(
data,heading
));
jScrollPane1.setViewportView(TransactionTable);

javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 565, Short.MAX_VALUE))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 151, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(19, Short.MAX_VALUE))
);

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(layout.createSequentialGroup()
.addComponent(TransactionPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(58, 58, 58)
.addComponent(ViewButton)
.addContainerGap(171, Short.MAX_VALUE))))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(142, 142, 142)
.addComponent(ViewButton))
.addGroup(layout.createSequentialGroup()
.addGap(31, 31, 31)
.addComponent(TransactionPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(20, Short.MAX_VALUE))
);

pack();
}// </editor-fold>//GEN-END:initComponents

private void SubmitButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SubmitButtonActionPerformed

if(evt.getSource()==SubmitButton)
{
try{
if(validateData()==true)
{
insertIntoDB();
}
}
catch(Exception e)
{
System.out.println(e);
}
}
}//GEN-LAST:event_SubmitButtonActionPerformed

public Vector getTransac() throws Exception
{
ResultSet rs=null;
rs=st.executeQuery("Select * from Transactions");
while(rs.next())
{
tmp.addElement(rs.getString(1));
tmp.addElement(rs.getString(2));
tmp.addElement(rs.getString(3));
tmp.addElement(rs.getString(4));
tmp.addElement(rs.getString(5));
data.addElement(tmp);
}

/* if(con!=null)
{
con.close();
}*/
return data;
}

private void ClearButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ClearButtonActionPerformed

clearData();
}//GEN-LAST:event_ClearButtonActionPerformed

private void ViewButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ViewButtonActionPerformed
try{
Vector data1=new Vector();
data1=getTransac();
}
catch(Exception e)
{
System.out.println(e);
}
}//GEN-LAST:event_ViewButtonActionPerformed

public static void main(String args[])
{
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
try
{
new Examp().setVisible(true);
}
catch(Exception e)
{
e.printStackTrace();
}
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JLabel BranchLabel;
private javax.swing.JTextField BranchText;
private javax.swing.JButton ClearButton;
private javax.swing.JComboBox CustCombo;
private javax.swing.JLabel CustLabel;
private javax.swing.JLabel DateLabel;
private javax.swing.JTextField DateText;
private javax.swing.JLabel ErrorLabel;
private javax.swing.JComboBox PCCombo;
private javax.swing.JLabel PCLabel;
private javax.swing.JButton SubmitButton;
private javax.swing.JLabel SuccessLabel;
private javax.swing.JPanel TransactionPanel;
private javax.swing.JTable TransactionTable;
private javax.swing.JComboBox TransactionTypeCombo;
private javax.swing.JLabel TransactionTypeLabel;
public javax.swing.JButton ViewButton;
private javax.swing.JPanel jPanel1;
private javax.swing.JScrollPane jScrollPane1;
// End of variables declaration//GEN-END:variables

}


//This is the class for Database Connection(with MS SQL Database)


import java.sql.*;
public class DBCon {
public Connection dbConnection() throws Exception
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
return DriverManager.getConnection("Jdbc:odbc:MyDSN");
}
}


//This is the class to validate the fields...

import java.util.Calendar;
import java.text.SimpleDateFormat;
public class ValidationCheck {

public boolean checkFloat(String in) {
//To Check whether the number is correct or not
try {

//Integer.parseInt(in);
Float.parseFloat(in);

} catch (NumberFormatException ex) {
return false;
}

return true;
}

public boolean checkIfNull(String inp)
{
if((inp.matches(""))||(inp.equals(null)))
{
return false;
}
else
{
return true;
}
}
public boolean checkDateDDMMYY(String date)
{
//To Check Date in DD/MM/YY
if(date.matches("(0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012])[- /.](19|20)\\d\\d"))
{
return true;
}
else
{
return false;
}
}
public boolean checkDateMMDDYY(String date)
{
//To Check Date in MM/DD/YY
if(date.matches("(0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012])[- /.](19|20)\\d\\d"))
{
return true;
}
else
{
return false;
}
}
public boolean checkEmail(String email)
{
//To Check E-Mail
if(email.matches("^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9-]+)*(\\.[_A-Za-z0-9-]+)"))
{
return true;
}
else
{
return false;
}
}
public boolean checkComboNull(String comb)
{
if((comb.matches("Select")))
{
return false;
}
else
{
return true;
}
}

public boolean containsOnlyNumbers(String str) {

//It can't contain only numbers if it's null or empty...
if (str == null || str.length() == 0)
return false;

for (int i = 0; i < str.length(); i++) {

//If we find a non-digit character we return false.
if (!Character.isDigit(str.charAt(i)))
return false;
}

return true;
}

public boolean checkPinCode(String PIN)
{
if(PIN.matches("\\d{6}"))
{
return true;
}
else
{
return false;
}
}
public boolean checkPhone(String phone)
{
if(phone.matches("\\d{10}"))
{
return true;
}
else
{
return false;
}
}
public boolean checkName(String name)
{
if(name.matches("[a-zA-Z]*" ))
{
return true;
}
else
{
return false;
}
}

public boolean dateValidator(String dateString)
{
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
Calendar cal = Calendar.getInstance();
try
{
cal.setTime(sdf.parse(dateString));
if(dateString.equals(sdf.format(cal.getTime())))
{
return true;
}
else
{
return false;
}
}
catch(Exception ex)
{
return false;
}


}

public boolean checkFullName(String fullname)
{
if(fullname.matches("[a-zA-z]+([ '-./][a-zA-Z]+)*"))
{
return true;
}
else
{
return false;
}
}

public boolean checkURL(String urlname)
{

if(urlname.matches("(http[s]?://)?[A-Za-z]+(\\.[A-Za-z0-9-]+)*(\\.[_A-Za-z]+)"))
{
return true;
}
else
{
return false;
}
}

}

Please Reply......

This post has been edited by tinoymalayil: 16 July 2009 - 09:31 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Showing NullPointerException when tried to retrieve data to JTable fro

#2 syfran  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 83
  • View blog
  • Posts: 1,103
  • Joined: 12-July 09

Re: Showing NullPointerException when tried to retrieve data to JTable fro

Posted 16 July 2009 - 09:17 AM

First...
:code:
And also show the exact error you are getting.
Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Showing NullPointerException when tried to retrieve data to JTable fro

Posted 16 July 2009 - 04:54 PM

Code produced by a GUI generator is almost impossible to read for a buman being
Make sure you cliked/selected all the appropriate options in your GUI generator
Was This Post Helpful? 0
  • +
  • -

#4 tinoymalayil  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 23-June 08

Re: Showing NullPointerException when tried to retrieve data to JTable fro

Posted 16 July 2009 - 09:14 PM

View Postpbl, on 16 Jul, 2009 - 03:54 PM, said:

Code produced by a GUI generator is almost impossible to read for a buman being
Make sure you cliked/selected all the appropriate options in your GUI generator


This is the originalcode that is generated using Netbeans IDE.
I pasted here after opening it with Editplus...

The Error is:
When I clicked the View Button it shows

NullPointerException

I want to get the data in JTable from the database
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Showing NullPointerException when tried to retrieve data to JTable fro

Posted 16 July 2009 - 09:45 PM

View Posttinoymalayil, on 16 Jul, 2009 - 08:14 PM, said:

View Postpbl, on 16 Jul, 2009 - 03:54 PM, said:

Code produced by a GUI generator is almost impossible to read for a buman being
Make sure you cliked/selected all the appropriate options in your GUI generator


This is the originalcode that is generated using Netbeans IDE.
I pasted here after opening it with Editplus...

The Error is:
When I clicked the View Button it shows

NullPointerException

I want to get the data in JTable from the database

Go back edit your first post
:code:

but as I told you if Netbeans is the culprit because you don't know how to use it and if your GUI is complicated there is not much we can do

This post has been edited by pbl: 16 July 2009 - 09:47 PM

Was This Post Helpful? 0
  • +
  • -

#6 tinoymalayil  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 23-June 08

Re: Showing NullPointerException when tried to retrieve data to JTable fro

Posted 17 July 2009 - 02:26 AM

View Postpbl, on 16 Jul, 2009 - 08:45 PM, said:

View Posttinoymalayil, on 16 Jul, 2009 - 08:14 PM, said:

View Postpbl, on 16 Jul, 2009 - 03:54 PM, said:

Code produced by a GUI generator is almost impossible to read for a buman being
Make sure you cliked/selected all the appropriate options in your GUI generator


This is the originalcode that is generated using Netbeans IDE.
I pasted here after opening it with Editplus...

The Error is:
When I clicked the View Button it shows

NullPointerException

I want to get the data in JTable from the database

Go back edit your first post
:code:

but as I told you if Netbeans is the culprit because you don't know how to use it and if your GUI is complicated there is not much we can do



//This is my Class with main method

import java.sql.Connection;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.sql.ResultSet;
import java.util.Vector;
import java.util.Date;
public class Examp extends javax.swing.JFrame {


DBCon DB=new DBCon();
ValidationCheck vc=new ValidationCheck();
Vector tmp=new Vector();
Vector data=new Vector();
Vector heading=new Vector();
Connection con=null;
Statement st=null;
String Branch;
String TransactionType;
String PC;
String Cust;
public Examp() throws Exception {
heading.addElement("Branch");
heading.addElement("Transaction Type");
heading.addElement("Principal Company");
heading.addElement("Customer");
heading.addElement("Date");
initComponents();
SimpleDateFormat dateformat=new SimpleDateFormat("dd/MM/yyyy");
Date date=new Date();
DateText.setText(dateformat.format(date));
clearData();
}
public void insertIntoDB() throws Exception
{
con=DB.dbConnection();
st=con.createStatement();
st.executeUpdate("Insert into Transactions values('"+BranchText.getText()+ "','"+TransactionTypeCombo.getSelectedItem()+"','"+PCCombo.getSelectedItem()+"','"+CustCombo.getSelectedItem()+"','"+DateText.getText()+"')");
}

public boolean validateData()
{
Branch=BranchText.getText();
TransactionType=(String)TransactionTypeCombo.getSelectedItem();
PC=(String)PCCombo.getSelectedItem();
Cust=(String)CustCombo.getSelectedItem();
if(vc.checkIfNull(Branch)==false)
{
ErrorLabel.setText("Please Enter Branch");
return false;
}
else if(vc.checkFullName(Branch)==false)
{
ErrorLabel.setText("Branch Name is Incorrect");
return false;
}
else if(vc.checkComboNull(TransactionType)==false)
{
ErrorLabel.setText("Please Select Transaction Type");
return false;
}
else if(vc.checkComboNull(PC)==false)
{
ErrorLabel.setText("Please Select Principal Company");
return false;
}
else if(vc.checkComboNull(Cust)==false)
{
ErrorLabel.setText("Please Select Customer");
return false;
}
else
{
ErrorLabel.setText("");
SuccessLabel.setText("Entered Successfully");
SuccessLabel.setVisible(true);
return true;
}
}

public void clearData()
{
BranchText.setText("");
TransactionTypeCombo.setSelectedItem("Select");
PCCombo.setSelectedItem("Select");
CustCombo.setSelectedItem("Select");
ErrorLabel.setText("");
SuccessLabel.setText("");
}

@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {

TransactionPanel = new javax.swing.JPanel();
BranchLabel = new javax.swing.JLabel();
BranchText = new javax.swing.JTextField();
TransactionTypeLabel = new javax.swing.JLabel();
PCLabel = new javax.swing.JLabel();
PCCombo = new javax.swing.JComboBox();
CustLabel = new javax.swing.JLabel();
TransactionTypeCombo = new javax.swing.JComboBox();
CustCombo = new javax.swing.JComboBox();
DateLabel = new javax.swing.JLabel();
DateText = new javax.swing.JTextField();
SubmitButton = new javax.swing.JButton();
ClearButton = new javax.swing.JButton();
ErrorLabel = new javax.swing.JLabel();
SuccessLabel = new javax.swing.JLabel();
ViewButton = new javax.swing.JButton();
jPanel1 = new javax.swing.JPanel();
jScrollPane1 = new javax.swing.JScrollPane();
TransactionTable = new javax.swing.JTable();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Transaction");

TransactionPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Transaction"));

BranchLabel.setText("Branch");

TransactionTypeLabel.setText("Transaction Type");

PCLabel.setText("Principal Company");

PCCombo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Select", "Zuari", "Birla" }));

CustLabel.setText("Customer");

TransactionTypeCombo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Select", "GoDown", "Direct Delivery" }));

CustCombo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Select", "M/S Asociates", "ABC Merchandale" }));

DateLabel.setText("Date");

SubmitButton.setText("Submit");
SubmitButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
SubmitButtonActionPerformed(evt);
}
});

ClearButton.setText("Clear");
ClearButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
ClearButtonActionPerformed(evt);
}
});

ErrorLabel.setForeground(new java.awt.Color(255, 0, 0));
ErrorLabel.setText("Error:");

SuccessLabel.setForeground(new java.awt.Color(0, 153, 0));
SuccessLabel.setText("Success");

javax.swing.GroupLayout TransactionPanelLayout = new javax.swing.GroupLayout(TransactionPanel);
TransactionPanel.setLayout(TransactionPanelLayout);
TransactionPanelLayout.setHorizontalGroup(
TransactionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(TransactionPanelLayout.createSequentialGroup()
.addGroup(TransactionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(TransactionPanelLayout.createSequentialGroup()
.addContainerGap()
.addGroup(TransactionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(TransactionPanelLayout.createSequentialGroup()
.addGroup(TransactionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(BranchLabel)
.addComponent(TransactionTypeLabel)
.addGroup(TransactionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(SubmitButton)
.addComponent(PCLabel))
.addComponent(CustLabel))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 54, Short.MAX_VALUE)
.addGroup(TransactionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(BranchText, javax.swing.GroupLayout.DEFAULT_SIZE, 130, Short.MAX_VALUE)
.addComponent(TransactionTypeCombo, 0, 130, Short.MAX_VALUE)
.addComponent(PCCombo, 0, 130, Short.MAX_VALUE)
.addGroup(TransactionPanelLayout.createSequentialGroup()
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(TransactionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(ClearButton)
.addComponent(DateText, javax.swing.GroupLayout.DEFAULT_SIZE, 130, Short.MAX_VALUE)))
.addGroup(TransactionPanelLayout.createSequentialGroup()
.addGap(3, 3, 3)
.addComponent(CustCombo, 0, 127, Short.MAX_VALUE))))
.addComponent(DateLabel)))
.addGroup(TransactionPanelLayout.createSequentialGroup()
.addGap(33, 33, 33)
.addComponent(ErrorLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 141, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, TransactionPanelLayout.createSequentialGroup()
.addContainerGap()
.addComponent(SuccessLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 142, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap())
);
TransactionPanelLayout.setVerticalGroup(
TransactionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(TransactionPanelLayout.createSequentialGroup()
.addGap(9, 9, 9)
.addComponent(ErrorLabel)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(TransactionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(BranchLabel)
.addComponent(BranchText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(TransactionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(TransactionTypeCombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(TransactionTypeLabel))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(TransactionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(PCCombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(PCLabel))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(TransactionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(CustLabel)
.addComponent(CustCombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(TransactionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(DateLabel)
.addComponent(DateText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(TransactionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(SubmitButton)
.addComponent(ClearButton))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(SuccessLabel)
.addContainerGap(13, Short.MAX_VALUE))
);

ViewButton.setText("View");
ViewButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
ViewButtonActionPerformed(evt);
}
});

jPanel1.setBackground(new java.awt.Color(255, 255, 255));
jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Transaction View"));

TransactionTable.setModel(new javax.swing.table.DefaultTableModel(
data,heading
));
jScrollPane1.setViewportView(TransactionTable);

javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 565, Short.MAX_VALUE))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 151, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(19, Short.MAX_VALUE))
);

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(layout.createSequentialGroup()
.addComponent(TransactionPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(58, 58, 58)
.addComponent(ViewButton)
.addContainerGap(171, Short.MAX_VALUE))))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(142, 142, 142)
.addComponent(ViewButton))
.addGroup(layout.createSequentialGroup()
.addGap(31, 31, 31)
.addComponent(TransactionPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(20, Short.MAX_VALUE))
);

pack();
}// </editor-fold>//GEN-END:initComponents

private void SubmitButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SubmitButtonActionPerformed

if(evt.getSource()==SubmitButton)
{
try{
if(validateData()==true)
{
insertIntoDB();
}
}
catch(Exception e)
{
System.out.println(e);
}
}
}//GEN-LAST:event_SubmitButtonActionPerformed

public Vector getTransac() throws Exception
{
ResultSet rs=null;
rs=st.executeQuery("Select * from Transactions");
while(rs.next())
{ 
tmp.addElement(rs.getString(1));
tmp.addElement(rs.getString(2));
tmp.addElement(rs.getString(3));
tmp.addElement(rs.getString(4));
tmp.addElement(rs.getString(5));
data.addElement(tmp);
}

/* if(con!=null)
{
con.close();
}*/
return data;
}

private void ClearButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ClearButtonActionPerformed

clearData();
}//GEN-LAST:event_ClearButtonActionPerformed

private void ViewButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ViewButtonActionPerformed
try{
Vector data1=new Vector();
data1=getTransac();
}
catch(Exception e)
{
System.out.println(e);
}
}//GEN-LAST:event_ViewButtonActionPerformed

public static void main(String args[])
{
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
try
{
new Examp().setVisible(true);
}
catch(Exception e)
{
e.printStackTrace();
}
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JLabel BranchLabel;
private javax.swing.JTextField BranchText;
private javax.swing.JButton ClearButton;
private javax.swing.JComboBox CustCombo;
private javax.swing.JLabel CustLabel;
private javax.swing.JLabel DateLabel;
private javax.swing.JTextField DateText;
private javax.swing.JLabel ErrorLabel;
private javax.swing.JComboBox PCCombo;
private javax.swing.JLabel PCLabel;
private javax.swing.JButton SubmitButton;
private javax.swing.JLabel SuccessLabel;
private javax.swing.JPanel TransactionPanel;
private javax.swing.JTable TransactionTable;
private javax.swing.JComboBox TransactionTypeCombo;
private javax.swing.JLabel TransactionTypeLabel;
public javax.swing.JButton ViewButton;
private javax.swing.JPanel jPanel1;
private javax.swing.JScrollPane jScrollPane1;
// End of variables declaration//GEN-END:variables

}


//This is the class for Database Connection(with MS SQL Database)


import java.sql.*;
public class DBCon {
public Connection dbConnection() throws Exception
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
return DriverManager.getConnection("Jdbc:odbc:MyDSN");
}
}


//This is the class to validate the fields...

import java.util.Calendar;
import java.text.SimpleDateFormat;
public class ValidationCheck {

public boolean checkFloat(String in) {
//To Check whether the number is correct or not
try {

//Integer.parseInt(in);
Float.parseFloat(in);

} catch (NumberFormatException ex) {
return false;
}

return true;
}

public boolean checkIfNull(String inp)
{
if((inp.matches(""))||(inp.equals(null)))
{
return false;
}
else
{
return true;
}
}
public boolean checkDateDDMMYY(String date)
{
//To Check Date in DD/MM/YY
if(date.matches("(0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012])[- /.](19|20)\\d\\d"))
{
return true;
}
else
{
return false;
}
}
public boolean checkDateMMDDYY(String date)
{
//To Check Date in MM/DD/YY
if(date.matches("(0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012])[- /.](19|20)\\d\\d"))
{
return true;
}
else
{
return false;
}
}
public boolean checkEmail(String email)
{
//To Check E-Mail
if(email.matches("^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9-]+)*(\\.[_A-Za-z0-9-]+)"))
{
return true;
}
else
{
return false;
}
}
public boolean checkComboNull(String comb)
{
if((comb.matches("Select")))
{
return false;
}
else
{
return true;
}
}

public boolean containsOnlyNumbers(String str) {

//It can't contain only numbers if it's null or empty...
if (str == null || str.length() == 0)
return false;

for (int i = 0; i < str.length(); i++) {

//If we find a non-digit character we return false.
if (!Character.isDigit(str.charAt(i)))
return false;
}

return true;
}

public boolean checkPinCode(String PIN)
{
if(PIN.matches("\\d{6}"))
{
return true;
}
else
{
return false;
}
}
public boolean checkPhone(String phone)
{
if(phone.matches("\\d{10}"))
{
return true;
}
else
{
return false;
}
}
public boolean checkName(String name)
{
if(name.matches("[a-zA-Z]*" ))
{
return true;
}
else
{
return false;
}
}

public boolean dateValidator(String dateString)
{
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
Calendar cal = Calendar.getInstance();
try
{
cal.setTime(sdf.parse(dateString));
if(dateString.equals(sdf.format(cal.getTime())))
{
return true;
}
else
{
return false;
}
}
catch(Exception ex)
{
return false;
}


}

public boolean checkFullName(String fullname)
{
if(fullname.matches("[a-zA-z]+([ '-./][a-zA-Z]+)*"))
{
return true;
}
else
{
return false;
}
}

public boolean checkURL(String urlname)
{

if(urlname.matches("(http[s]?/)?[A-Za-z]+(\\.[A-Za-z0-9-]+)*(\\.[_A-Za-z]+)"))
{
return true;
}
else
{
return false;
}
}

}




*Edited to replace the [ quote] tag by a [ code] tag so the code can be readable

:code:

This post has been edited by pbl: 17 July 2009 - 08:42 PM

Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Showing NullPointerException when tried to retrieve data to JTable fro

Posted 17 July 2009 - 08:53 PM

Changing the tag didn't fix the identation problem
so here is it's indented code

[code]
import java.sql.*;
import java.util.Date;

import javax.swing.*;
import java.text.SimpleDateFormat;
import java.util.*;
import java.text.SimpleDateFormat;



class Examp extends JFrame {


DBCon DB = new DBCon();
ValidationCheck vc=new ValidationCheck();
Vector tmp=new Vector();
Vector data=new Vector();
Vector heading=new Vector();
Connection con=null;
Statement st=null;
String Branch;
String TransactionType;
String PC;
String Cust;

public Examp() throws Exception {
heading.addElement("Branch");
heading.addElement("Transaction Type");
heading.addElement("Principal Company");
heading.addElement("Customer");
heading.addElement("Date");
initComponents();
SimpleDateFormat dateformat=new SimpleDateFormat("dd/MM/yyyy");
Date date=new Date();
DateText.setText(dateformat.format(date));
clearData();
}
public void insertIntoDB() throws Exception
{
con=DB.dbConnection();
st=con.createStatement();
st.executeUpdate("Insert into Transactions values('"+BranchText.getText()+ "','"+TransactionTypeCombo.getSelectedItem()+"','"+PCCombo.getSelectedItem()+"','"+CustCombo.getSelectedItem()+"','"+DateText.getText()+"')");
}

public boolean validateData()
{
Branch=BranchText.getText();
TransactionType=(String)TransactionTypeCombo.getSelectedItem();
PC=(String)PCCombo.getSelectedItem();
Cust=(String)CustCombo.getSelectedItem();
if(vc.checkIfNull(Branch)==false)
{
ErrorLabel.setText("Please Enter Branch");
return false;
}
else if(vc.checkFullName(Branch)==false)
{
ErrorLabel.setText("Branch Name is Incorrect");
return false;
}
else if(vc.checkComboNull(TransactionType)==false)
{
ErrorLabel.setText("Please Select Transaction Type");
return false;
}
else if(vc.checkComboNull(PC)==false)
{
ErrorLabel.setText("Please Select Principal Company");
return false;
}
else if(vc.checkComboNull(Cust)==false)
{
ErrorLabel.setText("Please Select Customer");
return false;
}
else
{
ErrorLabel.setText("");
SuccessLabel.setText("Entered Successfully");
SuccessLabel.setVisible(true);
return true;
}
}

public void clearData()
{
BranchText.setText("");
TransactionTypeCombo.setSelectedItem("Select");
PCCombo.setSelectedItem("Select");
CustCombo.setSelectedItem("Select");
ErrorLabel.setText("");
SuccessLabel.setText("");
}

@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {

TransactionPanel = new javax.swing.JPanel();
BranchLabel = new javax.swing.JLabel();
BranchText = new javax.swing.JTextField();
TransactionTypeLabel = new javax.swing.JLabel();
PCLabel = new javax.swing.JLabel();
PCCombo = new javax.swing.JComboBox();
CustLabel = new javax.swing.JLabel();
TransactionTypeCombo = new javax.swing.JComboBox();
CustCombo = new javax.swing.JComboBox();
DateLabel = new javax.swing.JLabel();
DateText = new javax.swing.JTextField();
SubmitButton = new javax.swing.JButton();
ClearButton = new javax.swing.JButton();
ErrorLabel = new javax.swing.JLabel();
SuccessLabel = new javax.swing.JLabel();
ViewButton = new javax.swing.JButton();
jPanel1 = new javax.swing.JPanel();
jScrollPane1 = new javax.swing.JScrollPane();
TransactionTable = new javax.swing.JTable();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Transaction");

TransactionPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Transaction"));

BranchLabel.setText("Branch");

TransactionTypeLabel.setText("Transaction Type");

PCLabel.setText("Principal Company");

PCCombo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Select", "Zuari", "Birla" }));

CustLabel.setText("Customer");

TransactionTypeCombo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Select", "GoDown", "Direct Delivery" }));

CustCombo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Select", "M/S Asociates", "ABC Merchandale" }));

DateLabel.setText("Date");

SubmitButton.setText("Submit");
SubmitButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
SubmitButtonActionPerformed(evt);
}
});

ClearButton.setText("Clear");
ClearButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
ClearButtonActionPerformed(evt);
}
});

ErrorLabel.setForeground(new java.awt.Color(255, 0, 0));
ErrorLabel.setText("Error:");

SuccessLabel.setForeground(new java.awt.Color(0, 153, 0));
SuccessLabel.setText("Success");

javax.swing.GroupLayout TransactionPanelLayout = new javax.swing.GroupLayout(TransactionPanel);
TransactionPanel.setLayout(TransactionPanelLayout);
TransactionPanelLayout.setHorizontalGroup(
TransactionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(TransactionPanelLayout.createSequentialGroup()
.addGroup(TransactionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(TransactionPanelLayout.createSequentialGroup()
.addContainerGap()
.addGroup(TransactionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(TransactionPanelLayout.createSequentialGroup()
.addGroup(TransactionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(BranchLabel)
.addComponent(TransactionTypeLabel)
.addGroup(TransactionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(SubmitButton)
.addComponent(PCLabel))
.addComponent(CustLabel))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 54, Short.MAX_VALUE)
.addGroup(TransactionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(BranchText, javax.swing.GroupLayout.DEFAULT_SIZE, 130, Short.MAX_VALUE)
.addComponent(TransactionTypeCombo, 0, 130, Short.MAX_VALUE)
.addComponent(PCCombo, 0, 130, Short.MAX_VALUE)
.addGroup(TransactionPanelLayout.createSequentialGroup()
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(TransactionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(ClearButton)
.addComponent(DateText, javax.swing.GroupLayout.DEFAULT_SIZE, 130, Short.MAX_VALUE)))
.addGroup(TransactionPanelLayout.createSequentialGroup()
.addGap(3, 3, 3)
.addComponent(CustCombo, 0, 127, Short.MAX_VALUE))))
.addComponent(DateLabel)))
.addGroup(TransactionPanelLayout.createSequentialGroup()
.addGap(33, 33, 33)
.addComponent(ErrorLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 141, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, TransactionPanelLayout.createSequentialGroup()
.addContainerGap()
.addComponent(SuccessLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 142, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap())
);
TransactionPanelLayout.setVerticalGroup(
TransactionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(TransactionPanelLayout.createSequentialGroup()
.addGap(9, 9, 9)
.addComponent(ErrorLabel)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(TransactionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(BranchLabel)
.addComponent(BranchText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(TransactionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(TransactionTypeCombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(TransactionTypeLabel))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(TransactionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(PCCombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(PCLabel))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(TransactionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(CustLabel)
.addComponent(CustCombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(TransactionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(DateLabel)
.addComponent(DateText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(TransactionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(SubmitButton)
.addComponent(ClearButton))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(SuccessLabel)
.addContainerGap(13, Short.MAX_VALUE))
);

ViewButton.setText("View");
ViewButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
ViewButtonActionPerformed(evt);
}
});

jPanel1.setBackground(new java.awt.Color(255, 255, 255));
jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Transaction View"));

TransactionTable.setModel(new javax.swing.table.DefaultTableModel(
data,heading
));
jScrollPane1.setViewportView(TransactionTable);

javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 565, Short.MAX_VALUE))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 151, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(19, Short.MAX_VALUE))
);

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(layout.createSequentialGroup()
.addComponent(TransactionPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(58, 58, 58)
.addComponent(ViewButton)
.addContainerGap(171, Short.MAX_VALUE))))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(142, 142, 142)
.addComponent(ViewButton))
.addGroup(layout.createSequentialGroup()
.addGap(31, 31, 31)
.addComponent(TransactionPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(20, Short.MAX_VALUE))
);

pack();
}// </editor-fold>//GEN-END:initComponents

private void SubmitButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SubmitButtonActionPerformed

if(evt.getSource()==SubmitButton)
{
try{
if(validateData()==true)
{
insertIntoDB();
}
}
catch(Exception e)
{
System.out.println(e);
}
}
}//GEN-LAST:event_SubmitButtonActionPerformed

public Vector getTransac() throws Exception
{
ResultSet rs=null;
rs=st.executeQuery("Select * from Transactions");
while(rs.next())
{
tmp.addElement(rs.getString(1));
tmp.addElement(rs.getString(2));
tmp.addElement(rs.getString(3));
tmp.addElement(rs.getString(4));
tmp.addElement(rs.getString(5));
data.addElement(tmp);
}

/* if(con!=null)
{
con.close();
}*/
return data;
}

private void ClearButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ClearButtonActionPerformed

clearData();
}//GEN-LAST:event_ClearButtonActionPerformed

private void ViewButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ViewButtonActionPerformed
try{
Vector data1=new Vector();
data1=getTransac();
}
catch(Exception e)
{
System.out.println(e);
}
}//GEN-LAST:event_ViewButtonActionPerformed

public static void main(String args[])
{
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
try
{
new Examp().setVisible(true);
}
catch(Exception e)
{
e.printStackTrace();
}
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JLabel BranchLabel;
private javax.swing.JTextField BranchText;
private javax.swing.JButton ClearButton;
private javax.swing.JComboBox CustCombo;
private javax.swing.JLabel CustLabel;
private javax.swing.JLabel DateLabel;
private javax.swing.JTextField DateText;
private javax.swing.JLabel ErrorLabel;
private javax.swing.JComboBox PCCombo;
private javax.swing.JLabel PCLabel;
private javax.swing.JButton SubmitButton;
private javax.swing.JLabel SuccessLabel;
private javax.swing.JPanel TransactionPanel;
private javax.swing.JTable TransactionTable;
private javax.swing.JComboBox TransactionTypeCombo;
private javax.swing.JLabel TransactionTypeLabel;
public javax.swing.JButton ViewButton;
private javax.swing.JPanel jPanel1;
private javax.swing.JScrollPane jScrollPane1;
// End of variables declaration//GEN-END:variables

}


//This is the class for Database Connection(with MS SQL Database)

class DBCon {
public Connection dbConnection() throws Exception
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
return DriverManager.getConnection("Jdbc:odbc:MyDSN");
}
}


//This is the class to validate the fields...

class ValidationCheck {

public boolean checkFloat(String in) {
//To Check whether the number is correct or not
try {

//Integer.parseInt(in);
Float.parseFloat(in);

} catch (NumberFormatException ex) {
return false;
}

return true;
}

public boolean checkIfNull(String inp)
{
if((inp.matches(""))||(inp.equals(null)))
{
return false;
}
else
{
return true;
}
}
public boolean checkDateDDMMYY(String date)
{
//To Check Date in DD/MM/YY
if(date.matches("(0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012])[- /.](19|20)\\d\\d"))
{
return true;
}
else
{
return false;
}
}
public boolean checkDateMMDDYY(String date)
{
//To Check Date in MM/DD/YY
if(date.matches("(0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012])[- /.](19|20)\\d\\d"))
{
return true;
}
else
{
Was This Post Helpful? 0
  • +
  • -

#8 mostyfriedman  Icon User is online

  • The Algorithmi
  • member icon

Reputation: 725
  • View blog
  • Posts: 4,471
  • Joined: 24-October 08

Re: Showing NullPointerException when tried to retrieve data to JTable fro

Posted 17 July 2009 - 09:27 PM

the whole CTRL+SHIFT+F trick always works eh ;)
Was This Post Helpful? 0
  • +
  • -

#9 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Showing NullPointerException when tried to retrieve data to JTable fro

Posted 17 July 2009 - 09:36 PM

View Postmostyfriedman, on 17 Jul, 2009 - 08:27 PM, said:

the whole CTRL+SHIFT+F trick always works eh ;)

What is that stuff ?
That I'd never heard about ?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1