• Post Reply Bookmark Topic Watch Topic
  • New Topic

Please help! runtime errors.....

 
Lori Battey
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know this is long but I'm getting desperate!
I have included chunks of my code and the console messages I'm getting....I'm using Visual Cafe 4 (Pro) and Access 2000 for my database. Why is this compiling but won't run? What could I possibly be doing wrong??
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;
import javax.swing.text.*;
import java.sql.*;
import java.lang.*;
import java.util.*;
import java.io.*;
public class Distribution extends JFrame
implements ActionListener
{
public static void main(String[] args)
{
new Distribution(); //****************Line 17
}
private Container contentPane;
//to hold dynamic lists
DefaultListModel products ;//= new DefaultListModel();
DefaultListModel invoices ;//= new DefaultListModel(); DefaultListModel lineItems ;//= new DefaultListModel();
DefaultComboBoxModel customers ;//= new DefaultComboBoxModel();
//data containers for lists
JList prodList ;//= new JList(products);
JList invList ;//= new JList(invoices);
JList invProdList ;//= new JList (lineItems);
//so windows will scroll
JScrollPane prodScroll ;//= new JScrollPane (prodList);
JScrollPane invScroll ;//= new JScrollPane (invList);
JScrollPane invProdScroll ;//= new JScrollPane (invProdList);
//labels for windows
JLabel prodLbl ;//= new JLabel ("Products");
JLabel invLbl ;//= new JLabel ("Invoices");
JLabel invProdLbl ;//= new JLabel("On this Invoice");
//data options buttons
//for product window
JButton creProd ;//= new JButton ("Create");
JButton editProd ;//= new JButton("Edit");
JButton remProd ;//= new JButton ("Remove");
//for invoice window
JButton creInv ;//= new JButton ("Create");
JButton editInv ;//= new JButton("Edit");
JButton remInv ;//= new JButton ("Remove");
//for invoice lineitems
JButton addItem ;//= new JButton("Add new Item");
JButton remItem ;//= new JButton("Remove Item");
//for JDialogs
JButton btnDelete ;//= new JButton ("Confirm Delete");
//menu items to correspond with option buttons
JMenuBar menuBar ;//= new JMenuBar();
JMenu menuFile ;//= new JMenu("File Maintenance");
JMenu menuProd ;//= new JMenu ("Products");
JMenu menuInv ;//= new JMenu ("Invoices");
JMenuItem miNew ;//= new JMenuItem ("New");
//private JMenuItem miSave = new JMenuItem ("Save");
JMenuItem miOpen ;//= new JMenuItem ("Open...");
JMenuItem miExit ;//= new JMenuItem ("Exit");
JMenuItem miCreProd ;//= new JMenuItem ("Create Product");
JMenuItem miEditProd ;//= new JMenuItem("Edit Product");
JMenuItem miRemProd ;//= new JMenuItem ("Remove Product");
JMenuItem miCreInv ;//= new JMenuItem ("Create Invoice");
JMenuItem miEditInv ;//= new JMenuItem("Edit Invoice");
JMenuItem miRemInv ;//= new JMenuItem ("Remove Invoice");
JMenuItem miAddItem ;//= new JMenuItem("Add Item");
JMenuItem miRemItem ;//= new JMenuItem ("Remove item");
String url
= "jdbc dbc istributionDB";
String user = "dba";
String password = "sql";
Connection conn = null;
Statement stmt = null;
PreparedStatement pstmtAddProd = null;
PreparedStatement pstmtEditProd = null;
PreparedStatement pstmtDeleteProd = null;
PreparedStatement pstmtAddInv = null;
PreparedStatement pstmtSelectInv = null;
PreparedStatement pstmtEditInv = null;
PreparedStatement pstmtAddCust = null;
PreparedStatement pstmtSelectCust = null;
PreparedStatement pstmtDeleteCust = null;
ResultSet queryProducts;
ResultSet queryInvoices;
ResultSet queryCustomers;
ResultSet getNumber;
ResultSet queryLineItems;
//for cost
// private JLabel totalCost; STILL NEED TO DO!!
public Distribution()
{
try
{
//contentpane
contentPane = this.getContentPane();
contentPane.setLayout(null);
this.setBounds(150, 50, 500, 500);
this.setTitle("Products and Invoices/Entry and Maintenance");
try
{
//type1 - loading bridge driver
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
//connect to the database
conn = DriverManager.getConnection(url, user, password);
//create a statement to send sql
stmt = conn.createStatement();
//create a statement to send sql
pstmtAddProd = conn.prepareStatement("INSERT into products VALUES (?,?,?)");
pstmtEditProd = conn.prepareStatement("UPDATE products SET " +
"part_descrip tion = ? , price = ? WHERE model_number = ?");
pstmtDeleteProd = conn.prepareStatement("DELETE from products WHERE model_number = ?");
pstmtAddInv = conn.prepareStatement("INSERT into invoices VALUES(?,?)");
pstmtSelectInv = conn.prepareStatement("SELECT *
from invoices where invoice_number = ?");
pstmtEditInv = conn.prepareStatement("UPDATE invoices set customer_name = ? where invoice_number = ?");
pstmtAddCust = conn.prepareStatement("INSERT into customers VALUES(?,?)");
pstmtSelectCust = conn.prepareStatement("SELECT
* from customers where customer_number = ?");
pstmtDeleteCust = conn.prepareStatement("DELETE from customers WHERE customer_number = ?");
}
catch (ClassNotFoundException e)
{
System.out.println("Error: " + e.getMessage());
}
catch (SQLException e)
{
System.out.println("Error: " + e.getMessage());
}
finally
{
System.out.println("finally");
}
products = new DefaultListModel();
invoices = new DefaultListModel();
lineItems = new DefaultListModel();
customers = new DefaultComboBoxModel();
//data containers for lists
prodList = new JList(products);
invList = new JList(invoices);
invProdList = new JList(lineItems);
//so windows will scroll
prodScroll = new JScrollPane(prodList);
invScroll = new JScrollPane(invList);
invProdScroll = new JScrollPane(invProdList);
//labels for windows
prodLbl = new JLabel("Products");
invLbl = new JLabel("Invoices");
invProdLbl = new JLabel("On this Invoice");
//data options buttons
//for product window
creProd = new JButton("Create");
editProd = new JButton("Edit");
remProd = new JButton("Remove");
//for invoice window
creInv = new JButton("Create");
editInv = new JButton("Edit");
remInv = new JButton("Remove");
//for invoice lineitems
addItem = new JButton("Add new Item");
remItem = new JButton("Remove Item");
//for JDialogs
btnDelete = new JButton("Confirm Delete");
//menu items to correspond with option buttons
menuBar = new JMenuBar();
menuFile = new JMenu("File Maintenance");
menuProd = new JMenu("Products");
menuInv = new JMenu("Invoices");
miNew = new JMenuItem("New");
//private JMenuItem miSave = new JMenuItem("Save");
miOpen = new JMenuItem("Open...");
miExit = new JMenuItem("Exit");
miCreProd = new JMenuItem("Create Product");
miEditProd = new JMenuItem("Edit Product");
miRemProd = new JMenuItem("Remove Product");
miCreInv = new JMenuItem("Create Invoice");
miEditInv = new JMenuItem("Edit Invoice");
miRemInv = new JMenuItem("Remove Invoice");
miAddItem = new JMenuItem("Add Item");
miRemItem = new JMenuItem("Remove item");
//add to contentpane and locate
contentPane.add(prodLbl).setBounds(55, 30, 75, 25);
contentPane.add(prodScroll).setBounds(15,75, 140, 300);
contentPane.add(invLbl).setBounds(210, 30, 75, 25);
contentPane.add(invScroll).setBounds(175, 75, 135, 305);
contentPane.add(invProdLbl).setBounds(360, 30, 100, 25);
contentPane.add(invProdScroll).setBounds(335, 75, 140, 300);
contentPane.add(creProd).setBounds(15,388,75,25);
contentPane.add(editProd).setBounds(95,388,60,25);
contentPane.add(remProd).setBounds(40,418,90,25);
contentPane.add(creInv).setBounds(175,388,75,25);
contentPane.add(editInv).setBounds(255,388,60,25);
contentPane.add(remInv).setBounds(200,418,90,25);
contentPane.add(addItem).setBounds(345,388,120,25);
contentPane.add(remItem).setBounds(345,418,120,25);
//menu setup
menuFile.add(miNew);
menuFile.addSeparator();
//menuFile.add(miSave);
menuFile.add(miOpen);
menuFile.addSeparator();
menuFile.add(miExit);
menuProd.add(miCreProd);
menuProd.add(miEditProd);
menuProd.add(miRemProd);
menuInv.add(miCreInv);
menuInv.add(miEditInv);
menuInv.add(miRemInv);
menuInv.addSeparator();
menuInv.add(miAddItem);
menuInv.add(miRemItem);
menuBar.add(menuFile);
menuBar.add(menuProd);
menuBar.add(menuInv);
this.setJMenuBar(menuBar);
//action listeners
//ActionAdapt aa = new ActionAdapt();
miCreProd.addActionListener(this);
miEditProd.addActionListener(this);//
miRemProd.addActionListener(this);
miCreInv.addActionListener(this);
miEditInv.addActionListener(this);
miRemInv.addActionListener(this);
miAddItem.addActionListener(this);
miRemItem.addActionListener(this);
creProd.addActionListener(this);
editProd.addActionListener(this);
remProd.addActionListener(this);
creInv.addActionListener(this);
editInv.addActionListener(this);
remInv.addActionListener(this);
addItem.addActionListener(this);
remItem.addActionListener(this);
//miSave.addActionListener(aa);
miOpen.addActionListener(this);
//settings for menu items & option buttons//
miCreProd.setEnabled(true);
miEditProd.setEnabled(false);
miRemProd.setEnabled(false);
miCreInv.setEnabled(false);
miEditInv.setEnabled(false);
miRemInv.setEnabled(false);
miAddItem.setEnabled(false);
miRemItem.setEnabled(false);
//miSave.setEnabled(false);
miOpen.setEnabled(false);
creProd.setEnabled(true);
editProd.setEnabled(false);
remProd.setEnabled(false);
creInv.setEnabled(false);
editInv.setEnabled(false);
remInv.setEnabled(false);
addItem.setEnabled(false);
remItem.setEnabled(false);
//window listener to shut program down
this.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
try
{
if (conn != null) { conn.close();}
if (stmt != null) { stmt.close();}
}
catch (Exception f)
{
System.out.println("Error: " +
f.getMessage());
}
finally
{
System.exit(0);
}
}
});
}
catch (OutOfMemoryError oome)
{
System.out.println("OOME :" + oome);
}
displayProductList();
displayInvoiceList();
//displayCustomerList();
setVisible(true);
creProd.requestFocus(); //******************Line 327
}
public String NoClassDefFoundError(String s)
{
return(s);
}
snip bunch of code..... (to explain the "Product" and "Invoice"
query messages..............
public void displayProductList()
{
try
{
products.clear();
queryProducts = stmt.executeQuery("SELECT * FROM products ORDER BY model_number;");
while(queryProducts.next())
{
int modelNumber = queryProducts.getInt ("model_number");
String description = queryProducts.getString ("part_description");
float price = queryProducts.getFloat("price");
Product p = new Product(modelNumber, description, price);
products.addElement(p);
if (products.size() > 0);
{
miEditProd.setEnabled(true);
miRemProd.setEnabled(true);
editProd.setEnabled(true);
remProd.setEnabled(true);
miCreInv.setEnabled(true);
creInv.setEnabled(true);
}
}
}
catch (Exception e)
{
System.out.println("Products Query Error: " + e);
}
}
public void displayInvoiceList()
{
try
{
invoices.clear();
queryInvoices = stmt.executeQuery("SELECT * FROM invoices;");
while(queryInvoices.next())
{
int invoiceNumber = queryInvoices.getInt ("invoice_number");
String customerName = queryInvoices.getString ("customer_name");
//int customerNumber = queryInvoices.getInt ("customer_id");
Invoice i = new Invoice(invoiceNumber, customerName);
invoices.addElement(i);
if (invoices.size() >= 1)
{
miEditInv.setEnabled(true);
miRemInv.setEnabled(true);
editInv.setEnabled(true);
remInv.setEnabled(true);
addItem.setEnabled(true);
}
if (lineItems.size() >= 1)
{
remItem.setEnabled(true);
}
}
}
catch (Exception e)
{
System.out.println("Invoice Query Error: " + e);
}
}
*****************************
my console now says:
finally
OOME :java.lang.OutOfMemoryError
Products Query Error: java.lang.NullPointerException Invoice Query Error: java.lang.NullPointerException Exception in thread "main" java.lang.NullPointerException
at Distribution.<init>(Distribution.java:327)
at Distribution.main(Distribution.java:17)
I'm begging for help! I've spent the past 2 days modifying and remodifying and still don't know what the problem is. Any assistance or suggestions would be gratefully received!
Lori
 
Lori Battey
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thought I would follow up on this for those who may have read it and not had any suggestions. I took my program to a C++ programmer friend of mine who knows Java well enough. He spent about 4 hours going through and testing all my code and then removed all traces of Visual Cafe in the boot sequence on my computer and compiled right from the 1.2.2 SDK and it ran beautifully...the problem was in Visual Cafe, not my code!
Beware! This was a horrendous time waster for me!
Lori
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!