Thread: [REP + THANKS]JDBC - Null pointer Exception

Results 1 to 7 of 7
  1. #1 [REP + THANKS]JDBC - Null pointer Exception 
    Banned
    Join Date
    Apr 2011
    Posts
    215
    Thanks given
    18
    Thanks received
    7
    Rep Power
    0
    Hey,
    I am adding grand exchange to my server and everything works fine except one method:
    Code:
    public static void getCollect(final Client c, final String Username){
    			try {
    				String query = "SELECT * FROM collections";
    				ResultSet rs = query(query);
    				while(rs.next()){
    					int id = Integer.parseInt(rs.getString("itemid"));
    					int amt = Integer.parseInt(rs.getString("amount"));
    					String name = rs.getString("username");
    					if(name.equalsIgnoreCase(Username)){
    						c.getItems().addItem(id, amt);
    						c.sendMessage("Successfully collected GE Items.");
    						query("DELETE FROM `collections` WHERE username = '"+ Username + "' AND `itemid` = '"+id+"' AND `amount` = '"+amt+"'");
    						}else{
    						c.sendMessage("@dre@You have nothing to collect!");
    					}
    				}
    				
    			} catch (Exception e) {
    				e.printStackTrace();
    				con = null;
    				stm = null;
    			}
    		}
    Whenever I type ::collectge (it calls the getCollect Method) I get a null pointer. Can some one please tell me what I am doing wrong. (This is my first time using JDBC).

    Error:
    Code:
    [12/3/11 12:20 PM]: java.lang.NullPointerException
    [12/3/11 12:20 PM]:     at server.model.players.GrandExchange.getCollect(GrandEx
    change.java:612)
    P.S Line 612 is: Resultset rs = query(query)

    EDIT: MY QUERY METHOD:

    Code:
    public static ResultSet query(String s) throws SQLException {
    			try {
    				if (s.toLowerCase().startsWith("select")) {
    					ResultSet rs = stm.executeQuery(s);
    					return rs;
    				} else {
    					stm.executeUpdate(s);
    				}
    				return null;
    			} catch (Exception e) {
    				e.printStackTrace();
    				con = null;
    				stm = null;
    				
    			}
    			return null;
    		}
    Thanks,
    Saad
    Reply With Quote  
     

  2. #2  
    Respected Member


    kLeptO's Avatar
    Join Date
    Dec 2006
    Age
    28
    Posts
    2,955
    Thanks given
    1,183
    Thanks received
    754
    Rep Power
    3084
    Code:
    public static void getCollect(final Client c, final String Username){
    			try {
    				String query = "SELECT * FROM collections";
    				ResultSet rs = query(query);
    				if (rs == null) 
    					return;
    				while(rs.next()){
    					if (rs == null) 
    						break;
    					int id = Integer.parseInt(rs.getString("itemid"));
    					int amt = Integer.parseInt(rs.getString("amount"));
    					String name = rs.getString("username");
    					if(name.equalsIgnoreCase(Username)){
    						c.getItems().addItem(id, amt);
    						c.sendMessage("Successfully collected GE Items.");
    						query("DELETE FROM `collections` WHERE username = '"+ Username + "' AND `itemid` = '"+id+"' AND `amount` = '"+amt+"'");
    						}else{
    						c.sendMessage("@dre@You have nothing to collect!");
    					}
    				}
    				
    			} catch (Exception e) {
    				e.printStackTrace();
    				con = null;
    				stm = null;
    			}
    		}
    Reply With Quote  
     

  3. #3  
    Banned
    Join Date
    Apr 2011
    Posts
    215
    Thanks given
    18
    Thanks received
    7
    Rep Power
    0
    Quote Originally Posted by kLeptO View Post
    Code:
    public static void getCollect(final Client c, final String Username){
    			try {
    				String query = "SELECT * FROM collections";
    				ResultSet rs = query(query);
    				if (rs == null) 
    					return;
    				while(rs.next()){
    					if (rs == null) 
    						break;
    					int id = Integer.parseInt(rs.getString("itemid"));
    					int amt = Integer.parseInt(rs.getString("amount"));
    					String name = rs.getString("username");
    					if(name.equalsIgnoreCase(Username)){
    						c.getItems().addItem(id, amt);
    						c.sendMessage("Successfully collected GE Items.");
    						query("DELETE FROM `collections` WHERE username = '"+ Username + "' AND `itemid` = '"+id+"' AND `amount` = '"+amt+"'");
    						}else{
    						c.sendMessage("@dre@You have nothing to collect!");
    					}
    				}
    				
    			} catch (Exception e) {
    				e.printStackTrace();
    				con = null;
    				stm = null;
    			}
    		}

    I get the same error....
    Reply With Quote  
     

  4. #4  
    Registered Member
    Join Date
    Sep 2010
    Posts
    448
    Thanks given
    42
    Thanks received
    23
    Rep Power
    4
    maybe its because u send the String s with caps, but the query tries checks if it starts without caps

    Change ur String s with this one:
    Code:
    String query = "select * FROM collections";
    Reply With Quote  
     

  5. #5  
    Banned
    Join Date
    Apr 2011
    Posts
    215
    Thanks given
    18
    Thanks received
    7
    Rep Power
    0
    Quote Originally Posted by zivkovic View Post
    maybe its because u send the String s with caps, but the query tries checks if it starts without caps

    Change ur String s with this one:
    Code:
    String query = "select * FROM collections";
    My selltoge method also has something similiar, and the Select's s is capital and it works...
    Reply With Quote  
     

  6. #6  
    Respected Member


    Join Date
    Jan 2009
    Posts
    5,743
    Thanks given
    1,162
    Thanks received
    3,603
    Rep Power
    5000
    fixed on teamviewer.

    Word of advice. You do not need to keep opening and closing a connection per query and you should use close then null not just null.
    Reply With Quote  
     

  7. Thankful user:


  8. #7  
    Banned
    Join Date
    Apr 2011
    Posts
    215
    Thanks given
    18
    Thanks received
    7
    Rep Power
    0
    Quote Originally Posted by 1776 View Post
    fixed on teamviewer.
    Thank you soo much
    Reply With Quote  
     


Thread Information
Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)


User Tag List

Similar Threads

  1. [PI]Null pointer exception
    By Pyro Sauce in forum Help
    Replies: 5
    Last Post: 01-03-2011, 12:47 AM
  2. Null Pointer exception [508]
    By popdudej2 in forum Help
    Replies: 8
    Last Post: 11-01-2010, 07:55 PM
  3. Null pointer exception.
    By hybrent in forum Help
    Replies: 0
    Last Post: 10-19-2009, 01:41 AM
  4. null pointer exception please help
    By apur3slife in forum Help
    Replies: 0
    Last Post: 07-16-2009, 03:34 PM
Posting Permissions
  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •