Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Page 1 of 2 12 LastLast
Results 1 to 25 of 34

Thread: Small problem, help please?

  1. #1
    Junior Member
    Join Date
    Aug 2011
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Small problem, help please?

    java.lang.NullPointerException
    	at server.util.MysqlManager.checkVote(MysqlManager.java:43)
    	at server.model.players.Client.initialize(Client.java:81)
    	at server.model.players.PlayerHandler.process(PlayerHandler.java:186)
    	at server.Server.main(Server.java:176)


    And below is section of code that it states.

    Size 5 font = LINE 43

    		public static void checkVote(Client c) {
    		try {
    			[SIZE=5]Statement stmt = conn.createStatement();[/SIZE]
    			ResultSet rs = stmt
    					.executeQuery("SELECT COUNT(playerName) AS total FROM `votes` WHERE `playerName`= \""
    							+ c.playerName + "\" AND `recieved`= 0");
    			rs.first();
    			int total = rs.getInt("total");
    			if (total == 1) {
    				stmt.execute("UPDATE `votes` SET `recieved` = 1 WHERE `playerName` = '"
    						+ c.playerName + "'");
    				c.getItems().addItem(995, 10000000);
    				c.sendMessage("Thanks for voting! Please do so again in 24 hours!");
    			}
    		} catch (SQLException ex) {
    			System.out.println(ex);
    		}
    	}


    Client.java - Line 81

    private CombatAssistant combatAssistant = new CombatAssistant(this);


    Size 5 Font = LINE 186

    						Client o1 = (Client) Server.playerHandler.players[i];
    						if(PlayerSave.saveGame(o1)){ 
    							System.out.println("Game saved for player "+players[i].playerName); 
    						} else { 
    							System.out.println("Could not save for "+players[i].playerName); 
    						}
    						removePlayer(players[i]);
    						players[i] = null;
    					} else {
    						Client o = (Client) Server.playerHandler.players[i];
    						//if(o.g) {
    							if(!players[i].initialized) {
    								[B]players[i].initialize();[/B]
    								players[i].initialized = true;
    							}
    							else {
    								players[i].update();
    							}


    Server.java - Line 176

    playerHandler.process();


    It's on a Windows 64-bit VPS, so it's not like on Linux for capitalisation etc.

    Any help?
    Thanks.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Small problem, help please?

    at server.util.MysqlManager.checkVote(MysqlManager.ja va:43)
    What variable is null at line 43? Check back through the code to see why it has not been given a valid value.

  3. #3
    Junior Member
    Join Date
    Aug 2011
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Small problem, help please?

    I don't know what you mean.

    I'll post my MysqlManager.java file, sec.

    #2438939 - Pastie

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Small problem, help please?

    I don't know what you mean.
    Do you know what a program variable is?
    For example in the following line of code var is the variable and doSomething is a method:
    var.doSomething()
    If var has a null value you will get a NullPointerException on that line.

    Look at line 43 in MysqlManager.java
    What variables are on that line that can be null?
    Add a println to print out the values of all that variables used on that line. For example for the line I showed above, I'd add this just before the line to show the value of var:
    System.out.println("var=" + var);

  5. #5
    Junior Member
    Join Date
    Aug 2011
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Small problem, help please?

    I'm new at programming, so I don't really know all the ins and outs.

    It appears Line 43 didn't turn into size 5, so you couldn't see it.

    This is line 43 below:

    Statement stmt = conn.createStatement();

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Small problem, help please?

    Where does the variable conn get assigned a value?
    The error message says it has a value of null. It needs to have a valid value that points to an object of the class that has the createStatement method.

  7. #7
    Junior Member
    Join Date
    Aug 2011
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Small problem, help please?

    Sorry for sounding like a complete noob, but where would I find this?

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Small problem, help please?

    Assignment statements look like this:
    conn = <HERE IS THE VALUE TO ASSIGN TO conn>;

    Search your source code for statements with "conn =" or "conn="

    Or better search for ALL the statements that use the variable: conn.

  9. #9
    Junior Member
    Join Date
    Aug 2011
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Small problem, help please?

    Oh wait, is it this?

    	public static Connection conn = null;
    	public static Statement statement = null;
    	public static ResultSet results = null;

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Small problem, help please?

    As you can see from the code you posted, conn has a value of null.
    It needs to have the address of a Connection object.

    Where did you get this code from? You should go to that location or person and find out why the conn variable is not being given a valid value.

  11. #11
    Junior Member
    Join Date
    Aug 2011
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Small problem, help please?

    How would I assign it the address of a Connection object?

    I got it from here : [PI] Auto Donation System with Items - NEW

    Is their anyway you can help me, because the OP isn't active anymore.

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Small problem, help please?

    How would I assign it the address of a Connection object?
    Time for some studying.
    Read up on how to do database programming. Go to this site and Find database
    The Really Big Index

  13. #13
    Junior Member
    Join Date
    Aug 2011
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Small problem, help please?

    Uh, I hate when people say that.

    I know I have to learn, but I can't learn if I don't know what to look at, change etc.

    Do you have MSN/Skype so we could chat on there?

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Small problem, help please?

    Do you have MSN/Skype so we could chat on there?
    If wouldn't do you any good. I would tell you the same thing. Read the tutorial and get an understanding about what a database is and how to use it.
    There is a LOT to learn before you can write this program. There is no quick, easy short cut.

  15. #15
    Junior Member
    Join Date
    Aug 2011
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Small problem, help please?

    I can do MySQL databases but I don't know how to use them in my server.

    All I need to do is set conn a value, how do I do it. It's not that complicated.

  16. #16
    Junior Member
    Join Date
    Aug 2011
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Small problem, help please?

    Also, I don't even know what part I'm supposed to be looking at on that website.

  17. #17
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Small problem, help please?

    what part I'm supposed to be looking at on that website.
    The part about databases

  18. #18

  19. #19
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Small problem, help please?

    Start with the first one and then do the second one.

  20. #20
    Junior Member
    Join Date
    Aug 2011
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Small problem, help please?

    It doesn't say anything about assigning a connection a value...

    This is my SQL table.

    CREATE TABLE `status` (
    `id` INT(10) NOT NULL AUTO_INCREMENT,
    `date` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    `username` VARCHAR(15) NULL,
    `item` SMALLINT(5) NULL,
    `quantity` SMALLINT(5) NULL,
    `given` TINYINT(1) NOT NULL DEFAULT '0',
    PRIMARY KEY (`id`)
    )

  21. #21
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Small problem, help please?

    Sorry, I know nothing about SQL and how to use databases.

  22. #22
    Junior Member
    Join Date
    Aug 2011
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Small problem, help please?

    So you told me to read The Big Index because you don't know how to fix it? :3

  23. #23
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Small problem, help please?

    I gave you the Big Index because you will be needing it for lots of Java programming problems.
    Starting from the Big Index you can find the section where need specific information for today's problem.
    Tomorrow with another problem, you'll start with the Big Index again, find the section you need help with and read from there.

    If I'd given you the index to the page with the Connection class info. Tomorrow some one would have to give you the index to the page with the LinkedList class info. Next day the link to the page with the Painting info.
    You can now find all that yourself by starting at the Big Index.
    I hope that is not too hard for you to do.

  24. #24
    Junior Member
    Join Date
    Aug 2011
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Small problem, help please?

    So I need to go to the connection class info?

  25. #25
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Small problem, help please?

    Did you look thru the code you posted a link to? The MysqlManager class definition.
    Did you do a search for "conn =" in that code as I suggested back in post#8?

Page 1 of 2 12 LastLast

Similar Threads

  1. Small help needed
    By javabeg in forum Java Theory & Questions
    Replies: 4
    Last Post: March 15th, 2011, 09:50 AM
  2. keylistener small problem
    By matecno in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 14th, 2010, 08:51 PM
  3. Small Project
    By 3XiLED in forum Paid Java Projects
    Replies: 7
    Last Post: March 1st, 2010, 08:35 AM
  4. small problem ... I need help
    By Ashar in forum Loops & Control Statements
    Replies: 4
    Last Post: December 4th, 2009, 11:26 AM
  5. Small Project Ideas
    By Freaky Chris in forum Project Collaboration
    Replies: 20
    Last Post: August 12th, 2009, 12:49 PM