Thread: Null Pointer... Can't find it.

Results 1 to 5 of 5
  1. #1 Null Pointer... Can't find it. 
    Registered Member
    CTucker's Avatar
    Join Date
    Oct 2008
    Posts
    2,422
    Thanks given
    263
    Thanks received
    281
    Rep Power
    343
    So, I'm writing a small program, to insert things into a database for my new minigame... But im having some problems... Very short code.. here we go


    Code:
    public static boolean addCards(Cards c) {
    		try {
    			query("INSERT INTO 'cards' VALUES (" + c.ID + 
    					"," + c.TYPE + 
    					","
    					+ c.NAME + 
    					"," + c.getBooleanText(c.isMonster()) + ","
    					+ c.ATT + "," +
    							"" + c.DEF + ");");
    							
    		} catch (Exception e) {
    			e.printStackTrace();
    			return false;
    		}
    		return true;
    	}
    The error is showing up on c.ID


    here is the other part.


    Code:
    public static String ID = "", TYPE = "", NAME = "",MONSTER = "", ATT = "", DEF = "", MORE = "";
    	
    	public static void main(String ags[]) {
    		Cards c = new Cards();
    		Scanner s = new Scanner(System.in);
    		print("What is the Card ID: ");
    		ID = s.nextLine();
    		print("What is the Card Type: ");
    		TYPE = s.nextLine();
    		print("What is the Card Name: ");
    		NAME = s.nextLine();
    		print("Is this a monster: ");
    		MONSTER = s.nextLine();
    		print("How many AP: ");
    		ATT = s.nextLine();
    		print("How many DP: ");
    		DEF = s.nextLine();
    		Main.addCards(c);
    	}

    What am i doing wrong?

    Please note, my SQL is in Main.java
    Being a lazy person, and not changing the names.


    The Massacred conventions were so i could see which part of the code it was erroring out on.
    Reply With Quote  
     

  2. #2  
    Meh. I can't code.

    Soulevoker's Avatar
    Join Date
    Sep 2007
    Age
    29
    Posts
    1,198
    Thanks given
    5
    Thanks received
    6
    Rep Power
    137
    Okay this is how you setup MySQL in java:

    Updating records example:
    Code:
     public static void savePunishments(Player p) {
            try {
                PreparedStatement s = QueryManager.prepareQuery(
                        "UPDATE p_punishments " +
                                "SET username=?," +
                                "muted=?," +
                                "mutelength=?," +
                                "arrested=?," +
                                "jailtime=?\n" +
                                "WHERE username=\"" + p.username + "\"");
                s.setString(1, p.username);
                s.setInt(2, p.muteType);
                s.setInt(3, p.muteTimer);
                s.setInt(4, p.arrested);
                s.setInt(5, p.jailTime);
                s.execute();
            } catch (SQLException ex) {
                QueryManager.queryError(ex);
            }
        }
    Insert new records:

    Code:
    public static void savePunishments(Player p) {
            try {
                PreparedStatement s = QueryManager.prepareQuery(
                        "INSERT INTO p_punishments" +
                                "(username,muted,mutelength,arrested,jailtime) " +
                                "VALUES(?,?,?,?,?)");
                s.setString(1, p.username);
                s.setInt(2, p.muteType);
                s.setInt(3, p.muteTimer);
                s.setInt(4, p.arrested);
                s.setInt(5, p.jailTime);
                s.execute();
            } catch (SQLException ex) {
                QueryManager.queryError(ex);
            }
        }
    And finally loading:
    Code:
        public void loadPunishments(Player p) {
            try {
                ResultSet set = loadTable("p_punishments", p);
                if (set.first()) {
                    p.muteType = set.getInt("muted");
                    p.muteTimer = set.getInt("mutelength");
                    p.arrested = set.getInt("arrested");
                    p.jailTime = set.getInt("jailtime");
                }
            } catch (SQLException ex) {
                QueryManager.queryError(ex);
            }
        }

    Reply With Quote  
     

  3. #3  
    Registered Member
    CTucker's Avatar
    Join Date
    Oct 2008
    Posts
    2,422
    Thanks given
    263
    Thanks received
    281
    Rep Power
    343
    Thanks, Thanked aswell.
    But, I need a querymanager?

    Also, neat trick using the "?", didn't know you could do that
    Reply With Quote  
     

  4. #4  
    Registered Member
    CTucker's Avatar
    Join Date
    Oct 2008
    Posts
    2,422
    Thanks given
    263
    Thanks received
    281
    Rep Power
    343
    Bump, Used his method, still getting the null.
    Reply With Quote  
     

  5. #5  
    Renown Programmer

    Join Date
    Dec 2010
    Posts
    2,876
    Thanks given
    508
    Thanks received
    1,898
    Rep Power
    5000
    derp, the fields shouldn't be static; you should be setting cards.FIELD_NAME not FIELD_NAME
    never talk to me or my wife's son ever again
    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. Null Pointer... Can't find it.
    By CTucker in forum Help
    Replies: 0
    Last Post: 03-12-2012, 04:38 AM
  2. Null Pointer
    By Jaegon in forum Help
    Replies: 6
    Last Post: 10-09-2011, 10:05 PM
  3. Null pointer
    By Bruno in forum Help
    Replies: 19
    Last Post: 08-09-2010, 01:30 AM
  4. Null pointer
    By Warlock 999 in forum Help
    Replies: 4
    Last Post: 11-02-2009, 01:09 AM
  5. Null pointer
    By Bruno in forum Help
    Replies: 3
    Last Post: 07-09-2009, 04:39 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
  •