AWT-EventQueue-0

Hi, very new to java, in need of some help. Started trying to simply add some information to a table in a Access database. Code compiles but when i actually enter information in the text field i created and press the 'write' to databse button i get this error:

- java -

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException

at DBDemo2.actionPerformed(DBDemo2.java:80)

at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1849)

at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2169)

at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:420)

at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:258)

at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:234)

at java.awt.Component.processMouseEvent(Component.java:5488)

at javax.swing.JComponent.processMouseEvent(JComponent.java:3126)

at java.awt.Component.processEvent(Component.java:5253)

at java.awt.Container.processEvent(Container.java:1966)

at java.awt.Component.dispatchEventImpl(Component.java:3955)

at java.awt.Container.dispatchEventImpl(Container.java:2024)

at java.awt.Component.dispatchEvent(Component.java:3803)

at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4212)

at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3892)

at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3822)

at java.awt.Container.dispatchEventImpl(Container.java:2010)

at java.awt.Window.dispatchEventImpl(Window.java:1774)

at java.awt.Component.dispatchEvent(Component.java:3803)

at java.awt.EventQueue.dispatchEvent(EventQueue.java:463)

at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:242)

at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:163)

at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:157)

at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:149)

at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)

Output completed (43 sec consumed) - Normal Termination

can anyone help. Here is my code:

import java.awt.*;

import javax.swing.*;

import java.awt.event.*;

import java.sql.*;

public class DBDemo2 extends JFrame implements ActionListener

{

JTextField IPTxt = new JTextField(10);

JButton writeBtn = new JButton("Write");

JButton displayBtn = new JButton("Display");

JLabel IPLbl= new JLabel("IP Address");

Connection UserDB;

Statement myStatement;

public static void main(String[] args) { new DBDemo2(); }

public DBDemo2()

{

setLayout(new BorderLayout());

setSize(300, 300);

setTitle("Database Demo");

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JPanel middle = new JPanel();

middle.setLayout(new FlowLayout());

middle.add(IPLbl);

middle.add(IPTxt);

add("West", middle);

setVisible(true);

JPanel bottom = new JPanel();

bottom.setLayout(new FlowLayout());

bottom.add(writeBtn);

bottom.add(displayBtn);

writeBtn.addActionListener(this);

displayBtn.addActionListener(this);

add("South", bottom);

setVisible(true);

try{

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

String sourceURL =

"jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=UserDB.mdb;";

Connection userDB =

DriverManager.getConnection(sourceURL, "admin", "");

Statement myStatement = userDB.createStatement();

}

catch (ClassNotFoundException cnfe){

System.out.println(cnfe);

}

catch (SQLException sqle) {

System.out.println(sqle);

}

}

public void actionPerformed(ActionEvent e) {

if (e.getSource() == writeBtn)

{

String i = IPTxt.getText();

if (i.equals(""))

{

JOptionPane.showMessageDialog(this, "One or more fields are blank idiot");

return;

}

String writeString =

"INSERT INTO Users(IP) VALUES('" + i + "')";

try

{

myStatement.executeUpdate(writeString);

IPTxt.setText("");

}

catch (SQLException sqle) {

JOptionPane.showMessageDialog(this, "Duplicate key " + i);

}

}

if (e.getSource() ==displayBtn) {

try

{

ResultSet results = myStatement.executeQuery

("SELECT IP, FROM Users ORDER BY IP");

while (results.next()) {

System.out.print(results.getString(1));

}

results.close();

}

catch (SQLException sqle) {

System.out.println(sqle);

}

}

}

}

[4840 byte] By [Mr_Mojo_Risina] at [2007-11-27 4:31:18]
# 1
Can you put code tags around posted code in future, makes it readable (ish), What's line 80 of your code?
georgemca at 2007-7-12 9:40:44 > top of Java-index,Java Essentials,New To Java...
# 2

There is alot of bad design going on in this code.

Your specific problem is that you have class variables and method variables with the same names, specifically your Connection and Statement variables and so while you are initializing the method versions of those you are not initializing the class versions of them and hence they are null when you try and use them in your actionPeformed method.

If you didn't follow that. myStatement is null and you should look to see in your constructor why that is.

cotton.ma at 2007-7-12 9:40:44 > top of Java-index,Java Essentials,New To Java...
# 3
> Can you put code tags around posted code in future,> makes it readable (ish), What's line 80 of your code?myStatement.executeUpdate(writeString);
cotton.ma at 2007-7-12 9:40:44 > top of Java-index,Java Essentials,New To Java...
# 4
Yes that is line 80 Yes i will keep that in mind. Jus to mention again, very new.
Mr_Mojo_Risina at 2007-7-12 9:40:44 > top of Java-index,Java Essentials,New To Java...
# 5
You are not asigning anything to your instance variable myStatement in the constructor but to a local variable. Change lie 51 fromStatement myStatement = userDB.createStatement();tomyStatement = userDB.createStatement();
akimotoa at 2007-7-12 9:40:44 > top of Java-index,Java Essentials,New To Java...
# 6
Thanks for the help. I know you explained it but could you jus explain for someone new to java exactly why that worked. Cheers
Mr_Mojo_Risina at 2007-7-12 9:40:44 > top of Java-index,Java Essentials,New To Java...
# 7

> Thanks for the help. I know you explained it but

> could you jus explain for someone new to java exactly

> why that worked. Cheers

Because when you do this

Classname variablename

As in

Statement myStatement

you are declaring a new variable.

So in your constructor you were making a NEW, method local variable named myStatement, but this is not the same as the class variable named myStatement.

For more information on this subject consult http://java.sun.com/docs/books/tutorial/java/javaOO/variables.html

cotton.ma at 2007-7-12 9:40:44 > top of Java-index,Java Essentials,New To Java...