Java Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingJava Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old August 14th, 2009, 01:32 PM
saromadian saromadian is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Aug 2009
Posts: 1 saromadian User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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(); 	 	} }

Reply With Quote
  #2  
Old August 14th, 2009, 04:03 PM
paulscode's Avatar
paulscode paulscode is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2008
Location: Fort Meade, MD
Posts: 77 paulscode User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 11 h 45 m 3 sec
Reputation Power: 2
I am having a little trouble navigating through the haphazardly formated code. Which line is #107, and which varriable is null at that line?

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingJava Development > Null Pointer exception


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump




 Free IT White Papers!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

Request Your Free Technology Downloads!
 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

Request Your Free Technology Downloads!
 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

Request Your Free Technology Downloads!
 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

Request Your Free Technology Downloads!
 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

Request Your Free Technology Downloads!
 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 




© 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
For more Enterprise Application Development news, visit eWeek