Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

problem in sql syntax (method calling)

 
sarath bon
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello,
I am using mysql database for account validation, for this i wrote swing program which works good.
I can also able to recognise driver and can connect to the database. The problem is, if i use a method loadAccounts

i called above method in a actioncommand

if i run the progarm i am getting exceptions..
java.lang.NullPointerException
at MyField.loadAccounts(MyField.java:483)
at MyField.actionPerformed(MyField.java:581)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1764)
at javax.swing.AbstractButton$ForwardActionEvents.actionPerformed(AbstractButton.java:1817)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:419)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:257)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:245)
at java.awt.Component.processMouseEvent(Component.java:5134)
at java.awt.Component.processEvent(Component.java:4931)
at java.awt.Container.processEvent(Container.java:1566)
at java.awt.Component.dispatchEventImpl(Component.java:3639)
at java.awt.Container.dispatchEventImpl(Container.java:1623)
at java.awt.Component.dispatchEvent(Component.java:3480)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:3450)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3165)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3095)
at java.awt.Container.dispatchEventImpl(Container.java:1609)
at java.awt.Window.dispatchEventImpl(Window.java:1590)
at java.awt.Component.dispatchEvent(Component.java:3480)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:450)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:197)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:144)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:136)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:99)

I would think this is not the right way to call loadAccounts();
can i get a right solution from anybody..
any idea or suggestions,
thankyou..
am
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34410
346
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are missing the single quotes around the username. It should be:
rs = statement.executeQuery("select acc_password from acc where username='"+Uname.getText() + "'");

You can use prepared statements to avoid having to worry about the quotes.
However, that wouldn't cause a null pointer exception. It would cause a sql exception. Can you show the code where you create the statement object. This is likely to be the culprit.
 
sarath bon
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thankyou for the comment,
i worked with rs = statement.executeQuery("select acc_password from acc where username='"+Uname.getText() + "'"); which shows the same excepions as you said.
I have created statement in the method which connects to database..
<code>
public void connectToDB() {
try {
connection = DriverManager.getConnection("jdbc:mysql://localhost/accounts");
System.out.println("database connected");
statement = connection.createStatement();
statement.setMaxRows(5);
statement.setFetchSize(2);
} catch(SQLException connectException) {
System.out.println(connectException.getMessage());
System.out.println(connectException.getSQLState());
System.out.println(connectException.getErrorCode());
System.exit(1);
}
}
</code>

thankyou once more..
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64838
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch sarbon!
You'll find this forum a great place to seek help on JDBC, and there aren't many rules you'll have to worry about, but one is that proper names are required. Please take a look at the JavaRanch Naming Policy and change your display name to match it.
In particular, your display name must be a first and a last name separated by a space character, and must not be obviously fictitious.
Thanks!
bear
 
sarath bon
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, I've stick to the rules and changed the display name.Any help on my previous post??
Regards
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34410
346
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I assume you are calling connectToDB() somewhere? Looking quickly, your code seems fine. Try putting System.out.println() statements in to find out exactly which line is throwing the null pointer.
 
Jason Steele
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, just a thought, use a PreparedStatment instead, it's safer.

One more thing...since this is a null pointer exception, I believe this is your culprit:
if (event.getActionCommand().equals(submit))
it is looking for a submit object. submit needs to be in quotes.
if (event.getActionCommand().equals("submit)")
 
Jason Steele
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I meant:
if (event.getActionCommand().equals("submit"))

!!!
 
sarath bon
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
When calling my loadAccounts method i am getting null exceptions

please help me..
thankyou..
 
sarath bon
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mr.Jason Steele
I will check for "submit", but instead of calling loadAccounts(); if i write someother code i am not getting exceptions, submit is a actionlistener name for button..
but i will see your idea..
thankyou for the post..
 
sarath bon
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I cannot use
if (event.getActionCommand().equals("submit"));
with "submit"..
 
Jason Steele
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
getActionCommand() is looking for a String. So, if your button text is 'submit', then it will be ok.
When you have
if(event.getActionCommand().equals(submit))
Pseudo says:
if(the button's text is equal to "submit") then loadAccounts().
So if your button displays "submit" as its text, then it should be:
if(event.getActionCommand().equals("submit"))
if your button displays "Load Accounts" as its text, then is should be:
if(event.getActionCommand().equals("Load Accounts"))
Also, trap to see if your actually making it inside the if block like:

if you get the message then you know your ok and it is probably something in your loadAccouts() method.
I hope I have helped you.
Jason Steele
 
Bosun Bello
Ranch Hand
Posts: 1511
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can this call 'Uname.getText' return null?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic