Welcome to Dream.In.Code
Become a Java Expert!

Join 149,611 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,856 people online right now. Registration is fast and FREE... Join Now!




Java + SQL problem

 
Reply to this topicStart new topic

Java + SQL problem

nastassja_hong
17 Sep, 2007 - 11:09 AM
Post #1

New D.I.C Head
*

Joined: 13 Sep, 2007
Posts: 2


My Contributions
hello..

this is the situation.. i have a method in a class which is called from the main. i have no problem called the method from the main.. however the problem lies in the line that i will show below.. i'm pasting the whole method so that u have a complete understand of it..

CODE

public void retrieveData(){
        System.out.println("test retrivedata method");
        
            ResultSet rs= null;
            ResultSets=new ArrayList<String[]>();
                
         try{
             rs=stmt.executeQuery("Select * from weightedwordlist");  //-->error appears at this line
         }
         catch(SQLException e){}
        
        System.out.println("test execute query");

        
       try{
         while(rs.next()){
              
               String[] row={
                 rs.getString("Word"),rs.getString("Weight")
                 };
             ResultSets.add(row);
      
          }  
       }
       catch(Exception e){
           System.out.println("Exception in AptmntTableModel");
             }    
    
    System.out.println("Data from db" + ResultSets);
    
     }


when testing the output, it displays the first line "test retrivedata method" but then the error appears.. as pasted below:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at CalculateWWLWeight.retrieveData(CalculateWWLWeight.java:63)
at SpamFilter.createSetComparison(SpamFilter.java:343)
at SpamFilter.jButtonFilterActionPerformed(SpamFilter.java:202)
at SpamFilter.access$100(SpamFilter.java:19)
at SpamFilter$2.actionPerformed(SpamFilter.java:95)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
at java.awt.Component.processMouseEvent(Component.java:6038)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3260)
at java.awt.Component.processEvent(Component.java:5803)
at java.awt.Container.processEvent(Container.java:2058)
at java.awt.Component.dispatchEventImpl(Component.java:4410)
at java.awt.Container.dispatchEventImpl(Container.java:2116)
at java.awt.Component.dispatchEvent(Component.java:4240)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4322)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3986)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3916)
at java.awt.Container.dispatchEventImpl(Container.java:2102)
at java.awt.Window.dispatchEventImpl(Window.java:2429)
at java.awt.Component.dispatchEvent(Component.java:4240)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:273)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:183)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:173)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:160)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:121)


Pls can someone help me?? thank u
User is offlineProfile CardPM
+Quote Post

alpha02
RE: Java + SQL Problem
17 Sep, 2007 - 12:07 PM
Post #2

D.I.C Addict
Group Icon

Joined: 20 May, 2006
Posts: 687


Dream Kudos: 850
My Contributions
I assume stmt is a Statement object, created by Connection.createStatement(). Are you sure you initialized your Statement object, using stmt = conn.createStatement() (conn is a Connection object)? If not, this is the cause of a NullPointerException. This is one of the most frequently encountered Exceptions. If you want to know more about SQL with Java, have a look at this:

http://www.dreamincode.net/forums/showtopic32360.htm

This is a tutorial I wrote about using MySQL with Java, but it can be used with other systems (SQLServer, Oracle..). Hope this helps, if you have any question just reply and I'll help you!
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/8/09 12:22AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month