
August 14th, 2009, 01:32 PM
|
|
Registered User
|
|
Join Date: Aug 2009
Posts: 1
Time spent in forums: 32 m 52 sec
Reputation Power: 0
|
|
|
Null Pointer exception
I'm writing a program that is like a sample inventory system for a toy Store. It is supposed to connect to a database and create read update and delete. I've written most of the code and i've gotten to the point where i've trying to add items to the database field but i keep getting the same null pointer exception and i'm really not sure why, i've tried many different things and i have asked some other people and have yet to find an answer. any help would be greatly appreciated.
This is the error i'm getting
Quote: | Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at ToysR.actionPerformed(ToysR.java:107) at javax.swing.AbstractButton.fireActionPerformed(Abs tractButton.java:19 95) at javax.swing.AbstractButton$Handler.actionPerformed (AbstractButton.jav a:2318) at javax.swing.DefaultButtonModel.fireActionPerformed (DefaultButtonModel .java:387) at javax.swing.DefaultButtonModel.setPressed(DefaultB uttonModel.java:242 ) at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(BasicButtonL istener.java:236) at java.awt.Component.processMouseEvent(Component.jav a:6263) at javax.swing.JComponent.processMouseEvent(JComponen t.java:3267) at java.awt.Component.processEvent(Component.java:602 8) at java.awt.Container.processEvent(Container.java:204 1) at java.awt.Component.dispatchEventImpl(Component.jav a:4630) at java.awt.Container.dispatchEventImpl(Container.jav a:2099) at java.awt.Component.dispatchEvent(Component.java:44 60) at java.awt.LightweightDispatcher.retargetMouseEvent( Container.java:4574 ) at java.awt.LightweightDispatcher.processMouseEvent(C ontainer.java:4238) at java.awt.LightweightDispatcher.dispatchEvent(Conta iner.java:4168) at java.awt.Container.dispatchEventImpl(Container.jav a:2085) at java.awt.Window.dispatchEventImpl(Window.java:2475 ) at java.awt.Component.dispatchEvent(Component.java:44 60) at java.awt.EventQueue.dispatchEvent(EventQueue.java: 599) at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThre ad.java:269) at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread. java:184) at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThre ad.java:174) at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:169) at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:161) at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:122) Press any key to continue . . . |
AND this is my code:
Code:
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;
class ToysR extends JFrame implements ActionListener { private Connection connection;
private Statement statement;
private ResultSet rs;
JTextField AddName = new JTextField(20);
JTextField AddPrice = new JTextField(20);
JTextField AddQuantity = new JTextField(20);
JTextField SearchField = new JTextField(20);
JLabel page1lbl = new JLabel("Toy Store inventory System"); JLabel searchtoplbl = new JLabel("Search For Items"); JLabel searchtitle = new JLabel("Type in what you would like to search for");
JLabel addtitle = new JLabel("This is where you can add items to your inventory");
JLabel MainMenu = new JLabel("1. Search Items in Inventory 2. Add Items to Inventory 3. List all items in inventory"); JLabel addName = new JLabel("Add Name");
JLabel addPrice = new JLabel("Add Price");
JLabel addQuantity = new JLabel("Add Quantity");
JButton gotoSearchItem = new JButton("Search Items"); JButton gotoAddItem = new JButton("Add Items");
JButton gotoListAll = new JButton("ListAll");
JButton AddItem = new JButton("Add");
JButton ListItem = new JButton("List");
JButton FindItem = new JButton("Find");
JButton gotoMain = new JButton("Return to Main");
JButton gotoMain2 = new JButton("Return to Main");
JPanel MainTop = new JPanel(new GridLayout(5, 1));
JPanel Main2nd = new JPanel(new GridLayout(3,1));
JPanel Main3rd = new JPanel(new GridLayout(1,3));
JPanel Main4th = new JPanel(new GridLayout(1,1));
JPanel SearchTitle = new JPanel(new GridLayout(1,1)); JPanel SearchTop = new JPanel(new GridLayout(1,1)); JPanel Searchmiddle = new JPanel(new GridLayout(1,1)); JPanel Searchbottom = new JPanel(new GridLayout(1,1)); JPanel addTitle = new JPanel(new GridLayout(1,1));
JPanel addTop = new JPanel(new GridLayout(2,3));
JPanel addMiddle = new JPanel(new GridLayout(1,1)); JPanel addBottom = new JPanel(new GridLayout(1,1));
JList listeverything = new JList();
private ImageIcon toysIcon = new ImageIcon("toysrus-logo-high.jpg");
ToysR ToysR2;
JFrame searchFrame = new JFrame();
JFrame mainFrame = new JFrame();
JFrame addFrame = new JFrame();
JFrame listAllFrame = new JFrame();
private void loaditems() {
Vector v = new Vector();
try { rs = statement.executeQuery("SELECT * FROM items");
while (rs.next()) { v.addElement(rs.getString("name")); } }
catch (SQLException e) { //displaySQLErrors(e); } listeverything.setListData(v); }
public void actionPerformed(ActionEvent evt) // function to control the ActionListener { Object src = evt.getSource();
if(src == gotoSearchItem) { mainFrame.setVisible(false); searchFrame.setVisible(true); searchFrame.show(); }
if (src == gotoMain || src == gotoMain2) { mainFrame.setVisible(true); searchFrame.setVisible(false); addFrame.setVisible(false);
mainFrame.show(); }
if (src == gotoAddItem) { mainFrame.setVisible(false); addFrame.setVisible(true); addFrame.show(); }
if (src == gotoListAll) { mainFrame.setVisible(false); listAllFrame.setVisible(true); listAllFrame.show();
try { Statement statement = connection.createStatement(); rs = statement.executeQuery("SELECT * FROM student"); }
catch (SQLException insertException) { System.out.println(insertException); } System.out.println(rs); }
if (src == AddItem) {
this.AddPrice = new JTextField(20);
try { Statement statement = connection.createStatement();
int i = statement.executeUpdate("INSERT INTO items (name, price, quantity)" + "VALUES(" +"'"+ AddName.getText() + "', " +"'"+ AddPrice.getText() + "', " +"'"+ AddQuantity.getText() + "')"); System.out.print("\nInserted " + i + " rows successfully");
listeverything.removeAll();
loaditems(); }
catch (SQLException insertException) { System.out.println(insertException); } } }
public void ConnectToTheDatabase() {
try { connection = DriverManager .getConnection("jdbc:mysql://localhost/toysr","",""); statement = connection.createStatement(); } catch (SQLException connectException) { System.out.println(connectException.getMessage()); System.out.println(connectException.getSQLState()) ; System.out.println(connectException.getErrorCode() ); System.exit(1); } }
ToysR() {
try { Class.forName("com.mysql.jdbc.Driver").newInstance(); } catch (Exception e) { System.err.println("Unable to find and load driver");
System.exit(1); }
mainFrame.setLayout(new FlowLayout()); mainFrame.setSize(550,500); mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_ CLOSE); gotoSearchItem.addActionListener(this); MainTop.add(page1lbl);
Main2nd.add(MainMenu);
Main3rd.add(gotoSearchItem);
Main3rd.add(gotoAddItem);
Main3rd.add(gotoListAll);
Main4th.add(new JLabel(toysIcon)); mainFrame.add(MainTop);
mainFrame.add(Main2nd);
mainFrame.add(Main3rd);
mainFrame.add(Main4th);
mainFrame.setVisible(true);
searchFrame.setSize(350,250);
searchFrame.setLayout(new FlowLayout()); searchFrame.setDefaultCloseOperation(JFrame.EXIT_O N_CLOSE); gotoMain.addActionListener(this); gotoMain2.addActionListener(this);
SearchTitle.add(searchtitle);
SearchTop.add(searchtoplbl); Searchmiddle.add(SearchField); Searchbottom.add(FindItem); Searchbottom.add(gotoMain); searchFrame.add(SearchTitle); searchFrame.add(SearchTop); searchFrame.add(Searchmiddle); searchFrame.add(Searchbottom);
addFrame.setVisible(false);
addFrame.setSize(800,200);
addFrame.setLayout(new FlowLayout()); addFrame.setDefaultCloseOperation(JFrame.EXIT_ON_C LOSE); gotoAddItem.addActionListener(this); addTitle.add(addtitle);
addTop.add(addName);
addTop.add(addPrice);
addTop.add(addQuantity);
addTop.add(AddName);
addTop.add(AddPrice);
addTop.add(AddQuantity);
addMiddle.add(AddItem);
addBottom.add(gotoMain2);
addFrame.add(addTitle);
addFrame.add(addTop);
addFrame.add(addMiddle);
addFrame.add(addBottom);
AddItem.addActionListener(this);
listAllFrame.setVisible(false);
listAllFrame.setSize(400,300);
listAllFrame.setLayout(new FlowLayout()); listAllFrame.setDefaultCloseOperation(JFrame.EXIT_ ON_CLOSE); gotoListAll.addActionListener(this); }
public static void main(String[] args)
{ ToysR x1 = new ToysR(); x1.mainFrame.show(); } }
|